Skip to content

Commit

Permalink
Allow additional context to be added when WithHeaders is used in OTLP…
Browse files Browse the repository at this point in the history
… gRPC traces exporter (#5915)

Fix #5904
  • Loading branch information
pree-dew authored Oct 24, 2024
1 parent 3429e15 commit 30c4a9a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc` now keeps the metadata already present in the context when `WithHeaders` is used. (#5892)
- `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc` now keeps the metadata already present in the context when `WithHeaders` is used. (#5911)
- `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc` now keeps the metadata already present in the context when `WithHeaders` is used. (#5915)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
7 changes: 6 additions & 1 deletion exporters/otlp/otlptrace/otlptracegrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@ func (c *client) exportContext(parent context.Context) (context.Context, context
}

if c.metadata.Len() > 0 {
ctx = metadata.NewOutgoingContext(ctx, c.metadata)
md := c.metadata
if outMD, ok := metadata.FromOutgoingContext(ctx); ok {
md = metadata.Join(md, outMD)
}

ctx = metadata.NewOutgoingContext(ctx, md)
}

// Unify the client stopCtx with the parent.
Expand Down
4 changes: 4 additions & 0 deletions exporters/otlp/otlptrace/otlptracegrpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/encoding/gzip"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -216,6 +217,8 @@ func TestNewWithHeaders(t *testing.T) {
t.Cleanup(func() { require.NoError(t, mc.stop()) })

ctx := context.Background()
additionalKey := "additional-custom-header"
ctx = metadata.AppendToOutgoingContext(ctx, additionalKey, "additional-value")
exp := newGRPCExporter(t, ctx, mc.endpoint,
otlptracegrpc.WithHeaders(map[string]string{"header1": "value1"}))
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) })
Expand All @@ -224,6 +227,7 @@ func TestNewWithHeaders(t *testing.T) {
headers := mc.getHeaders()
require.Regexp(t, "OTel OTLP Exporter Go/1\\..*", headers.Get("user-agent"))
require.Len(t, headers.Get("header1"), 1)
require.Len(t, headers.Get(additionalKey), 1)
assert.Equal(t, "value1", headers.Get("header1")[0])
}

Expand Down

0 comments on commit 30c4a9a

Please sign in to comment.