Skip to content

Commit

Permalink
[configgrpc] added tests for server/client extra options
Browse files Browse the repository at this point in the history
  • Loading branch information
jade-guiton-dd committed Sep 6, 2024
1 parent 38b99f8 commit 0ec83a3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 21 deletions.
41 changes: 26 additions & 15 deletions config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,19 +262,19 @@ func (gcs *ClientConfig) ToClientConnWithOptions(
settings component.TelemetrySettings,
extraOpts ...ToClientConnOption,
) (*grpc.ClientConn, error) {
grpcOpts, err := gcs.getGrpcDialOptions(ctx, host, settings)
grpcOpts, err := gcs.getGrpcDialOptions(ctx, host, settings, extraOpts)
if err != nil {
return nil, err
}
for _, opt := range extraOpts {
if wrapper, ok := opt.(grpcDialOptionWrapper); ok {
grpcOpts = append(grpcOpts, wrapper.opt)
}
}
return grpc.NewClient(gcs.sanitizedEndpoint(), grpcOpts...)
}

func (gcs *ClientConfig) getGrpcDialOptions(ctx context.Context, host component.Host, settings component.TelemetrySettings) ([]grpc.DialOption, error) {
func (gcs *ClientConfig) getGrpcDialOptions(
ctx context.Context,
host component.Host,
settings component.TelemetrySettings,
extraOpts []ToClientConnOption,
) ([]grpc.DialOption, error) {
var opts []grpc.DialOption
if gcs.Compression.IsCompressed() {
cp, err := getGRPCCompressionName(gcs.Compression)
Expand Down Expand Up @@ -351,6 +351,12 @@ func (gcs *ClientConfig) getGrpcDialOptions(ctx context.Context, host component.
// Enable OpenTelemetry observability plugin.
opts = append(opts, grpc.WithStatsHandler(otelgrpc.NewClientHandler(otelOpts...)))

for _, opt := range extraOpts {
if wrapper, ok := opt.(grpcDialOptionWrapper); ok {
opts = append(opts, wrapper.opt)
}
}

return opts, nil
}

Expand Down Expand Up @@ -409,21 +415,20 @@ func (gss *ServerConfig) ToServerWithOptions(
_ context.Context,
host component.Host,
settings component.TelemetrySettings,
opts ...ToServerOption,
extraOpts ...ToServerOption,
) (*grpc.Server, error) {
grpcOpts, err := gss.getGrpcServerOptions(host, settings)
grpcOpts, err := gss.getGrpcServerOptions(host, settings, extraOpts)
if err != nil {
return nil, err
}
for _, opt := range opts {
if wrapper, ok := opt.(grpcServerOptionWrapper); ok {
grpcOpts = append(grpcOpts, wrapper.opt)
}
}
return grpc.NewServer(grpcOpts...), nil
}

func (gss *ServerConfig) getGrpcServerOptions(host component.Host, settings component.TelemetrySettings) ([]grpc.ServerOption, error) {
func (gss *ServerConfig) getGrpcServerOptions(
host component.Host,
settings component.TelemetrySettings,
extraOpts []ToServerOption,
) ([]grpc.ServerOption, error) {
switch gss.NetAddr.Transport {
case confignet.TransportTypeTCP, confignet.TransportTypeTCP4, confignet.TransportTypeTCP6, confignet.TransportTypeUDP, confignet.TransportTypeUDP4, confignet.TransportTypeUDP6:
internal.WarnOnUnspecifiedHost(settings.Logger, gss.NetAddr.Endpoint)
Expand Down Expand Up @@ -513,6 +518,12 @@ func (gss *ServerConfig) getGrpcServerOptions(host component.Host, settings comp

opts = append(opts, grpc.StatsHandler(otelgrpc.NewServerHandler(otelOpts...)), grpc.ChainUnaryInterceptor(uInterceptors...), grpc.ChainStreamInterceptor(sInterceptors...))

for _, opt := range extraOpts {
if wrapper, ok := opt.(grpcServerOptionWrapper); ok {
opts = append(opts, wrapper.opt)
}
}

return opts, nil
}

Expand Down
51 changes: 45 additions & 6 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,33 @@ func TestDefaultGrpcClientSettings(t *testing.T) {
Insecure: true,
},
}
opts, err := gcs.getGrpcDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings())
opts, err := gcs.getGrpcDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings(), []ToClientConnOption{})
assert.NoError(t, err)
assert.Len(t, opts, 2)
}

func TestGrpcClientExtraOption(t *testing.T) {
tt, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

gcs := &ClientConfig{
TLSSetting: configtls.ClientConfig{
Insecure: true,
},
}
extraOpt := grpc.WithUserAgent("test-agent")
opts, err := gcs.getGrpcDialOptions(
context.Background(),
componenttest.NewNopHost(),
tt.TelemetrySettings(),
[]ToClientConnOption{WithGrpcDialOption(extraOpt)},
)
assert.NoError(t, err)
assert.Len(t, opts, 3)
assert.Equal(t, opts[2], extraOpt)
}

func TestAllGrpcClientSettings(t *testing.T) {
tt, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
Expand Down Expand Up @@ -231,7 +253,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
opts, err := test.settings.getGrpcDialOptions(context.Background(), test.host, tt.TelemetrySettings())
opts, err := test.settings.getGrpcDialOptions(context.Background(), test.host, tt.TelemetrySettings(), []ToClientConnOption{})
assert.NoError(t, err)
assert.Len(t, opts, 9)
})
Expand All @@ -244,11 +266,28 @@ func TestDefaultGrpcServerSettings(t *testing.T) {
Endpoint: "0.0.0.0:1234",
},
}
opts, err := gss.getGrpcServerOptions(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
opts, err := gss.getGrpcServerOptions(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings(), []ToServerOption{})
assert.NoError(t, err)
assert.Len(t, opts, 3)
}

