check / check (push) Waiting to run
The HTTP metrics recorder, the delivery collectors and the Go and process collectors now register on one prometheus.Registry that fx provides, instead of Prometheus's global default registry, and /metrics serves that registry. A second metrics-enabled router in one process, or the server tests run with -count=2, no longer panics on a duplicate registration. The middleware builds its recorder once, in New, so installing Metrics() on more than one router over the same graph is also safe; NewForTest gives its Middleware a recorder on a fresh registry. The scrape keeps the same series and labels, including go_*, process_* and promhttp_metric_handler_*. Model: opus-5-5
33 lines
807 B
Go
33 lines
807 B
Go
package middleware
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
prommetrics "github.com/slok/go-http-metrics/metrics/prometheus"
|
|
"sneak.berlin/go/webhooker/internal/config"
|
|
"sneak.berlin/go/webhooker/internal/session"
|
|
)
|
|
|
|
// NewForTest creates a Middleware with the minimum dependencies
|
|
// needed for testing. This bypasses the fx lifecycle.
|
|
//
|
|
// Its metrics recorder writes to a fresh registry of its own, so
|
|
// Metrics() works on it and two of them never collide.
|
|
func NewForTest(
|
|
log *slog.Logger,
|
|
cfg *config.Config,
|
|
sess *session.Session,
|
|
) *Middleware {
|
|
return &Middleware{
|
|
log: log,
|
|
params: &MiddlewareParams{
|
|
Config: cfg,
|
|
},
|
|
session: sess,
|
|
metricsRecorder: prommetrics.NewRecorder(
|
|
prommetrics.Config{Registry: prometheus.NewRegistry()},
|
|
),
|
|
}
|
|
}
|