Version: v0.0.0-...-5dc576f Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2020 License: GPL-3.0 Imports: 6 Imported by: 0




This section is empty.


This section is empty.


This section is empty.


type HTTPMiddleware

type HTTPMiddleware struct {
	// contains filtered or unexported fields

HTTPMiddleware is an object that knows how to measure an HTTP handler by wrapping another handler.

func NewHTTPMiddleware

func NewHTTPMiddleware(cfg HTTPMiddlewareConfig) HTTPMiddleware

NewHTTPMiddleware creates a new HTTP Middleware object.

func (*HTTPMiddleware) Measure

func (m *HTTPMiddleware) Measure(reporter HTTPReporter, next func())

Measure abstracts the HTTP handler implementation by only requesting an HTTP reporter, this reporter will return the required data to be measured. It accepts a next function that will be called as the wrapped logic before and after measurement actions.

type HTTPMiddlewareConfig

type HTTPMiddlewareConfig struct {
	// Recorder is the way the HTTP metrics will be recorded in the different backends.
	// By default it will use OpenTelemetry.
	Recorder metric.HTTPRecorder
	// GroupedStatus will group the status label in the form of `\dxx`, for example,
	// 200, 201, and 203 will have the label `status="2xx"`. This impacts on the cardinality
	// of the metrics and also improves the performance of queries that are grouped by
	// status code because they are already aggregated in the metric.
	// By default it will be set to false.
	GroupedStatus bool
	// DisableMeasureReqSize will disable the recording metrics about the request size,
	// by default measuring request size is enabled (`DisableMeasureReqSize` is false).
	DisableMeasureReqSize bool
	// DisableMeasureRespSize will disable the recording metrics about the response size,
	// by default measuring response size is enabled (`DisableMeasureRespSize` is false).
	DisableMeasureRespSize bool
	// DisableMeasureInflight will disable the recording metrics about the inflight requests,
	// by default measuring inflights is enabled (`DisableMeasureInflight` is false).
	DisableMeasureInflight bool

HTTPMiddlewareConfig stores configurations for the HTTP middleware factory.

type HTTPReporter

type HTTPReporter interface {
	Context() context.Context
	URLHost() string
	URLPath() string
	Method() string
	StatusCode() int
	RequestSize() int64
	ResponseSize() int64

HTTPReporter abstracts the ways to report the required data for measuring HTTP metrics to the middleware object.

The most important thing to pay attention to when implementing this interface is to make sure the `URLPath()` function groups all endpoints with path parameters as a single URL path. As an example, `/api/v1/providers/:name` endpoint has `:name` as a path parameter, when multiple requests hit the endpoint using the following URL path: - /api/v1/providers/aws - /api/v1/providers/gcp - /api/v1/providers/azure then the reporter should only produce 1 metric with `/api/v1/providers/:name` as its endpoint label and an aggregated value, instead of 3 metrics with 3 different endpoint labels matching the requested URL path and 3 non-aggregated values. Failing to do this will cause the produced metrics to have extremely high cardinality which is against the recommended best practice. Please refer to the following article to learn more about this topic: https://banzaicloud.com/blog/monitoring-gin-with-prometheus/

Source Files


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL