go test -count=2 ./internal/server/... panics on duplicate metrics collector registration #227
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Found during the review of #224, then reproduced on plain
origin/next(bb30b3a) with that branch absent. It is a defect innext, not in any PR.Path:
routes_test.go:734 TestMetricsRouteRequiresCredentials->middleware.Metrics-> go-http-metricsNewRecorder->MustRegister(internal/middleware/middleware.go:444).metrics.NewRecorder(metrics.Config{})leavesConfig.Registrydefaulted toprometheus.DefaultRegisterer, andMustRegisterpanics on a duplicate.TestMetricsRouteRequiresCredentialsarrived with #216 — the firstinternal/servertest to construct a metrics-enabled router. One registration happens per test binary today, so the normal gate passes; a second metrics-enabled router test, or-count=2, panics.Why it is worth fixing rather than tolerating:
-count=2is the ordinary way to smoke out state leaking between runs, and it is currently unavailable forinternal/server. It also means any future test that builds a second metrics-enabled router fails for a reason unrelated to what it is testing, which is a trap for whoever writes it.Remedy (proposed by two independent reviewers): give
middleware.Newa dedicatedprometheus.Registryand serve/metricsthrough a matching gatherer, instead of the global default registerer. That also decouples the delivery collectors added by #209 from the global default.Note the delivery collectors in #224 are NOT implicated — they register via
sync.OnceValueand pass-count=2.Definition of done:
go test -count=2 ./internal/server/...passes/metricsstill exposes the three existinghttp_*series and the delivery series from #209/metricsroute and no second scrape path