From 2fa6df8ad7488db15daf9296f228487fdea78a52 Mon Sep 17 00:00:00 2001 From: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Fri, 1 Mar 2024 17:18:35 -0600 Subject: [PATCH] [configgrpc] Add `ToServerContext` (#9624) Adds a new function, `ToServerContext` which does what `ToServer` does, but takes a `context.Context`. After the next release we'll deprecate `ToServerContext` and rename it to `ToServer`. Related to https://github.com/open-telemetry/opentelemetry-collector/issues/9490 --------- Co-authored-by: Dmitrii Anoshin --- .chloggen/configgrpc-add-context.yaml | 25 +++++++++++++++++++++++++ config/configgrpc/configgrpc.go | 7 +++++++ config/configgrpc/configgrpc_test.go | 27 +++++++++++++++++++++++---- receiver/otlpreceiver/otlp.go | 2 +- 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100755 .chloggen/configgrpc-add-context.yaml diff --git a/.chloggen/configgrpc-add-context.yaml b/.chloggen/configgrpc-add-context.yaml new file mode 100755 index 00000000000..2b378dda837 --- /dev/null +++ b/.chloggen/configgrpc-add-context.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: deprecation + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: configgrpc + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Deprecates `ToServer`. Use `ToServerContext` instead. + +# One or more tracking issues or pull requests related to the change +issues: [9624] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] diff --git a/config/configgrpc/configgrpc.go b/config/configgrpc/configgrpc.go index 16ee9413104..18526262317 100644 --- a/config/configgrpc/configgrpc.go +++ b/config/configgrpc/configgrpc.go @@ -267,7 +267,14 @@ func validateBalancerName(balancerName string) bool { return balancer.Get(balancerName) != nil } +// ToServer returns a grpc.Server for the configuration +// Deprecated: [0.96.0] Use ToServerContext instead. func (gss *ServerConfig) ToServer(host component.Host, settings component.TelemetrySettings, extraOpts ...grpc.ServerOption) (*grpc.Server, error) { + return gss.ToServerContext(context.Background(), host, settings, extraOpts...) +} + +// ToServerContext returns a grpc.Server for the configuration +func (gss *ServerConfig) ToServerContext(_ context.Context, host component.Host, settings component.TelemetrySettings, extraOpts ...grpc.ServerOption) (*grpc.Server, error) { opts, err := gss.toServerOption(host, settings) if err != nil { return nil, err diff --git a/config/configgrpc/configgrpc_test.go b/config/configgrpc/configgrpc_test.go index da1c68f2bcf..7d646af2b3e 100644 --- a/config/configgrpc/configgrpc_test.go +++ b/config/configgrpc/configgrpc_test.go @@ -224,6 +224,25 @@ func TestAllGrpcServerSettingsExceptAuth(t *testing.T) { } func TestGrpcServerAuthSettings(t *testing.T) { + gss := &ServerConfig{ + NetAddr: confignet.AddrConfig{ + Endpoint: "0.0.0.0:1234", + }, + } + gss.Auth = &configauth.Authentication{ + AuthenticatorID: mockID, + } + host := &mockHost{ + ext: map[component.ID]component.Component{ + mockID: auth.NewServer(), + }, + } + srv, err := gss.ToServerContext(context.Background(), host, componenttest.NewNopTelemetrySettings()) + assert.NoError(t, err) + assert.NotNil(t, srv) +} + +func TestGrpcServerAuthSettings_Deprecated(t *testing.T) { gss := &ServerConfig{ NetAddr: confignet.AddrConfig{ Endpoint: "0.0.0.0:1234", @@ -481,7 +500,7 @@ func TestGRPCServerSettingsError(t *testing.T) { } for _, test := range tests { t.Run(test.err, func(t *testing.T) { - _, err := test.settings.ToServer(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings()) + _, err := test.settings.ToServerContext(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings()) assert.Regexp(t, test.err, err) }) } @@ -618,7 +637,7 @@ func TestHttpReception(t *testing.T) { } ln, err := gss.NetAddr.Listen(context.Background()) assert.NoError(t, err) - s, err := gss.ToServer(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings()) + s, err := gss.ToServerContext(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings()) assert.NoError(t, err) ptraceotlp.RegisterGRPCServer(s, &grpcTraceServer{}) @@ -665,7 +684,7 @@ func TestReceiveOnUnixDomainSocket(t *testing.T) { } ln, err := gss.NetAddr.Listen(context.Background()) assert.NoError(t, err) - srv, err := gss.ToServer(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings()) + srv, err := gss.ToServerContext(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings()) assert.NoError(t, err) ptraceotlp.RegisterGRPCServer(srv, &grpcTraceServer{}) @@ -859,7 +878,7 @@ func TestClientInfoInterceptors(t *testing.T) { Transport: "tcp", }, } - srv, err := gss.ToServer(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings()) + srv, err := gss.ToServerContext(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings()) require.NoError(t, err) ptraceotlp.RegisterGRPCServer(srv, mock) diff --git a/receiver/otlpreceiver/otlp.go b/receiver/otlpreceiver/otlp.go index 9a32f36d7a5..91936bfb6a5 100644 --- a/receiver/otlpreceiver/otlp.go +++ b/receiver/otlpreceiver/otlp.go @@ -83,7 +83,7 @@ func (r *otlpReceiver) startGRPCServer(host component.Host) error { } var err error - if r.serverGRPC, err = r.cfg.GRPC.ToServer(host, r.settings.TelemetrySettings); err != nil { + if r.serverGRPC, err = r.cfg.GRPC.ToServerContext(context.Background(), host, r.settings.TelemetrySettings); err != nil { return err }