Skip to content

Commit

Permalink
Not all grpc connections need metrics
Browse files Browse the repository at this point in the history
GRPC client connection that node register makes as such doesn't need
metrics and hence allow connections to be made without metrics
  • Loading branch information
gnufied committed Aug 11, 2023
1 parent b6ca620 commit 20b8416
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 12 additions & 4 deletions connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions connection/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 20b8416

Please sign in to comment.