func TestGrpcServerExtraOption(t *testing.T) {
gss := &ServerConfig{
NetAddr: confignet.AddrConfig{
Endpoint: "0.0.0.0:1234",
},
}
extraOpt := grpc.ConnectionTimeout(1_000_000_000)
opts, err := gss.getGrpcServerOptions(
componenttest.NewNopHost(),
componenttest.NewNopTelemetrySettings(),
[]ToServerOption{WithGrpcServerOption(extraOpt)},
)
assert.NoError(t, err)
assert.Len(t, opts, 4)
assert.Equal(t, opts[3], extraOpt)
}

func TestGrpcServerValidate(t *testing.T) {
tests := []struct {
gss *ServerConfig
Expand Down Expand Up @@ -329,7 +368,7 @@ func TestAllGrpcServerSettingsExceptAuth(t *testing.T) {
},
},
}
opts, err := gss.getGrpcServerOptions(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
opts, err := gss.getGrpcServerOptions(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings(), []ToServerOption{})
assert.NoError(t, err)
assert.Len(t, opts, 10)
}
Expand Down Expand Up @@ -488,7 +527,7 @@ func TestUseSecure(t *testing.T) {
TLSSetting: configtls.ClientConfig{},
Keepalive: nil,
}
dialOpts, err := gcs.getGrpcDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings())
dialOpts, err := gcs.getGrpcDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings(), []ToClientConnOption{})
assert.NoError(t, err)
assert.Len(t, dialOpts, 2)
}
Expand Down Expand Up @@ -540,7 +579,7 @@ func TestGRPCServerWarning(t *testing.T) {
logger, observed := observer.New(zap.DebugLevel)
set.Logger = zap.New(logger)

opts, err := test.settings.getGrpcServerOptions(componenttest.NewNopHost(), set)
opts, err := test.settings.getGrpcServerOptions(componenttest.NewNopHost(), set, []ToServerOption{})
require.NoError(t, err)
require.NotNil(t, opts)
_ = grpc.NewServer(opts...)
Expand Down

0 comments on commit 0ec83a3

Please sign in to comment.