From a47c406e57b56f4469d3f8af97ad43e61a4f014a Mon Sep 17 00:00:00 2001 From: wgy035 <44359735+wgy035@users.noreply.github.com> Date: Wed, 5 Jun 2024 05:27:22 +0800 Subject: [PATCH] fix grpc set metadata multi-value (#11308) Co-authored-by: Lauri Tulmin --- .../grpc/v1_6/MetadataSetter.java | 4 +- .../grpc/v1_6/MetadataSetterTest.java | 67 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/MetadataSetterTest.java diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/MetadataSetter.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/MetadataSetter.java index 99a50d9a151c..e79c4bb86907 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/MetadataSetter.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/MetadataSetter.java @@ -13,6 +13,8 @@ enum MetadataSetter implements TextMapSetter { @Override public void set(Metadata carrier, String key, String value) { - carrier.put(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER), value); + Metadata.Key metadataKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER); + carrier.removeAll(metadataKey); + carrier.put(metadataKey, value); } } diff --git a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/MetadataSetterTest.java b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/MetadataSetterTest.java new file mode 100644 index 000000000000..f138f4db0a4e --- /dev/null +++ b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/MetadataSetterTest.java @@ -0,0 +1,67 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.grpc.v1_6; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.grpc.Metadata; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanContext; +import io.opentelemetry.api.trace.SpanId; +import io.opentelemetry.api.trace.TraceFlags; +import io.opentelemetry.api.trace.TraceId; +import io.opentelemetry.api.trace.TraceState; +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.propagation.TextMapGetter; +import io.opentelemetry.context.propagation.TextMapPropagator; +import javax.annotation.Nullable; +import org.junit.jupiter.api.Test; + +class MetadataSetterTest { + + @Test + void overwriteTracingHeader() { + Metadata metadata = new Metadata(); + TextMapPropagator propagator = W3CTraceContextPropagator.getInstance(); + for (int i = 1; i <= 2; i++) { + Context context = + Context.root() + .with( + Span.wrap( + SpanContext.create( + TraceId.fromLongs(0, i), + SpanId.fromLong(i), + TraceFlags.getDefault(), + TraceState.getDefault()))); + propagator.inject(context, metadata, MetadataSetter.INSTANCE); + } + + assertThat(metadata.getAll(Metadata.Key.of("traceparent", Metadata.ASCII_STRING_MARSHALLER))) + .hasSize(1); + + Context context = + propagator.extract( + Context.root(), + metadata, + new TextMapGetter() { + @Override + public Iterable keys(Metadata metadata) { + return metadata.keys(); + } + + @Nullable + @Override + public String get(@Nullable Metadata metadata, String key) { + return metadata.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)); + } + }); + + SpanContext spanContext = Span.fromContext(context).getSpanContext(); + assertThat(spanContext.getTraceId()).isEqualTo(TraceId.fromLongs(0, 2)); + assertThat(spanContext.getSpanId()).isEqualTo(SpanId.fromLong(2)); + } +}