Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
38 changes: 38 additions & 0 deletions .chloggen/no-host-in-configgrpc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: pkg/config/configgrpc

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Remove `component.Host` parameter of ToServer/ToClientConn

# One or more tracking issues or pull requests related to the change
issues: [13640]

# (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: |
The `host` parameter was only used to find the middleware extensions specified
under the `middlewares` configuration key.

The map of available extensions must now be passed explicitly with the
`With[Server/Client]Extensions` option.
If the option is not provided, no middlewares will be available for use.

For typical use cases (user-controlled configuration inside a Collector component)
where a real `host` was passed, it should be replaced by
`With[Server/Client]Extensions(host.GetExtensions())`.

For test or non-Collector use cases where a no-op host was passed,
the `host` parameter can simply be dropped.

# 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]
38 changes: 38 additions & 0 deletions .chloggen/no-host-in-confighttp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: pkg/config/confighttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Remove `component.Host` parameter of ToServer/ToClient

# One or more tracking issues or pull requests related to the change
issues: [13640]

# (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: |
The `host` parameter was only used to find the middleware extensions specified
under the `middlewares` configuration key.

The map of available extensions must now be passed explicitly with the
`With[Server/Client]Extensions` option.
If the option is not provided, no middlewares will be available for use.

For typical use cases (user-controlled configuration inside a Collector component)
where a real `host` was passed, it should be replaced by
`With[Server/Client]Extensions(host.GetExtensions())`.

For test or non-Collector use cases where a no-op host was passed,
the `host` parameter can simply be dropped.

# 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]
27 changes: 10 additions & 17 deletions config/configgrpc/client_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,8 @@ func TestClientMiddlewareOrdering(t *testing.T) {
},
}

// Create a test host with our mock extensions
host := &mockHost{ext: mockExt}

// Send a request using the client with middleware
resp, err := sendTestRequestWithHost(t, clientConfig, host)
resp, err := sendTestRequest(t, clientConfig, WithClientExtensions(mockExt))
require.NoError(t, err)
assert.NotNil(t, resp)

