metrics: restore global registry in unit tests (#5399)

Currently there appears to be a test order dependency between a couple
of the info_test.go test cases and the Test_PrometheusHandler test. This
can be exposed by running:

  go test -count 2 ./internal/telemetry/metrics

The test cases in info_test.go overwrite the global 'registry' variable,
which seems to prevent Test_PrometheusHandler from being able to export
the internal Go metrics. Add a helper method to restore the original
registry after these test cases.
This commit is contained in:
Kenneth Jenkins 2024-12-18 13:21:06 -08:00 committed by GitHub
parent 69cb6f53de
commit 247cd175fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -65,7 +65,7 @@ func Test_SetDBConfigInfo(t *testing.T) {
}
func Test_SetBuildInfo(t *testing.T) {
registry = newMetricRegistry()
initTemporaryMetricsRegistry(t)
version.Version = "v0.0.1"
version.GitCommit = "deadbeef"
@ -84,7 +84,7 @@ func Test_SetBuildInfo(t *testing.T) {
}
func Test_AddPolicyCountCallback(t *testing.T) {
registry = newMetricRegistry()
initTemporaryMetricsRegistry(t)
wantValue := int64(42)
wantLabels := []metricdata.LabelValue{
@ -106,3 +106,9 @@ func Test_RegisterInfoMetrics(t *testing.T) {
t.Error("Did not find enough registries")
}
}
func initTemporaryMetricsRegistry(t *testing.T) {
original := registry
registry = newMetricRegistry()
t.Cleanup(func() { registry = original })
}