Documentation
¶
Overview ¶
Package metrics provides contrib HTTP metrics middleware.
The middleware records request counts and durations through a supplied recorder, using low-cardinality method, route, and status labels. See docs/metrics.md for label policy and bootstrap defaults.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrIncompatibleCollectorRegistration = errors.New("metrics: incompatible collector already registered")
ErrIncompatibleCollectorRegistration reports that a Prometheus metric name is already registered with an incompatible collector type or descriptor shape.
Functions ¶
func PrometheusHandler ¶
PrometheusHandler returns a standard /metrics http.Handler if the Prometheus client is linked; otherwise returns http.NotFoundHandler. This indirection avoids hard dependency on the Prometheus client.
Types ¶
type MetricsRecorder ¶
type MetricsRecorder interface {
IncCounter(name string, labels Labels)
ObserveHistogram(name string, value float64, labels Labels)
}
MetricsRecorder captures counters and histograms.
type Middleware ¶
type Middleware struct {
M MetricsRecorder
Clock ports.Clock
}
Middleware instruments HTTP traffic using a provided recorder.
func (*Middleware) Handler ¶
func (mw *Middleware) Handler(next http.Handler) http.Handler
Handler wraps the next handler to record counters and duration. It is intended to emit exactly one observation on both normal and panic paths. For panic paths, it should infer the final visible status before re-panicking so outer recovery can still produce the response contract.
func (*Middleware) HandlerFunc ¶
func (mw *Middleware) HandlerFunc() func(http.Handler) http.Handler
HandlerFunc exposes middleware as a plain function for router use.
func (*Middleware) Middleware ¶
func (mw *Middleware) Middleware() func(http.Handler) http.Handler
Middleware implements ports.Middleware by returning Handler.
type NoopMetrics ¶
type NoopMetrics struct{}
NoopMetrics is the default. Swap later for Prometheus, etc.
func (NoopMetrics) IncCounter ¶
func (NoopMetrics) IncCounter(_ string, _ Labels)
IncCounter is a no-op implementation.
func (NoopMetrics) ObserveHistogram ¶
func (NoopMetrics) ObserveHistogram(_ string, _ float64, _ Labels)
ObserveHistogram is a no-op implementation.
type Options ¶
type Options struct {
Recorder MetricsRecorder
Clock ports.Clock
}
Options configures the metrics middleware.
type PrometheusRecorder ¶
type PrometheusRecorder struct {
// contains filtered or unexported fields
}
PrometheusRecorder implements MetricsRecorder using Prometheus client. This is a minimal adapter; applications can supply their own recorder.
func NewPrometheusRecorder ¶
func NewPrometheusRecorder(registerer prometheus.Registerer, buckets []float64) *PrometheusRecorder
NewPrometheusRecorder wires counters and histograms with standard names. Consumers may pass a custom registerer (e.g. for testing). When nil, the default Prometheus registerer is used.
func NewPrometheusRecorderChecked ¶ added in v2.1.0
func NewPrometheusRecorderChecked(registerer prometheus.Registerer, buckets []float64) (*PrometheusRecorder, error)
NewPrometheusRecorderChecked wires counters and histograms with standard names and returns registration conflicts instead of panicking.
func (*PrometheusRecorder) IncCounter ¶
func (p *PrometheusRecorder) IncCounter(_ string, labels Labels)
IncCounter increments the Prometheus counter.
func (*PrometheusRecorder) ObserveHistogram ¶
func (p *PrometheusRecorder) ObserveHistogram(_ string, value float64, labels Labels)
ObserveHistogram records the Prometheus histogram observation.