Expand All @@ -147,16 +144,14 @@ func TestClientMiddlewareOrdering(t *testing.T) {
// specifically related to middleware resolution and API calls.
func TestClientMiddlewareToClientErrors(t *testing.T) {
tests := []struct {
name string
host component.Host
config ClientConfig
errText string
name string
extensions map[component.ID]component.Component
config ClientConfig
errText string
}{
{
name: "extension_not_found",
host: &mockHost{
ext: map[component.ID]component.Component{},
},
name: "extension_not_found",
extensions: map[component.ID]component.Component{},
config: ClientConfig{
Endpoint: "localhost:1234",
TLS: configtls.ClientConfig{
Expand All @@ -172,10 +167,8 @@ func TestClientMiddlewareToClientErrors(t *testing.T) {
},
{
name: "get_client_options_fails",
host: &mockHost{
ext: map[component.ID]component.Component{
component.MustNewID("errormw"): extensionmiddlewaretest.NewErr(errors.New("get options failed")),
},
extensions: map[component.ID]component.Component{
component.MustNewID("errormw"): extensionmiddlewaretest.NewErr(errors.New("get options failed")),
},
config: ClientConfig{
Endpoint: "localhost:1234",
Expand All @@ -195,7 +188,7 @@ func TestClientMiddlewareToClientErrors(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Test creating the client with middleware errors
_, err := tc.config.ToClientConn(context.Background(), tc.host, componenttest.NewNopTelemetrySettings())
_, err := tc.config.ToClientConn(context.Background(), componenttest.NewNopTelemetrySettings(), WithClientExtensions(tc.extensions))
require.Error(t, err)
assert.Contains(t, err.Error(), tc.errText)
})
Expand Down
78 changes: 57 additions & 21 deletions config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,29 @@ func WithGrpcDialOption(opt grpc.DialOption) ToClientConnOption {
}
func (grpcDialOptionWrapper) isToClientConnOption() {}

type clientExtensionsOption struct {
extensions map[component.ID]component.Component
}

// WithClientExtensions is a [ToClientConnOption] which supplies the map of extensions to search for middlewares.
func WithClientExtensions(extensions map[component.ID]component.Component) ToClientConnOption {
return clientExtensionsOption{extensions: extensions}
}
func (clientExtensionsOption) isToClientConnOption() {}

// ToClientConn creates a client connection to the given target. By default, it's
// a non-blocking dial (the function won't wait for connections to be
// established, and connecting happens in the background). To make it a blocking
// dial, use the WithGrpcDialOption(grpc.WithBlock()) option.
//
// To allow the configuration to reference middleware extensions,
// use the [WithClientExtensions] option with the output of host.GetExtensions().
func (cc *ClientConfig) ToClientConn(
ctx context.Context,
host component.Host,
settings component.TelemetrySettings,
extraOpts ...ToClientConnOption,
) (*grpc.ClientConn, error) {
grpcOpts, err := cc.getGrpcDialOptions(ctx, host, settings, extraOpts)
grpcOpts, err := cc.getGrpcDialOptions(ctx, settings, extraOpts)
if err != nil {
return nil, err
}
Expand All @@ -299,10 +311,20 @@ func (cc *ClientConfig) addHeadersIfAbsent(ctx context.Context) context.Context

func (cc *ClientConfig) getGrpcDialOptions(
ctx context.Context,
host component.Host,
settings component.TelemetrySettings,
extraOpts []ToClientConnOption,
) ([]grpc.DialOption, error) {
var extraGrpcOpts []grpc.DialOption
var extensions map[component.ID]component.Component
for _, opt := range extraOpts {
switch opt := opt.(type) {
case grpcDialOptionWrapper:
extraGrpcOpts = append(extraGrpcOpts, opt.opt)
case clientExtensionsOption:
extensions = opt.extensions
}
}

var opts []grpc.DialOption
if cc.Compression.IsCompressed() {
cp, err := getGRPCCompressionName(cc.Compression)
Expand Down Expand Up @@ -343,11 +365,11 @@ func (cc *ClientConfig) getGrpcDialOptions(
}

if cc.Auth.HasValue() {
if host.GetExtensions() == nil {
if extensions == nil {
return nil, errors.New("no extensions configuration available")
}

grpcAuthenticator, cerr := cc.Auth.Get().GetGRPCClientAuthenticator(ctx, host.GetExtensions())
grpcAuthenticator, cerr := cc.Auth.Get().GetGRPCClientAuthenticator(ctx, extensions)
if cerr != nil {
return nil, cerr
}
Expand Down Expand Up @@ -389,18 +411,14 @@ func (cc *ClientConfig) getGrpcDialOptions(

// Apply middleware options. Note: OpenTelemetry could be registered as an extension.
for _, middleware := range cc.Middlewares {
middlewareOptions, err := middleware.GetGRPCClientOptions(ctx, host.GetExtensions())
middlewareOptions, err := middleware.GetGRPCClientOptions(ctx, extensions)
if err != nil {
return nil, fmt.Errorf("failed to get gRPC client options from middleware: %w", err)
}
opts = append(opts, middlewareOptions...)
}

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

return opts, nil
}
Expand Down Expand Up @@ -436,14 +454,26 @@ func WithGrpcServerOption(opt grpc.ServerOption) ToServerOption {
}
func (grpcServerOptionWrapper) isToServerOption() {}

type serverExtensionsOption struct {
extensions map[component.ID]component.Component
}

// WithServerExtensions is a [ToServerOption] which supplies the map of extensions to search for middlewares.
func WithServerExtensions(extensions map[component.ID]component.Component) ToServerOption {
return serverExtensionsOption{extensions: extensions}
}
func (serverExtensionsOption) isToServerOption() {}

// ToServer returns a [grpc.Server] for the configuration.
//
// To allow the configuration to reference middleware extensions,
// use the [WithServerExtensions] option with the output of host.GetExtensions().
func (sc *ServerConfig) ToServer(
ctx context.Context,
host component.Host,
settings component.TelemetrySettings,
extraOpts ...ToServerOption,
) (*grpc.Server, error) {
grpcOpts, err := sc.getGrpcServerOptions(ctx, host, settings, extraOpts)
grpcOpts, err := sc.getGrpcServerOptions(ctx, settings, extraOpts)
if err != nil {
return nil, err
}
Expand All @@ -452,10 +482,20 @@ func (sc *ServerConfig) ToServer(

func (sc *ServerConfig) getGrpcServerOptions(
ctx context.Context,
host component.Host,
settings component.TelemetrySettings,
extraOpts []ToServerOption,
) ([]grpc.ServerOption, error) {
var extraGrpcOpts []grpc.ServerOption
var extensions map[component.ID]component.Component
for _, opt := range extraOpts {
switch opt := opt.(type) {
case grpcServerOptionWrapper:
extraGrpcOpts = append(extraGrpcOpts, opt.opt)
case serverExtensionsOption:
extensions = opt.extensions
}
}

var opts []grpc.ServerOption

if sc.TLS.HasValue() {
Expand Down Expand Up @@ -515,7 +555,7 @@ func (sc *ServerConfig) getGrpcServerOptions(
var sInterceptors []grpc.StreamServerInterceptor

if sc.Auth.HasValue() {
authenticator, err := sc.Auth.Get().GetServerAuthenticator(ctx, host.GetExtensions())
authenticator, err := sc.Auth.Get().GetServerAuthenticator(ctx, extensions)
if err != nil {
return nil, err
}
Expand All @@ -539,18 +579,14 @@ func (sc *ServerConfig) getGrpcServerOptions(

// Apply middleware options. Note: OpenTelemetry could be registered as an extension.
for _, middleware := range sc.Middlewares {
middlewareOptions, err := middleware.GetGRPCServerOptions(ctx, host.GetExtensions())
middlewareOptions, err := middleware.GetGRPCServerOptions(ctx, extensions)
if err != nil {
return nil, fmt.Errorf("failed to get gRPC server options from middleware: %w", err)
}
opts = append(opts, middlewareOptions...)
}

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

return opts, nil
}
Expand Down
Loading
Loading