From 12d84e0c773de3dd7fa60bfcd0e2d15c4ece6b6f Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Wed, 8 Jan 2025 01:37:58 +0800 Subject: [PATCH 1/3] Implementing `ExtendedTextMapGetter` in grpc-1.6 instrumentation Signed-off-by: xiepuhuan --- .../grpc/v1_6/GrpcRequestGetter.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java index 0d7cfe5e4837..6d4c3341872a 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java @@ -6,10 +6,12 @@ package io.opentelemetry.instrumentation.grpc.v1_6; import io.grpc.Metadata; -import io.opentelemetry.context.propagation.TextMapGetter; +import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter; +import java.util.Collections; +import java.util.Iterator; import javax.annotation.Nullable; -enum GrpcRequestGetter implements TextMapGetter { +enum GrpcRequestGetter implements ExtendedTextMapGetter { INSTANCE; @Override @@ -25,4 +27,19 @@ public String get(@Nullable GrpcRequest request, String key) { } return request.getMetadata().get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)); } + + @Override + public Iterator getAll(@Nullable GrpcRequest request, String key) { + if (request == null || request.getMetadata() == null) { + return Collections.emptyIterator(); + } + + Iterable values = + request.getMetadata().getAll(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)); + + if (values == null) { + return Collections.emptyIterator(); + } + return values.iterator(); + } } From e02b929bd19d470be490720f61d942c01d34b567 Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Wed, 8 Jan 2025 01:51:52 +0800 Subject: [PATCH 2/3] Use Static imports --- .../instrumentation/grpc/v1_6/GrpcRequestGetter.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java index 6d4c3341872a..95538c8ef926 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java @@ -5,9 +5,10 @@ package io.opentelemetry.instrumentation.grpc.v1_6; +import static java.util.Collections.emptyIterator; + import io.grpc.Metadata; import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter; -import java.util.Collections; import java.util.Iterator; import javax.annotation.Nullable; @@ -31,14 +32,14 @@ public String get(@Nullable GrpcRequest request, String key) { @Override public Iterator getAll(@Nullable GrpcRequest request, String key) { if (request == null || request.getMetadata() == null) { - return Collections.emptyIterator(); + return emptyIterator(); } Iterable values = request.getMetadata().getAll(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)); if (values == null) { - return Collections.emptyIterator(); + return emptyIterator(); } return values.iterator(); } From 3f1a3bc98cac83eb5436389e37348ea6dd8735a4 Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Wed, 8 Jan 2025 10:22:16 +0800 Subject: [PATCH 3/3] Remove null check --- .../instrumentation/grpc/v1_6/GrpcRequestGetter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java index 95538c8ef926..6ef3210579e9 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequestGetter.java @@ -31,7 +31,7 @@ public String get(@Nullable GrpcRequest request, String key) { @Override public Iterator getAll(@Nullable GrpcRequest request, String key) { - if (request == null || request.getMetadata() == null) { + if (request == null) { return emptyIterator(); }