From 30c4a9a33038bfc63349d509ceb5c74590331478 Mon Sep 17 00:00:00 2001 From: pree-dew Date: Thu, 24 Oct 2024 13:03:39 +0530 Subject: [PATCH] Allow additional context to be added when WithHeaders is used in OTLP gRPC traces exporter (#5915) Fix https://github.com/open-telemetry/opentelemetry-go/issues/5904 --- CHANGELOG.md | 1 + exporters/otlp/otlptrace/otlptracegrpc/client.go | 7 ++++++- exporters/otlp/otlptrace/otlptracegrpc/client_test.go | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af1218c61ee..99caa347000 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/exporters/otlp/otlptrace/otlptracegrpc/client.go b/exporters/otlp/otlptrace/otlptracegrpc/client.go index 3993df927de..2171bee3c84 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/client.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/client.go @@ -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. diff --git a/exporters/otlp/otlptrace/otlptracegrpc/client_test.go b/exporters/otlp/otlptrace/otlptracegrpc/client_test.go index 99f1a0cd860..11cff1494a9 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/client_test.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/client_test.go @@ -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" @@ -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)) }) @@ -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]) }