Skip to content

Commit

Permalink
[configgrpc] Add ToServerContext (#9624)
Browse files Browse the repository at this point in the history
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
#9490

---------

Co-authored-by: Dmitrii Anoshin <anoshindx@gmail.com>
  • Loading branch information
TylerHelmuth and dmitryax authored Mar 1, 2024
1 parent 6affda6 commit 2fa6df8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
25 changes: 25 additions & 0 deletions .chloggen/configgrpc-add-context.yaml
Original file line number Diff line number Diff line change
@@ -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]
7 changes: 7 additions & 0 deletions config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 23 additions & 4 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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{})

Expand Down Expand Up @@ -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{})

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion receiver/otlpreceiver/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 2fa6df8

Please sign in to comment.