From f2a9f2acb48807ec1b2a0e397615e289863a3920 Mon Sep 17 00:00:00 2001 From: Abhishek Ranjan Date: Thu, 19 Dec 2024 13:27:24 +0530 Subject: [PATCH] fix breaking tests --- balancer/rls/cache_test.go | 11 +++++------ .../testutils/stats/test_metrics_recorder.go | 19 +++++++++++++++++++ stats/opentelemetry/client_metrics.go | 2 +- stats/opentelemetry/opentelemetry.go | 16 +++++++++------- stats/opentelemetry/server_metrics.go | 2 +- test/creds_test.go | 2 +- 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/balancer/rls/cache_test.go b/balancer/rls/cache_test.go index c75e2add52cb..52239deb77d6 100644 --- a/balancer/rls/cache_test.go +++ b/balancer/rls/cache_test.go @@ -19,7 +19,6 @@ package rls import ( - "google.golang.org/grpc/stats/opentelemetry" "testing" "time" @@ -121,7 +120,7 @@ func (s) TestLRU_BasicOperations(t *testing.T) { func (s) TestDataCache_BasicOperations(t *testing.T) { initCacheEntries() - dc := newDataCache(5, nil, &opentelemetry.NoopMetricsRecorder{}, "") + dc := newDataCache(5, nil, &stats.NoopMetricsRecorder{}, "") for i, k := range cacheKeys { dc.addEntry(k, cacheEntries[i]) } @@ -135,7 +134,7 @@ func (s) TestDataCache_BasicOperations(t *testing.T) { func (s) TestDataCache_AddForcesResize(t *testing.T) { initCacheEntries() - dc := newDataCache(1, nil, &opentelemetry.NoopMetricsRecorder{}, "") + dc := newDataCache(1, nil, &stats.NoopMetricsRecorder{}, "") // The first entry in cacheEntries has a minimum expiry time in the future. // This entry would stop the resize operation since we do not evict entries @@ -164,7 +163,7 @@ func (s) TestDataCache_AddForcesResize(t *testing.T) { func (s) TestDataCache_Resize(t *testing.T) { initCacheEntries() - dc := newDataCache(5, nil, &opentelemetry.NoopMetricsRecorder{}, "") + dc := newDataCache(5, nil, &stats.NoopMetricsRecorder{}, "") for i, k := range cacheKeys { dc.addEntry(k, cacheEntries[i]) } @@ -195,7 +194,7 @@ func (s) TestDataCache_Resize(t *testing.T) { func (s) TestDataCache_EvictExpiredEntries(t *testing.T) { initCacheEntries() - dc := newDataCache(5, nil, &opentelemetry.NoopMetricsRecorder{}, "") + dc := newDataCache(5, nil, &stats.NoopMetricsRecorder{}, "") for i, k := range cacheKeys { dc.addEntry(k, cacheEntries[i]) } @@ -222,7 +221,7 @@ func (s) TestDataCache_ResetBackoffState(t *testing.T) { } initCacheEntries() - dc := newDataCache(5, nil, &opentelemetry.NoopMetricsRecorder{}, "") + dc := newDataCache(5, nil, &stats.NoopMetricsRecorder{}, "") for i, k := range cacheKeys { dc.addEntry(k, cacheEntries[i]) } diff --git a/internal/testutils/stats/test_metrics_recorder.go b/internal/testutils/stats/test_metrics_recorder.go index c521e7e3465e..e1a03b8d8008 100644 --- a/internal/testutils/stats/test_metrics_recorder.go +++ b/internal/testutils/stats/test_metrics_recorder.go @@ -275,3 +275,22 @@ func (r *TestMetricsRecorder) TagConn(ctx context.Context, _ *stats.ConnTagInfo) // HandleConn is TestMetricsRecorder's implementation of HandleConn. func (r *TestMetricsRecorder) HandleConn(context.Context, stats.ConnStats) {} + +// NoopMetricsRecorder is a noop MetricsRecorder to be used in tests to prevent +// nil panics. +type NoopMetricsRecorder struct{} + +// RecordInt64Count is a noop implementation of RecordInt64Count. +func (r *NoopMetricsRecorder) RecordInt64Count(*estats.Int64CountHandle, int64, ...string) {} + +// RecordFloat64Count is a noop implementation of RecordFloat64Count. +func (r *NoopMetricsRecorder) RecordFloat64Count(*estats.Float64CountHandle, float64, ...string) {} + +// RecordInt64Histo is a noop implementation of RecordInt64Histo. +func (r *NoopMetricsRecorder) RecordInt64Histo(*estats.Int64HistoHandle, int64, ...string) {} + +// RecordFloat64Histo is a noop implementation of RecordFloat64Histo. +func (r *NoopMetricsRecorder) RecordFloat64Histo(*estats.Float64HistoHandle, float64, ...string) {} + +// RecordInt64Gauge is a noop implementation of RecordInt64Gauge. +func (r *NoopMetricsRecorder) RecordInt64Gauge(*estats.Int64GaugeHandle, int64, ...string) {} diff --git a/stats/opentelemetry/client_metrics.go b/stats/opentelemetry/client_metrics.go index 0b1823dfc744..91b7822df396 100644 --- a/stats/opentelemetry/client_metrics.go +++ b/stats/opentelemetry/client_metrics.go @@ -61,7 +61,7 @@ func (h *clientStatsHandler) initializeMetrics() { // Will set no metrics to record, logically making this stats handler a // no-op. if h.options.MetricsOptions.MeterProvider == nil { - h.MetricsRecorder = &NoopMetricsRecorder{} + h.MetricsRecorder = &OtelNoopMetricsRecorder{} h.options.MetricsOptions.MeterProvider = metric.NewMeterProvider() h.setClientMetrics() return diff --git a/stats/opentelemetry/opentelemetry.go b/stats/opentelemetry/opentelemetry.go index e0400745ce82..5f773d75870f 100644 --- a/stats/opentelemetry/opentelemetry.go +++ b/stats/opentelemetry/opentelemetry.go @@ -395,21 +395,23 @@ func DefaultMetrics() *stats.MetricSet { return defaultPerCallMetrics.Join(estats.DefaultMetrics) } -// NoopMetricsRecorder is a noop MetricsRecorder to be used in tests to prevent +// OtelNoopMetricsRecorder is a noop MetricsRecorder to be used to prevent // nil panics. -type NoopMetricsRecorder struct{} +type OtelNoopMetricsRecorder struct{} // RecordInt64Count is a noop implementation of RecordInt64Count. -func (r *NoopMetricsRecorder) RecordInt64Count(*estats.Int64CountHandle, int64, ...string) {} +func (r *OtelNoopMetricsRecorder) RecordInt64Count(*estats.Int64CountHandle, int64, ...string) {} // RecordFloat64Count is a noop implementation of RecordFloat64Count. -func (r *NoopMetricsRecorder) RecordFloat64Count(*estats.Float64CountHandle, float64, ...string) {} +func (r *OtelNoopMetricsRecorder) RecordFloat64Count(*estats.Float64CountHandle, float64, ...string) { +} // RecordInt64Histo is a noop implementation of RecordInt64Histo. -func (r *NoopMetricsRecorder) RecordInt64Histo(*estats.Int64HistoHandle, int64, ...string) {} +func (r *OtelNoopMetricsRecorder) RecordInt64Histo(*estats.Int64HistoHandle, int64, ...string) {} // RecordFloat64Histo is a noop implementation of RecordFloat64Histo. -func (r *NoopMetricsRecorder) RecordFloat64Histo(*estats.Float64HistoHandle, float64, ...string) {} +func (r *OtelNoopMetricsRecorder) RecordFloat64Histo(*estats.Float64HistoHandle, float64, ...string) { +} // RecordInt64Gauge is a noop implementation of RecordInt64Gauge. -func (r *NoopMetricsRecorder) RecordInt64Gauge(*estats.Int64GaugeHandle, int64, ...string) {} +func (r *OtelNoopMetricsRecorder) RecordInt64Gauge(*estats.Int64GaugeHandle, int64, ...string) {} diff --git a/stats/opentelemetry/server_metrics.go b/stats/opentelemetry/server_metrics.go index 25c01be9b3f9..fa2801c86042 100644 --- a/stats/opentelemetry/server_metrics.go +++ b/stats/opentelemetry/server_metrics.go @@ -60,7 +60,7 @@ func (h *serverStatsHandler) initializeMetrics() { // Will set no metrics to record, logically making this stats handler a // no-op. if h.options.MetricsOptions.MeterProvider == nil { - h.MetricsRecorder = &NoopMetricsRecorder{} + h.MetricsRecorder = &OtelNoopMetricsRecorder{} h.options.MetricsOptions.MeterProvider = metric.NewMeterProvider() h.setServerMetrics() return diff --git a/test/creds_test.go b/test/creds_test.go index c9b8862442db..6cde39c57733 100644 --- a/test/creds_test.go +++ b/test/creds_test.go @@ -262,7 +262,7 @@ func (s) TestWaitForReadyRPCErrorOnBadCertificates(t *testing.T) { defer te.tearDown() opts := []grpc.DialOption{grpc.WithTransportCredentials(clientAlwaysFailCred{})} - cc, err := grpc.NewClient(te.srvAddr, opts...) + cc, err := grpc.Dial(te.srvAddr, opts...) if err != nil { t.Fatalf("NewClient(_) = %v, want %v", err, nil) }