Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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