check / check (push) Successful in 3m25s
A second metrics-enabled router in one process panicked on a duplicate collector registration, because every collector registered on Prometheus's global default registry. metrics.NewRegistry now builds one registry with the Go runtime and process collectors; fx provides it and the delivery metric set built on it. The middleware builds its HTTP recorder once on that registry (NewForTest on a fresh one), the engine and handlers take the metric set from fx, and nothing registers on the global default any more. /metrics is served from the new registry with the same series names, labels and auth. A test builds two metrics-enabled routers in one process. 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()},
|
|
),
|
|
}
|
|
}
|