Skip to content

[configgrpc] Add ClientConfig.EnhanceContextWithHeaders #12308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
WaitForReady bool `mapstructure:"wait_for_ready"`

// The headers associated with gRPC requests.
//
// The connection returned from ClientConfig.ToClientConn will not automatically set these headers.
// Make sure to use ClientConfig.EnhanceContextWithHeaders in your gRPC call.
Headers map[string]configopaque.String `mapstructure:"headers"`

// Sets the balancer in grpclb_policy to discover the servers. Default is pick_first.
Expand Down Expand Up @@ -348,6 +351,20 @@
return opts, nil
}

// EnhanceContextWithHeaders returns a new derived Context which, when passed into a gRPC call,
// will cause the gRPC library to append the configured headers to the request.
func (gcs *ClientConfig) EnhanceContextWithHeaders(ctx context.Context) context.Context {
if len(gcs.Headers) > 0 {
kv := make([]string, 0, 2*len(gcs.Headers))
for k, v := range gcs.Headers {
kv = append(kv, k)
kv = append(kv, string(v))
}
return metadata.AppendToOutgoingContext(ctx, kv...)

Check warning on line 363 in config/configgrpc/configgrpc.go

View check run for this annotation

Codecov / codecov/patch

config/configgrpc/configgrpc.go#L356-L363

Added lines #L356 - L363 were not covered by tests
}
return ctx

Check warning on line 365 in config/configgrpc/configgrpc.go

View check run for this annotation

Codecov / codecov/patch

config/configgrpc/configgrpc.go#L365

Added line #L365 was not covered by tests
}

func (gss *ServerConfig) Validate() error {
if gss.MaxRecvMsgSizeMiB*1024*1024 < 0 {
return fmt.Errorf("invalid max_recv_msg_size_mib value, must be between 1 and %d: %d", math.MaxInt/1024/1024, gss.MaxRecvMsgSizeMiB)
Expand Down
12 changes: 1 addition & 11 deletions exporter/otlpexporter/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -41,7 +40,6 @@ type baseExporter struct {
logExporter plogotlp.GRPCClient
profileExporter pprofileotlp.GRPCClient
clientConn *grpc.ClientConn
metadata metadata.MD
callOptions []grpc.CallOption

settings component.TelemetrySettings
Expand Down Expand Up @@ -70,11 +68,6 @@ func (e *baseExporter) start(ctx context.Context, host component.Host) (err erro
e.metricExporter = pmetricotlp.NewGRPCClient(e.clientConn)
e.logExporter = plogotlp.NewGRPCClient(e.clientConn)
e.profileExporter = pprofileotlp.NewGRPCClient(e.clientConn)
headers := map[string]string{}
for k, v := range e.config.ClientConfig.Headers {
headers[k] = string(v)
}
e.metadata = metadata.New(headers)
e.callOptions = []grpc.CallOption{
grpc.WaitForReady(e.config.ClientConfig.WaitForReady),
}
Expand Down Expand Up @@ -154,10 +147,7 @@ func (e *baseExporter) pushProfiles(ctx context.Context, td pprofile.Profiles) e
}

func (e *baseExporter) enhanceContext(ctx context.Context) context.Context {
if e.metadata.Len() > 0 {
return metadata.NewOutgoingContext(ctx, e.metadata)
}
return ctx
return e.config.ClientConfig.EnhanceContextWithHeaders(ctx)
}

func processError(err error) error {
Expand Down
Loading