Skip to content

Commit 0f69380

Browse files
authored
Merge pull request #143 from gnufied/allow-connection-without-metrics
Not all grpc connections need metrics
2 parents b6ca620 + 20b8416 commit 0f69380

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

connection/connection.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func Connect(address string, metricsManager metrics.CSIMetricsManager, options .
7373
return connect(address, metricsManager, []grpc.DialOption{grpc.WithTimeout(time.Second * 30)}, options)
7474
}
7575

76+
// ConnectWithoutMetrics behaves exactly like Connect except no metrics are recorded.
77+
func ConnectWithoutMetrics(address string, options ...Option) (*grpc.ClientConn, error) {
78+
return connect(address, nil, []grpc.DialOption{grpc.WithTimeout(time.Second * 30)}, options)
79+
}
80+
7681
// Option is the type of all optional parameters for Connect.
7782
type Option func(o *options)
7883

@@ -118,11 +123,14 @@ func connect(
118123
grpc.WithInsecure(), // Don't use TLS, it's usually local Unix domain socket in a container.
119124
grpc.WithBackoffMaxDelay(time.Second), // Retry every second after failure.
120125
grpc.WithBlock(), // Block until connection succeeds.
121-
grpc.WithChainUnaryInterceptor(
122-
LogGRPC, // Log all messages.
123-
ExtendedCSIMetricsManager{metricsManager}.RecordMetricsClientInterceptor, // Record metrics for each gRPC call.
124-
),
125126
)
127+
128+
interceptors := []grpc.UnaryClientInterceptor{LogGRPC}
129+
if metricsManager != nil {
130+
interceptors = append(interceptors, ExtendedCSIMetricsManager{metricsManager}.RecordMetricsClientInterceptor)
131+
}
132+
dialOptions = append(dialOptions, grpc.WithChainUnaryInterceptor(interceptors...))
133+
126134
unixPrefix := "unix://"
127135
if strings.HasPrefix(address, "/") {
128136
// It looks like filesystem path.

connection/connection_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,21 @@ func TestConnectUnix(t *testing.T) {
132132
}
133133
}
134134

135+
func TestConnectWithoutMetrics(t *testing.T) {
136+
tmp := tmpDir(t)
137+
defer os.RemoveAll(tmp)
138+
addr, stopServer := startServer(t, tmp, nil, nil, nil)
139+
defer stopServer()
140+
141+
conn, err := ConnectWithoutMetrics("unix:///" + addr)
142+
if assert.NoError(t, err, "connect with unix:/// prefix") &&
143+
assert.NotNil(t, conn, "got a connection") {
144+
assert.Equal(t, connectivity.Ready, conn.GetState(), "connection ready")
145+
err = conn.Close()
146+
assert.NoError(t, err, "closing connection")
147+
}
148+
}
149+
135150
func TestWaitForServer(t *testing.T) {
136151
tmp := tmpDir(t)
137152
defer os.RemoveAll(tmp)

0 commit comments

Comments
 (0)