From 615cb4e42906c2300cdfa92cf73ca93544d22184 Mon Sep 17 00:00:00 2001 From: mrasu Date: Thu, 17 Oct 2024 23:11:27 +0900 Subject: [PATCH 1/3] Keep metadata for gRPC in context --- exporters/otlp/otlpmetric/otlpmetricgrpc/client.go | 7 ++++++- exporters/otlp/otlpmetric/otlpmetricgrpc/client_test.go | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/client.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/client.go index 428cfea2334..e0fa0570a81 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/client.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/client.go @@ -155,7 +155,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) } return ctx, cancel diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/client_test.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/client_test.go index 3ae5ccd1519..54b99ae5007 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/client_test.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/client_test.go @@ -13,6 +13,7 @@ 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" "google.golang.org/protobuf/types/known/durationpb" @@ -207,7 +208,10 @@ func TestConfig(t *testing.T) { headers := map[string]string{key: "custom-value"} exp, coll := factoryFunc(nil, WithHeaders(headers)) t.Cleanup(coll.Shutdown) + ctx := context.Background() + additionalKey := "additional-custom-header" + ctx = metadata.AppendToOutgoingContext(ctx, additionalKey, "additional-value") require.NoError(t, exp.Export(ctx, &metricdata.ResourceMetrics{})) // Ensure everything is flushed. require.NoError(t, exp.Shutdown(ctx)) @@ -215,6 +219,7 @@ func TestConfig(t *testing.T) { got := coll.Headers() require.Regexp(t, "OTel Go OTLP over gRPC metrics exporter/[01]\\..*", got) require.Contains(t, got, key) + require.Contains(t, got, additionalKey) assert.Equal(t, []string{headers[key]}, got[key]) }) From 8fbd3f9a127945ac5bb7e616a991f0023168a60f Mon Sep 17 00:00:00 2001 From: mrasu Date: Thu, 17 Oct 2024 23:41:57 +0900 Subject: [PATCH 2/3] add changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bdb30ce4f8..2e0e4ff466b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add `go.opentelemetry.io/otel/sdk/metric/exemplar.AlwaysOffFilter`, which can be used to disable exemplar recording. (#5850) - Add `go.opentelemetry.io/otel/sdk/metric.WithExemplarFilter`, which can be used to configure the exemplar filter used by the metrics SDK. (#5850) +### Changed + +- `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc` now keeps the metadata in the context. (#5892) + From 0486b173d77534292f3901952d745c3df714e355 Mon Sep 17 00:00:00 2001 From: mrasu Date: Fri, 18 Oct 2024 00:41:14 +0900 Subject: [PATCH 3/3] Update CHANGELOG.md Co-authored-by: Tyler Yahn --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e0e4ff466b..2a2819b3888 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Changed -- `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc` now keeps the metadata in the context. (#5892) +- `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc` now keeps the metadata already present in the context when `WithHeaders` is used. (#5892)