From 20b84161d1bb031f87dc3b90554f12a561055429 Mon Sep 17 00:00:00 2001 From: Hemant Kumar Date: Fri, 11 Aug 2023 15:44:26 -0400 Subject: [PATCH] Not all grpc connections need metrics GRPC client connection that node register makes as such doesn't need metrics and hence allow connections to be made without metrics --- connection/connection.go | 16 ++++++++++++---- connection/connection_test.go | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/connection/connection.go b/connection/connection.go index 41e8908f..83ac367b 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -73,6 +73,11 @@ func Connect(address string, metricsManager metrics.CSIMetricsManager, options . return connect(address, metricsManager, []grpc.DialOption{grpc.WithTimeout(time.Second * 30)}, options) } +// ConnectWithoutMetrics behaves exactly like Connect except no metrics are recorded. +func ConnectWithoutMetrics(address string, options ...Option) (*grpc.ClientConn, error) { + return connect(address, nil, []grpc.DialOption{grpc.WithTimeout(time.Second * 30)}, options) +} + // Option is the type of all optional parameters for Connect. type Option func(o *options) @@ -118,11 +123,14 @@ func connect( grpc.WithInsecure(), // Don't use TLS, it's usually local Unix domain socket in a container. grpc.WithBackoffMaxDelay(time.Second), // Retry every second after failure. grpc.WithBlock(), // Block until connection succeeds. - grpc.WithChainUnaryInterceptor( - LogGRPC, // Log all messages. - ExtendedCSIMetricsManager{metricsManager}.RecordMetricsClientInterceptor, // Record metrics for each gRPC call. - ), ) + + interceptors := []grpc.UnaryClientInterceptor{LogGRPC} + if metricsManager != nil { + interceptors = append(interceptors, ExtendedCSIMetricsManager{metricsManager}.RecordMetricsClientInterceptor) + } + dialOptions = append(dialOptions, grpc.WithChainUnaryInterceptor(interceptors...)) + unixPrefix := "unix://" if strings.HasPrefix(address, "/") { // It looks like filesystem path. diff --git a/connection/connection_test.go b/connection/connection_test.go index 895eab5a..fd6ae6f1 100644 --- a/connection/connection_test.go +++ b/connection/connection_test.go @@ -132,6 +132,21 @@ func TestConnectUnix(t *testing.T) { } } +func TestConnectWithoutMetrics(t *testing.T) { + tmp := tmpDir(t) + defer os.RemoveAll(tmp) + addr, stopServer := startServer(t, tmp, nil, nil, nil) + defer stopServer() + + conn, err := ConnectWithoutMetrics("unix:///" + addr) + if assert.NoError(t, err, "connect with unix:/// prefix") && + assert.NotNil(t, conn, "got a connection") { + assert.Equal(t, connectivity.Ready, conn.GetState(), "connection ready") + err = conn.Close() + assert.NoError(t, err, "closing connection") + } +} + func TestWaitForServer(t *testing.T) { tmp := tmpDir(t) defer os.RemoveAll(tmp)