diff --git a/experimental/stats/metricregistry.go b/experimental/stats/metricregistry.go index 930140f57ed9..1d827dd5d9d4 100644 --- a/experimental/stats/metricregistry.go +++ b/experimental/stats/metricregistry.go @@ -20,7 +20,6 @@ package stats import ( "maps" - "testing" "google.golang.org/grpc/grpclog" "google.golang.org/grpc/internal" @@ -250,9 +249,9 @@ func RegisterInt64Gauge(descriptor MetricDescriptor) *Int64GaugeHandle { } // snapshotMetricsRegistryForTesting snapshots the global data of the metrics -// registry. Registers a cleanup function on the provided testing.T that sets -// the metrics registry to its original state. Only called in testing functions. -func snapshotMetricsRegistryForTesting(t *testing.T) { +// registry. Returns a cleanup function that sets the metrics registry to its +// original state. +func snapshotMetricsRegistryForTesting() func() { oldDefaultMetrics := DefaultMetrics oldRegisteredMetrics := registeredMetrics oldMetricsRegistry := metricsRegistry @@ -262,9 +261,9 @@ func snapshotMetricsRegistryForTesting(t *testing.T) { maps.Copy(registeredMetrics, registeredMetrics) maps.Copy(metricsRegistry, metricsRegistry) - t.Cleanup(func() { + return func() { DefaultMetrics = oldDefaultMetrics registeredMetrics = oldRegisteredMetrics metricsRegistry = oldMetricsRegistry - }) + } } diff --git a/experimental/stats/metricregistry_test.go b/experimental/stats/metricregistry_test.go index 6db7cf9ee402..83d7a5838b7c 100644 --- a/experimental/stats/metricregistry_test.go +++ b/experimental/stats/metricregistry_test.go @@ -38,7 +38,9 @@ func Test(t *testing.T) { // TestPanic tests that registering two metrics with the same name across any // type of metric triggers a panic. func (s) TestPanic(t *testing.T) { - snapshotMetricsRegistryForTesting(t) + cleanup := snapshotMetricsRegistryForTesting() + defer cleanup() + want := "metric simple counter already registered" defer func() { if r := recover(); !strings.Contains(fmt.Sprint(r), want) { @@ -64,7 +66,9 @@ func (s) TestPanic(t *testing.T) { // this tests the interactions between the metrics recorder and the metrics // registry. func (s) TestMetricRegistry(t *testing.T) { - snapshotMetricsRegistryForTesting(t) + cleanup := snapshotMetricsRegistryForTesting() + defer cleanup() + intCountHandle1 := RegisterInt64Count(MetricDescriptor{ Name: "simple counter", Description: "sum of all emissions from tests", @@ -141,7 +145,9 @@ func (s) TestMetricRegistry(t *testing.T) { // metric registry. A component (simulated by test) should be able to record on // the different registered int count metrics. func TestNumerousIntCounts(t *testing.T) { - snapshotMetricsRegistryForTesting(t) + cleanup := snapshotMetricsRegistryForTesting() + defer cleanup() + intCountHandle1 := RegisterInt64Count(MetricDescriptor{ Name: "int counter", Description: "sum of all emissions from tests", diff --git a/internal/internal.go b/internal/internal.go index 71cbc3bd6185..7aae9240ffc0 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -217,10 +217,9 @@ var ( SetConnectedAddress any // func(scs *SubConnState, addr resolver.Address) // SnapshotMetricRegistryForTesting snapshots the global data of the metric - // registry. Registers a cleanup function on the provided testing.T that - // sets the metric registry to its original state. Only called in testing - // functions. - SnapshotMetricRegistryForTesting any // func(t *testing.T) + // registry. Returns a cleanup function that sets the metric registry to its + // original state. Only called in testing functions. + SnapshotMetricRegistryForTesting func() func() // SetDefaultBufferPoolForTesting updates the default buffer pool, for // testing purposes. diff --git a/internal/stats/metrics_recorder_list_test.go b/internal/stats/metrics_recorder_list_test.go index c58266a31bf8..15139f128158 100644 --- a/internal/stats/metrics_recorder_list_test.go +++ b/internal/stats/metrics_recorder_list_test.go @@ -132,7 +132,9 @@ type recordingLoadBalancer struct { // test then asserts that the recorded metrics show up on both configured stats // handlers. func (s) TestMetricsRecorderList(t *testing.T) { - internal.SnapshotMetricRegistryForTesting.(func(t *testing.T))(t) + cleanup := internal.SnapshotMetricRegistryForTesting() + defer cleanup() + mr := manual.NewBuilderWithScheme("test-metrics-recorder-list") defer mr.Close() @@ -212,7 +214,9 @@ func (s) TestMetricsRecorderList(t *testing.T) { // TestMetricRecorderListPanic tests that the metrics recorder list panics if // received the wrong number of labels for a particular metric. func (s) TestMetricRecorderListPanic(t *testing.T) { - internal.SnapshotMetricRegistryForTesting.(func(t *testing.T))(t) + cleanup := internal.SnapshotMetricRegistryForTesting() + defer cleanup() + intCountHandle := estats.RegisterInt64Count(estats.MetricDescriptor{ Name: "simple counter", Description: "sum of all emissions from tests", diff --git a/stats/opentelemetry/metricsregistry_test.go b/stats/opentelemetry/metricsregistry_test.go index 0d2bb956ea85..67a2d7faeadc 100644 --- a/stats/opentelemetry/metricsregistry_test.go +++ b/stats/opentelemetry/metricsregistry_test.go @@ -61,7 +61,9 @@ func newServerStatsHandler(options MetricsOptions) metricsRecorderForTest { // the expected metrics emissions, which includes default metrics and optional // label assertions. func (s) TestMetricsRegistryMetrics(t *testing.T) { - internal.SnapshotMetricRegistryForTesting.(func(t *testing.T))(t) + cleanup := internal.SnapshotMetricRegistryForTesting() + defer cleanup() + intCountHandle1 := estats.RegisterInt64Count(estats.MetricDescriptor{ Name: "int-counter-1", Description: "Sum of calls from test",