diff --git a/cmd/agent/app/reporter/grpc/builder.go b/cmd/agent/app/reporter/grpc/builder.go index ccc2a071c22..9e4bb2c932c 100644 --- a/cmd/agent/app/reporter/grpc/builder.go +++ b/cmd/agent/app/reporter/grpc/builder.go @@ -47,6 +47,8 @@ type ConnBuilder struct { DiscoveryMinPeers int Notifier discovery.Notifier Discoverer discovery.Discoverer + + AdditionalDialOptions []grpc.DialOption } // NewConnBuilder creates a new grpc connection builder. @@ -96,6 +98,8 @@ func (b *ConnBuilder) CreateConnection(logger *zap.Logger, mFactory metrics.Fact } dialOptions = append(dialOptions, grpc.WithDefaultServiceConfig(grpcresolver.GRPCServiceConfig)) dialOptions = append(dialOptions, grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor(grpc_retry.WithMax(b.MaxRetry)))) + dialOptions = append(dialOptions, b.AdditionalDialOptions...) + conn, err := grpc.Dial(dialTarget, dialOptions...) if err != nil { return nil, err diff --git a/cmd/agent/app/reporter/grpc/builder_test.go b/cmd/agent/app/reporter/grpc/builder_test.go index 0f792171149..f70de066cf4 100644 --- a/cmd/agent/app/reporter/grpc/builder_test.go +++ b/cmd/agent/app/reporter/grpc/builder_test.go @@ -395,3 +395,33 @@ func assertConnectionState(t *testing.T, conn *grpc.ClientConn, expectedState st } } } + +type fakeInterceptor struct { + isCalled bool +} + +func (f *fakeInterceptor) intercept(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { + f.isCalled = true + return invoker(ctx, method, req, reply, cc, opts...) +} + +func (f *fakeInterceptor) assertCalled(t *testing.T) { + assert.True(t, f.isCalled) +} + +func TestBuilderWithAdditionalDialOptions(t *testing.T) { + fi := fakeInterceptor{} + defer fi.assertCalled(t) + + cb := ConnBuilder{ + CollectorHostPorts: []string{"127.0.0.1:14268"}, + AdditionalDialOptions: []grpc.DialOption{grpc.WithUnaryInterceptor(fi.intercept)}, + } + + r, err := cb.CreateConnection(zap.NewNop(), metrics.NullFactory) + require.NoError(t, err) + assert.NotNil(t, r) + + err = r.Invoke(context.Background(), "test", map[string]string{}, map[string]string{}, []grpc.CallOption{}...) + assert.Error(t, err, "should error because no server is running") +}