From c6f544b0e74f7045ac3179d992c5a6733cbabb93 Mon Sep 17 00:00:00 2001 From: Tav Date: Mon, 31 Oct 2022 15:46:00 +0200 Subject: [PATCH 01/43] Added gRPC request instrumentation --- .../grpc/v1_6/GrpcSingletons.java | 3 + .../grpc/v1_6/GrpcAttributesExtractor.java | 33 +++++++- .../grpc/v1_6/GrpcRpcAttributesGetter.java | 26 +++++++ .../grpc/v1_6/GrpcTelemetryBuilder.java | 14 +++- .../instrumentation/grpc/v1_6/GrpcTest.java | 78 +++++++++++++++++++ .../bootstrap/internal/CommonConfig.java | 12 ++- 6 files changed, 159 insertions(+), 7 deletions(-) diff --git a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java index 5896459c9584..342c58d157fd 100644 --- a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java +++ b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java @@ -11,6 +11,7 @@ import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.instrumentation.grpc.v1_6.GrpcTelemetry; import io.opentelemetry.instrumentation.grpc.v1_6.internal.ContextStorageBridge; +import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig; import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig; // Holds singleton references. @@ -27,9 +28,11 @@ public final class GrpcSingletons { InstrumentationConfig.get() .getBoolean("otel.instrumentation.grpc.experimental-span-attributes", false); + GrpcTelemetry telemetry = GrpcTelemetry.builder(GlobalOpenTelemetry.get()) .setCaptureExperimentalSpanAttributes(experimentalSpanAttributes) + .setRequestMetadataValuesToCapture(CommonConfig.get().getGrpcRequestMetadata()) .build(); CLIENT_INTERCEPTOR = telemetry.newClientInterceptor(); diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index 6de0bdd3baad..e83a56a88539 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -6,17 +6,34 @@ package io.opentelemetry.instrumentation.grpc.v1_6; import io.grpc.Status; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import javax.annotation.Nullable; +import java.util.List; + +import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; + final class GrpcAttributesExtractor implements AttributesExtractor { + // TODO: This should be added to package io.opentelemetry.semconv.trace.attributes in the next release + public static final String GRPC_METADATA_ATTRIBUTE_VALUE_PREFIX = "rpc.request.metadata"; + + private final GrpcRpcAttributesGetter getter; + private final List requestMetadataValuesToCapture; + + GrpcAttributesExtractor( + GrpcRpcAttributesGetter getter, + List requestMetadataValuesToCapture) { + this.getter = getter; + this.requestMetadataValuesToCapture = requestMetadataValuesToCapture; + } + @Override - public void onStart( - AttributesBuilder attributes, Context parentContext, GrpcRequest grpcRequest) { - // No request attributes + public void onStart(AttributesBuilder attributes, Context parentContext, GrpcRequest grpcRequest) { + // Request attributes captured on request end (onEnd) } @Override @@ -29,5 +46,15 @@ public void onEnd( if (status != null) { attributes.put(SemanticAttributes.RPC_GRPC_STATUS_CODE, status.getCode().value()); } + + if (requestMetadataValuesToCapture != null) { + for (String key : requestMetadataValuesToCapture) { + internalSet( + attributes, + AttributeKey.stringArrayKey(GRPC_METADATA_ATTRIBUTE_VALUE_PREFIX + "." + key), + getter.metadataValue(request, key) + ); + } + } } } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java index 4f1087ae83f2..24229e063716 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java @@ -5,8 +5,12 @@ package io.opentelemetry.instrumentation.grpc.v1_6; +import io.grpc.Metadata; import io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcAttributesGetter; import javax.annotation.Nullable; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; enum GrpcRpcAttributesGetter implements RpcAttributesGetter { INSTANCE; @@ -37,4 +41,26 @@ public String method(GrpcRequest request) { } return fullMethodName.substring(slashIndex + 1); } + + @Nullable + public List metadataValue(GrpcRequest request, String key) { + if (request.getMetadata() == null) { + return null; + } + + if (key == null || key.isEmpty()) { + return null; + } + + Iterable values = request.getMetadata().getAll( + Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER) + ); + + if (values == null) { + return null; + } + + return StreamSupport.stream(values.spliterator(), false) + .collect(Collectors.toList()); + } } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java index 1ce44d1dbc44..0bd6a02b7b99 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java @@ -50,6 +50,8 @@ public final class GrpcTelemetryBuilder { additionalClientExtractors = new ArrayList<>(); private boolean captureExperimentalSpanAttributes; + private List requestMetadataValuesToCapture; + GrpcTelemetryBuilder(OpenTelemetry openTelemetry) { this.openTelemetry = openTelemetry; @@ -115,6 +117,14 @@ public GrpcTelemetryBuilder setCaptureExperimentalSpanAttributes( return this; } + /** Sets which metadata request values should be captured as span attributes. */ + @CanIgnoreReturnValue + public GrpcTelemetryBuilder setRequestMetadataValuesToCapture( + List requestMetadataValuesToCapture) { + this.requestMetadataValuesToCapture = requestMetadataValuesToCapture; + return this; + } + /** Returns a new {@link GrpcTelemetry} with the settings of this {@link GrpcTelemetryBuilder}. */ public GrpcTelemetry build() { SpanNameExtractor originalSpanNameExtractor = new GrpcSpanNameExtractor(); @@ -139,7 +149,9 @@ public GrpcTelemetry build() { instrumenter -> instrumenter .setSpanStatusExtractor(new GrpcSpanStatusExtractor()) - .addAttributesExtractor(new GrpcAttributesExtractor()) + .addAttributesExtractor( + new GrpcAttributesExtractor( + GrpcRpcAttributesGetter.INSTANCE, requestMetadataValuesToCapture)) .addAttributesExtractors(additionalExtractors)); GrpcNetClientAttributesGetter netClientAttributesGetter = new GrpcNetClientAttributesGetter(); diff --git a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java index cada74856619..fbdd1de31bc6 100644 --- a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java +++ b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java @@ -24,6 +24,8 @@ import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions; +import java.util.Collections; +import java.util.List; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; import org.junit.jupiter.api.Test; @@ -35,6 +37,7 @@ class GrpcTest extends AbstractGrpcTest { static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); private static final AttributeKey CUSTOM_KEY = AttributeKey.stringKey("customKey"); + private static final String METADATA_ATTRIBUTE_PREFIX = "rpc.request.metadata."; private static final Metadata.Key CUSTOM_METADATA_KEY = Metadata.Key.of("customMetadataKey", Metadata.ASCII_STRING_MARSHALLER); @@ -56,6 +59,81 @@ protected InstrumentationExtension testing() { return testing; } + @Test + void grpcAttributesExtractor() throws Exception { + String metadataKey = "some-key"; + AttributeKey> attributeKey = AttributeKey.stringArrayKey(METADATA_ATTRIBUTE_PREFIX + metadataKey); + String metadataValue = "some-value"; + List metadataValueAsList = Collections.singletonList("some-value"); + + BindableService greeter = + new GreeterGrpc.GreeterImplBase() { + @Override + public void sayHello( + Helloworld.Request req, StreamObserver responseObserver) { + Helloworld.Response reply = + Helloworld.Response.newBuilder().setMessage("Hello " + req.getName()).build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } + }; + + GrpcAttributesExtractor grpcAttributesExtractor = new GrpcAttributesExtractor( + GrpcRpcAttributesGetter.INSTANCE, + Collections.singletonList(metadataKey)); + + Server server = + ServerBuilder.forPort(0) + .addService(greeter) + .intercept( + GrpcTelemetry.builder(testing.getOpenTelemetry()) + .addAttributeExtractor(grpcAttributesExtractor) + .build() + .newServerInterceptor()) + .build() + .start(); + + ManagedChannel channel = + createChannel( + ManagedChannelBuilder.forAddress("localhost", server.getPort()) + .intercept( + GrpcTelemetry.builder(testing.getOpenTelemetry()) + .addAttributeExtractor(grpcAttributesExtractor) + .build() + .newClientInterceptor())); + + Metadata extraMetadata = new Metadata(); + extraMetadata.put(Metadata.Key.of(metadataKey, Metadata.ASCII_STRING_MARSHALLER), metadataValue); + + GreeterGrpc.GreeterBlockingStub client = + GreeterGrpc.newBlockingStub(channel) + .withInterceptors(MetadataUtils.newAttachHeadersInterceptor(extraMetadata)); + + Helloworld.Response response = + testing() + .runWithSpan( + "parent", + () -> client.sayHello(Helloworld.Request.newBuilder().setName("test").build())); + + OpenTelemetryAssertions.assertThat(response.getMessage()).isEqualTo("Hello test"); + + testing() + .waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName("example.Greeter/SayHello") + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttribute(attributeKey, metadataValueAsList), + span -> + span.hasName("example.Greeter/SayHello") + .hasKind(SpanKind.SERVER) + .hasParent(trace.getSpan(1)) + .hasAttribute(attributeKey, metadataValueAsList))); + } + @Test void metadataProvided() throws Exception { BindableService greeter = diff --git a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java index 7072f64809c9..731cff65a141 100644 --- a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java +++ b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java @@ -28,6 +28,7 @@ public static CommonConfig get() { private final List clientResponseHeaders; private final List serverRequestHeaders; private final List serverResponseHeaders; + private final List grpcRequestMetadata; private final boolean statementSanitizationEnabled; CommonConfig(InstrumentationConfig config) { @@ -41,17 +42,18 @@ public static CommonConfig get() { config.getList("otel.instrumentation.http.capture-headers.server.request", emptyList()); serverResponseHeaders = config.getList("otel.instrumentation.http.capture-headers.server.response", emptyList()); + grpcRequestMetadata = + config.getList("otel.instrumentation.grpc.capture-metadata.request", emptyList()); statementSanitizationEnabled = config.getBoolean("otel.instrumentation.common.db-statement-sanitizer.enabled", true); + } public Map getPeerServiceMapping() { return peerServiceMapping; } - public List getClientRequestHeaders() { - return clientRequestHeaders; - } + public List getClientRequestHeaders() { return clientRequestHeaders; } public List getClientResponseHeaders() { return clientResponseHeaders; @@ -65,6 +67,10 @@ public List getServerResponseHeaders() { return serverResponseHeaders; } + public List getGrpcRequestMetadata() { + return grpcRequestMetadata; + } + public boolean isStatementSanitizationEnabled() { return statementSanitizationEnabled; } From 7830c0f77e03f9779efa15c0d066e1d36dc16efa Mon Sep 17 00:00:00 2001 From: Tav Date: Mon, 31 Oct 2022 17:33:10 +0200 Subject: [PATCH 02/43] spotless --- .../javaagent/bootstrap/internal/CommonConfig.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java index 731cff65a141..3438237bb778 100644 --- a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java +++ b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java @@ -46,14 +46,15 @@ public static CommonConfig get() { config.getList("otel.instrumentation.grpc.capture-metadata.request", emptyList()); statementSanitizationEnabled = config.getBoolean("otel.instrumentation.common.db-statement-sanitizer.enabled", true); - } public Map getPeerServiceMapping() { return peerServiceMapping; } - public List getClientRequestHeaders() { return clientRequestHeaders; } + public List getClientRequestHeaders() { + return clientRequestHeaders; + } public List getClientResponseHeaders() { return clientResponseHeaders; From 045a6169de24100c02e064ec24d1b5d2110c850b Mon Sep 17 00:00:00 2001 From: Tav Date: Mon, 31 Oct 2022 17:39:39 +0200 Subject: [PATCH 03/43] applied spotless to grpc-1.6:javaagent --- .../javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java | 1 - 1 file changed, 1 deletion(-) diff --git a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java index 342c58d157fd..a2ba6d36b4ec 100644 --- a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java +++ b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java @@ -28,7 +28,6 @@ public final class GrpcSingletons { InstrumentationConfig.get() .getBoolean("otel.instrumentation.grpc.experimental-span-attributes", false); - GrpcTelemetry telemetry = GrpcTelemetry.builder(GlobalOpenTelemetry.get()) .setCaptureExperimentalSpanAttributes(experimentalSpanAttributes) From 33f2232812126dc6120e26293c87c86418a668da Mon Sep 17 00:00:00 2001 From: Tav Date: Mon, 31 Oct 2022 17:43:06 +0200 Subject: [PATCH 04/43] applied spotless to grpc-1.6:library --- .../grpc/v1_6/GrpcAttributesExtractor.java | 19 +++++++++---------- .../grpc/v1_6/GrpcRpcAttributesGetter.java | 10 ++++------ .../grpc/v1_6/GrpcTelemetryBuilder.java | 1 - .../instrumentation/grpc/v1_6/GrpcTest.java | 12 +++++++----- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index e83a56a88539..051efe254049 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -5,34 +5,34 @@ package io.opentelemetry.instrumentation.grpc.v1_6; +import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; + import io.grpc.Status; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; -import javax.annotation.Nullable; - import java.util.List; - -import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; +import javax.annotation.Nullable; final class GrpcAttributesExtractor implements AttributesExtractor { - // TODO: This should be added to package io.opentelemetry.semconv.trace.attributes in the next release + // TODO: This should be added to package io.opentelemetry.semconv.trace.attributes in the next + // release public static final String GRPC_METADATA_ATTRIBUTE_VALUE_PREFIX = "rpc.request.metadata"; private final GrpcRpcAttributesGetter getter; private final List requestMetadataValuesToCapture; GrpcAttributesExtractor( - GrpcRpcAttributesGetter getter, - List requestMetadataValuesToCapture) { + GrpcRpcAttributesGetter getter, List requestMetadataValuesToCapture) { this.getter = getter; this.requestMetadataValuesToCapture = requestMetadataValuesToCapture; } @Override - public void onStart(AttributesBuilder attributes, Context parentContext, GrpcRequest grpcRequest) { + public void onStart( + AttributesBuilder attributes, Context parentContext, GrpcRequest grpcRequest) { // Request attributes captured on request end (onEnd) } @@ -52,8 +52,7 @@ public void onEnd( internalSet( attributes, AttributeKey.stringArrayKey(GRPC_METADATA_ATTRIBUTE_VALUE_PREFIX + "." + key), - getter.metadataValue(request, key) - ); + getter.metadataValue(request, key)); } } } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java index 24229e063716..ae4fae729ea2 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java @@ -7,10 +7,10 @@ import io.grpc.Metadata; import io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcAttributesGetter; -import javax.annotation.Nullable; import java.util.List; import java.util.stream.Collectors; import java.util.stream.StreamSupport; +import javax.annotation.Nullable; enum GrpcRpcAttributesGetter implements RpcAttributesGetter { INSTANCE; @@ -52,15 +52,13 @@ public List metadataValue(GrpcRequest request, String key) { return null; } - Iterable values = request.getMetadata().getAll( - Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER) - ); + Iterable values = + request.getMetadata().getAll(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)); if (values == null) { return null; } - return StreamSupport.stream(values.spliterator(), false) - .collect(Collectors.toList()); + return StreamSupport.stream(values.spliterator(), false).collect(Collectors.toList()); } } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java index 0bd6a02b7b99..795cbf0aa9bf 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java @@ -52,7 +52,6 @@ public final class GrpcTelemetryBuilder { private boolean captureExperimentalSpanAttributes; private List requestMetadataValuesToCapture; - GrpcTelemetryBuilder(OpenTelemetry openTelemetry) { this.openTelemetry = openTelemetry; } diff --git a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java index fbdd1de31bc6..cb6c7d9832ea 100644 --- a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java +++ b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java @@ -62,7 +62,8 @@ protected InstrumentationExtension testing() { @Test void grpcAttributesExtractor() throws Exception { String metadataKey = "some-key"; - AttributeKey> attributeKey = AttributeKey.stringArrayKey(METADATA_ATTRIBUTE_PREFIX + metadataKey); + AttributeKey> attributeKey = + AttributeKey.stringArrayKey(METADATA_ATTRIBUTE_PREFIX + metadataKey); String metadataValue = "some-value"; List metadataValueAsList = Collections.singletonList("some-value"); @@ -78,9 +79,9 @@ public void sayHello( } }; - GrpcAttributesExtractor grpcAttributesExtractor = new GrpcAttributesExtractor( - GrpcRpcAttributesGetter.INSTANCE, - Collections.singletonList(metadataKey)); + GrpcAttributesExtractor grpcAttributesExtractor = + new GrpcAttributesExtractor( + GrpcRpcAttributesGetter.INSTANCE, Collections.singletonList(metadataKey)); Server server = ServerBuilder.forPort(0) @@ -103,7 +104,8 @@ public void sayHello( .newClientInterceptor())); Metadata extraMetadata = new Metadata(); - extraMetadata.put(Metadata.Key.of(metadataKey, Metadata.ASCII_STRING_MARSHALLER), metadataValue); + extraMetadata.put( + Metadata.Key.of(metadataKey, Metadata.ASCII_STRING_MARSHALLER), metadataValue); GreeterGrpc.GreeterBlockingStub client = GreeterGrpc.newBlockingStub(channel) From bc79e30a811bad95b8f6b414fac0c6e57fe925f7 Mon Sep 17 00:00:00 2001 From: Tavh <44731477+Tavh@users.noreply.github.com> Date: Tue, 1 Nov 2022 15:58:53 +0200 Subject: [PATCH 05/43] Changed GRPC_METADATA_ATTRIBUTE_VALUE_PREFIX to RPC_REQUEST_METADATA_ATTRIBUTE_VALUE_PREFIX --- .../grpc/v1_6/GrpcAttributesExtractor.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index 051efe254049..df722ddec523 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -17,9 +17,9 @@ import javax.annotation.Nullable; final class GrpcAttributesExtractor implements AttributesExtractor { - // TODO: This should be added to package io.opentelemetry.semconv.trace.attributes in the next - // release - public static final String GRPC_METADATA_ATTRIBUTE_VALUE_PREFIX = "rpc.request.metadata"; + // TODO: This should be added to + // io.opentelemetry.semconv.trace.attributes.SemanticAttributes when possible + public static final String RPC_REQUEST_METADATA_ATTRIBUTE_VALUE_PREFIX = "rpc.request.metadata"; private final GrpcRpcAttributesGetter getter; private final List requestMetadataValuesToCapture; @@ -51,7 +51,7 @@ public void onEnd( for (String key : requestMetadataValuesToCapture) { internalSet( attributes, - AttributeKey.stringArrayKey(GRPC_METADATA_ATTRIBUTE_VALUE_PREFIX + "." + key), + AttributeKey.stringArrayKey(RPC_REQUEST_METADATA_ATTRIBUTE_VALUE_PREFIX + "." + key), getter.metadataValue(request, key)); } } From 4c909872d059f0b0e24fe7f2d92ab3fd80f60724 Mon Sep 17 00:00:00 2001 From: Tavh <44731477+Tavh@users.noreply.github.com> Date: Tue, 1 Nov 2022 16:11:31 +0200 Subject: [PATCH 06/43] grpcRequestMetadata -> rpcRequestMetadata --- .../javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java index a2ba6d36b4ec..501ace16bc62 100644 --- a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java +++ b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java @@ -31,7 +31,7 @@ public final class GrpcSingletons { GrpcTelemetry telemetry = GrpcTelemetry.builder(GlobalOpenTelemetry.get()) .setCaptureExperimentalSpanAttributes(experimentalSpanAttributes) - .setRequestMetadataValuesToCapture(CommonConfig.get().getGrpcRequestMetadata()) + .setRequestMetadataValuesToCapture(CommonConfig.get().getRpcRequestMetadata()) .build(); CLIENT_INTERCEPTOR = telemetry.newClientInterceptor(); From 5c302bb0aa2e31e952365bc48a4d3853d5b044ed Mon Sep 17 00:00:00 2001 From: Tavh <44731477+Tavh@users.noreply.github.com> Date: Tue, 1 Nov 2022 16:11:48 +0200 Subject: [PATCH 07/43] grpcRequestMetadata -> rpcRequestMetadata --- .../javaagent/bootstrap/internal/CommonConfig.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java index 3438237bb778..6cfcff0d34f0 100644 --- a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java +++ b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java @@ -28,7 +28,7 @@ public static CommonConfig get() { private final List clientResponseHeaders; private final List serverRequestHeaders; private final List serverResponseHeaders; - private final List grpcRequestMetadata; + private final List rpcRequestMetadata; private final boolean statementSanitizationEnabled; CommonConfig(InstrumentationConfig config) { @@ -42,8 +42,8 @@ public static CommonConfig get() { config.getList("otel.instrumentation.http.capture-headers.server.request", emptyList()); serverResponseHeaders = config.getList("otel.instrumentation.http.capture-headers.server.response", emptyList()); - grpcRequestMetadata = - config.getList("otel.instrumentation.grpc.capture-metadata.request", emptyList()); + rpcRequestMetadata = + config.getList("otel.instrumentation.rpc.capture-metadata.request", emptyList()); statementSanitizationEnabled = config.getBoolean("otel.instrumentation.common.db-statement-sanitizer.enabled", true); } @@ -68,8 +68,8 @@ public List getServerResponseHeaders() { return serverResponseHeaders; } - public List getGrpcRequestMetadata() { - return grpcRequestMetadata; + public List getRpcRequestMetadata() { + return rpcRequestMetadata; } public boolean isStatementSanitizationEnabled() { From cdd31b2583b1ae6d5ed9bbdc46c8d3dfb80bcc27 Mon Sep 17 00:00:00 2001 From: Tav Date: Wed, 2 Nov 2022 09:55:32 +0200 Subject: [PATCH 08/43] removed onEnd from comment --- .../instrumentation/grpc/v1_6/GrpcAttributesExtractor.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/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index df722ddec523..610cab067432 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -33,7 +33,7 @@ final class GrpcAttributesExtractor implements AttributesExtractor Date: Wed, 2 Nov 2022 21:11:53 +0200 Subject: [PATCH 09/43] changed requestMetadataValuesToCapture to capturedRequestMetadata moved rpcRequestMetadata definition to GrpcSingletons changed null to emptylist in GrpcRpcAttributesGetter Added caching to GrpcAttributesExtractor --- .../grpc/v1_6/GrpcSingletons.java | 11 ++++-- .../grpc/v1_6/CapturedGrpcMetadataUtil.java | 36 +++++++++++++++++++ .../grpc/v1_6/GrpcAttributesExtractor.java | 27 +++++++------- .../grpc/v1_6/GrpcRpcAttributesGetter.java | 10 +++--- .../grpc/v1_6/GrpcTelemetryBuilder.java | 6 ++-- .../bootstrap/internal/CommonConfig.java | 9 +---- 6 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java diff --git a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java index 501ace16bc62..38275157b23c 100644 --- a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java +++ b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java @@ -11,8 +11,10 @@ import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.instrumentation.grpc.v1_6.GrpcTelemetry; import io.opentelemetry.instrumentation.grpc.v1_6.internal.ContextStorageBridge; -import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig; import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig; +import java.util.List; + +import static java.util.Collections.emptyList; // Holds singleton references. public final class GrpcSingletons { @@ -28,10 +30,15 @@ public final class GrpcSingletons { InstrumentationConfig.get() .getBoolean("otel.instrumentation.grpc.experimental-span-attributes", false); + + List rpcRequestMetadata = + InstrumentationConfig.get() + .getList("otel.instrumentation.rpc.capture-metadata.request", emptyList()); + GrpcTelemetry telemetry = GrpcTelemetry.builder(GlobalOpenTelemetry.get()) .setCaptureExperimentalSpanAttributes(experimentalSpanAttributes) - .setRequestMetadataValuesToCapture(CommonConfig.get().getRpcRequestMetadata()) + .setCapturedRequestMetadata(rpcRequestMetadata) .build(); CLIENT_INTERCEPTOR = telemetry.newClientInterceptor(); diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java new file mode 100644 index 000000000000..6128004ce812 --- /dev/null +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java @@ -0,0 +1,36 @@ +package io.opentelemetry.instrumentation.grpc.v1_6; + +import static java.util.Collections.unmodifiableList; + +import io.opentelemetry.api.common.AttributeKey; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.stream.Collectors; + +final class CapturedGrpcMetadataUtil { + private static final String RPC_REQUEST_METADATA_KEY_ATTRIBUTE_PREFIX = "rpc.request.metadata."; + private static final ConcurrentMap>> requestKeysCache = + new ConcurrentHashMap<>(); + + static List lowercase(List names) { + if (names == null) { + return Collections.emptyList(); + } + + return unmodifiableList( + names.stream().map(s -> s.toLowerCase(Locale.ROOT)).collect(Collectors.toList())); + } + + static AttributeKey> requestAttributeKey(String metadataKey) { + return requestKeysCache.computeIfAbsent(metadataKey, CapturedGrpcMetadataUtil::createRequestKey); + } + + private static AttributeKey> createRequestKey(String metadataKey) { + return AttributeKey.stringArrayKey(RPC_REQUEST_METADATA_KEY_ATTRIBUTE_PREFIX + metadataKey); + } + + private CapturedGrpcMetadataUtil() {} +} diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index 610cab067432..d3ea4209633b 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -5,10 +5,7 @@ package io.opentelemetry.instrumentation.grpc.v1_6; -import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet; - import io.grpc.Status; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; @@ -16,18 +13,17 @@ import java.util.List; import javax.annotation.Nullable; -final class GrpcAttributesExtractor implements AttributesExtractor { - // TODO: This should be added to - // io.opentelemetry.semconv.trace.attributes.SemanticAttributes when possible - public static final String RPC_REQUEST_METADATA_ATTRIBUTE_VALUE_PREFIX = "rpc.request.metadata"; +import static io.opentelemetry.instrumentation.grpc.v1_6.CapturedGrpcMetadataUtil.lowercase; +import static io.opentelemetry.instrumentation.grpc.v1_6.CapturedGrpcMetadataUtil.requestAttributeKey; +final class GrpcAttributesExtractor implements AttributesExtractor { private final GrpcRpcAttributesGetter getter; - private final List requestMetadataValuesToCapture; + private final List capturedRequestMetadata; GrpcAttributesExtractor( GrpcRpcAttributesGetter getter, List requestMetadataValuesToCapture) { this.getter = getter; - this.requestMetadataValuesToCapture = requestMetadataValuesToCapture; + this.capturedRequestMetadata = lowercase(requestMetadataValuesToCapture); } @Override @@ -47,13 +43,14 @@ public void onEnd( attributes.put(SemanticAttributes.RPC_GRPC_STATUS_CODE, status.getCode().value()); } - if (requestMetadataValuesToCapture != null) { - for (String key : requestMetadataValuesToCapture) { - internalSet( - attributes, - AttributeKey.stringArrayKey(RPC_REQUEST_METADATA_ATTRIBUTE_VALUE_PREFIX + "." + key), - getter.metadataValue(request, key)); + if (capturedRequestMetadata != null) { + for (String key : capturedRequestMetadata) { + List value = getter.metadataValue(request, key); + if (value != null) { + attributes.put(requestAttributeKey(key), value); + } } } } + } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java index ae4fae729ea2..7c14aa85f581 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRpcAttributesGetter.java @@ -7,6 +7,7 @@ import io.grpc.Metadata; import io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcAttributesGetter; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @@ -42,21 +43,20 @@ public String method(GrpcRequest request) { return fullMethodName.substring(slashIndex + 1); } - @Nullable - public List metadataValue(GrpcRequest request, String key) { + List metadataValue(GrpcRequest request, String key) { if (request.getMetadata() == null) { - return null; + return Collections.emptyList(); } if (key == null || key.isEmpty()) { - return null; + return Collections.emptyList(); } Iterable values = request.getMetadata().getAll(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)); if (values == null) { - return null; + return Collections.emptyList(); } return StreamSupport.stream(values.spliterator(), false).collect(Collectors.toList()); diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java index 795cbf0aa9bf..39fb2da67780 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java @@ -118,9 +118,9 @@ public GrpcTelemetryBuilder setCaptureExperimentalSpanAttributes( /** Sets which metadata request values should be captured as span attributes. */ @CanIgnoreReturnValue - public GrpcTelemetryBuilder setRequestMetadataValuesToCapture( - List requestMetadataValuesToCapture) { - this.requestMetadataValuesToCapture = requestMetadataValuesToCapture; + public GrpcTelemetryBuilder setCapturedRequestMetadata( + List capturedRequestMetadata) { + this.requestMetadataValuesToCapture = capturedRequestMetadata; return this; } diff --git a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java index 6cfcff0d34f0..9f1c5c82a40f 100644 --- a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java +++ b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java @@ -28,7 +28,6 @@ public static CommonConfig get() { private final List clientResponseHeaders; private final List serverRequestHeaders; private final List serverResponseHeaders; - private final List rpcRequestMetadata; private final boolean statementSanitizationEnabled; CommonConfig(InstrumentationConfig config) { @@ -42,8 +41,6 @@ public static CommonConfig get() { config.getList("otel.instrumentation.http.capture-headers.server.request", emptyList()); serverResponseHeaders = config.getList("otel.instrumentation.http.capture-headers.server.response", emptyList()); - rpcRequestMetadata = - config.getList("otel.instrumentation.rpc.capture-metadata.request", emptyList()); statementSanitizationEnabled = config.getBoolean("otel.instrumentation.common.db-statement-sanitizer.enabled", true); } @@ -67,11 +64,7 @@ public List getServerRequestHeaders() { public List getServerResponseHeaders() { return serverResponseHeaders; } - - public List getRpcRequestMetadata() { - return rpcRequestMetadata; - } - + public boolean isStatementSanitizationEnabled() { return statementSanitizationEnabled; } From e0391fd47b10f2475d1ffda28f38bce1701abac2 Mon Sep 17 00:00:00 2001 From: Tav Date: Wed, 2 Nov 2022 21:41:08 +0200 Subject: [PATCH 10/43] spotless --- .../javaagent/bootstrap/internal/CommonConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java index 9f1c5c82a40f..7072f64809c9 100644 --- a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java +++ b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java @@ -64,7 +64,7 @@ public List getServerRequestHeaders() { public List getServerResponseHeaders() { return serverResponseHeaders; } - + public boolean isStatementSanitizationEnabled() { return statementSanitizationEnabled; } From ae0783be6ad3f9f95f26ec4f17e55eace824b3bb Mon Sep 17 00:00:00 2001 From: Tav Date: Wed, 2 Nov 2022 21:52:15 +0200 Subject: [PATCH 11/43] spotless --- .../javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java index 38275157b23c..30dd6c971b8b 100644 --- a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java +++ b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java @@ -5,6 +5,8 @@ package io.opentelemetry.javaagent.instrumentation.grpc.v1_6; +import static java.util.Collections.emptyList; + import io.grpc.ClientInterceptor; import io.grpc.Context; import io.grpc.ServerInterceptor; @@ -14,8 +16,6 @@ import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig; import java.util.List; -import static java.util.Collections.emptyList; - // Holds singleton references. public final class GrpcSingletons { @@ -30,7 +30,6 @@ public final class GrpcSingletons { InstrumentationConfig.get() .getBoolean("otel.instrumentation.grpc.experimental-span-attributes", false); - List rpcRequestMetadata = InstrumentationConfig.get() .getList("otel.instrumentation.rpc.capture-metadata.request", emptyList()); From 1b842eee04442c0385d177e062be1da91625948a Mon Sep 17 00:00:00 2001 From: Tav Date: Wed, 2 Nov 2022 22:08:35 +0200 Subject: [PATCH 12/43] spotless --- .../grpc/v1_6/CapturedGrpcMetadataUtil.java | 8 +++++++- .../grpc/v1_6/GrpcAttributesExtractor.java | 7 +++---- .../instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java | 3 +-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java index 6128004ce812..c178e8e48964 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.instrumentation.grpc.v1_6; import static java.util.Collections.unmodifiableList; @@ -25,7 +30,8 @@ static List lowercase(List names) { } static AttributeKey> requestAttributeKey(String metadataKey) { - return requestKeysCache.computeIfAbsent(metadataKey, CapturedGrpcMetadataUtil::createRequestKey); + return requestKeysCache.computeIfAbsent( + metadataKey, CapturedGrpcMetadataUtil::createRequestKey); } private static AttributeKey> createRequestKey(String metadataKey) { diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index d3ea4209633b..fc0e1407b77f 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -5,6 +5,9 @@ package io.opentelemetry.instrumentation.grpc.v1_6; +import static io.opentelemetry.instrumentation.grpc.v1_6.CapturedGrpcMetadataUtil.lowercase; +import static io.opentelemetry.instrumentation.grpc.v1_6.CapturedGrpcMetadataUtil.requestAttributeKey; + import io.grpc.Status; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; @@ -13,9 +16,6 @@ import java.util.List; import javax.annotation.Nullable; -import static io.opentelemetry.instrumentation.grpc.v1_6.CapturedGrpcMetadataUtil.lowercase; -import static io.opentelemetry.instrumentation.grpc.v1_6.CapturedGrpcMetadataUtil.requestAttributeKey; - final class GrpcAttributesExtractor implements AttributesExtractor { private final GrpcRpcAttributesGetter getter; private final List capturedRequestMetadata; @@ -52,5 +52,4 @@ public void onEnd( } } } - } diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java index 39fb2da67780..b5f898edbe95 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java @@ -118,8 +118,7 @@ public GrpcTelemetryBuilder setCaptureExperimentalSpanAttributes( /** Sets which metadata request values should be captured as span attributes. */ @CanIgnoreReturnValue - public GrpcTelemetryBuilder setCapturedRequestMetadata( - List capturedRequestMetadata) { + public GrpcTelemetryBuilder setCapturedRequestMetadata(List capturedRequestMetadata) { this.requestMetadataValuesToCapture = capturedRequestMetadata; return this; } From f8ed4e4ff6f1e8844f90bfce6be6563c7fc8f81d Mon Sep 17 00:00:00 2001 From: Tav Date: Thu, 3 Nov 2022 17:31:35 +0200 Subject: [PATCH 13/43] != null to !empty --- .../instrumentation/grpc/v1_6/GrpcAttributesExtractor.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/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index fc0e1407b77f..e24fde8c441b 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -46,7 +46,7 @@ public void onEnd( if (capturedRequestMetadata != null) { for (String key : capturedRequestMetadata) { List value = getter.metadataValue(request, key); - if (value != null) { + if (!value.isEmpty()) { attributes.put(requestAttributeKey(key), value); } } From 99e65a585e21dd286885df96213164febc8174df Mon Sep 17 00:00:00 2001 From: Tav Date: Thu, 3 Nov 2022 17:32:00 +0200 Subject: [PATCH 14/43] requestMetadataValuesToCapture -> capturedRequestMetadata --- .../instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java index b5f898edbe95..cf462e137c6c 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java @@ -50,7 +50,7 @@ public final class GrpcTelemetryBuilder { additionalClientExtractors = new ArrayList<>(); private boolean captureExperimentalSpanAttributes; - private List requestMetadataValuesToCapture; + private List capturedRequestMetadata; GrpcTelemetryBuilder(OpenTelemetry openTelemetry) { this.openTelemetry = openTelemetry; @@ -119,7 +119,7 @@ public GrpcTelemetryBuilder setCaptureExperimentalSpanAttributes( /** Sets which metadata request values should be captured as span attributes. */ @CanIgnoreReturnValue public GrpcTelemetryBuilder setCapturedRequestMetadata(List capturedRequestMetadata) { - this.requestMetadataValuesToCapture = capturedRequestMetadata; + this.capturedRequestMetadata = capturedRequestMetadata; return this; } @@ -149,7 +149,7 @@ public GrpcTelemetry build() { .setSpanStatusExtractor(new GrpcSpanStatusExtractor()) .addAttributesExtractor( new GrpcAttributesExtractor( - GrpcRpcAttributesGetter.INSTANCE, requestMetadataValuesToCapture)) + GrpcRpcAttributesGetter.INSTANCE, capturedRequestMetadata)) .addAttributesExtractors(additionalExtractors)); GrpcNetClientAttributesGetter netClientAttributesGetter = new GrpcNetClientAttributesGetter(); From bd73355f5ee10f93ea50780d83c09b9506df478f Mon Sep 17 00:00:00 2001 From: Tav Date: Thu, 3 Nov 2022 17:54:29 +0200 Subject: [PATCH 15/43] spotless & made METADATA_KEY protected --- .../grpc-1.6/javaagent/build.gradle.kts | 1 + .../instrumentation/grpc/v1_6/GrpcTest.java | 89 ++----------------- .../grpc/v1_6/AbstractGrpcTest.java | 63 +++++++++++++ 3 files changed, 72 insertions(+), 81 deletions(-) diff --git a/instrumentation/grpc-1.6/javaagent/build.gradle.kts b/instrumentation/grpc-1.6/javaagent/build.gradle.kts index cc0182df2fdc..6f7ec4d7e77c 100644 --- a/instrumentation/grpc-1.6/javaagent/build.gradle.kts +++ b/instrumentation/grpc-1.6/javaagent/build.gradle.kts @@ -33,5 +33,6 @@ tasks { // The agent context debug mechanism isn't compatible with the bridge approach which may add a // gRPC context to the root. jvmArgs("-Dotel.javaagent.experimental.thread-propagation-debugger.enabled=false") + jvmArgs("-Dotel.instrumentation.rpc.capture-metadata.request=some-key") } } diff --git a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java index cb6c7d9832ea..bb76830f211f 100644 --- a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java +++ b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java @@ -25,7 +25,6 @@ import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions; import java.util.Collections; -import java.util.List; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; import org.junit.jupiter.api.Test; @@ -37,7 +36,6 @@ class GrpcTest extends AbstractGrpcTest { static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); private static final AttributeKey CUSTOM_KEY = AttributeKey.stringKey("customKey"); - private static final String METADATA_ATTRIBUTE_PREFIX = "rpc.request.metadata."; private static final Metadata.Key CUSTOM_METADATA_KEY = Metadata.Key.of("customMetadataKey", Metadata.ASCII_STRING_MARSHALLER); @@ -45,13 +43,19 @@ class GrpcTest extends AbstractGrpcTest { @Override protected ServerBuilder configureServer(ServerBuilder server) { return server.intercept( - GrpcTelemetry.create(testing.getOpenTelemetry()).newServerInterceptor()); + GrpcTelemetry.builder(testing.getOpenTelemetry()) + .setCapturedRequestMetadata(Collections.singletonList(METADATA_KEY)) + .build() + .newServerInterceptor()); } @Override protected ManagedChannelBuilder configureClient(ManagedChannelBuilder client) { return client.intercept( - GrpcTelemetry.create(testing.getOpenTelemetry()).newClientInterceptor()); + GrpcTelemetry.builder(testing.getOpenTelemetry()) + .setCapturedRequestMetadata(Collections.singletonList(METADATA_KEY)) + .build() + .newClientInterceptor()); } @Override @@ -59,83 +63,6 @@ protected InstrumentationExtension testing() { return testing; } - @Test - void grpcAttributesExtractor() throws Exception { - String metadataKey = "some-key"; - AttributeKey> attributeKey = - AttributeKey.stringArrayKey(METADATA_ATTRIBUTE_PREFIX + metadataKey); - String metadataValue = "some-value"; - List metadataValueAsList = Collections.singletonList("some-value"); - - BindableService greeter = - new GreeterGrpc.GreeterImplBase() { - @Override - public void sayHello( - Helloworld.Request req, StreamObserver responseObserver) { - Helloworld.Response reply = - Helloworld.Response.newBuilder().setMessage("Hello " + req.getName()).build(); - responseObserver.onNext(reply); - responseObserver.onCompleted(); - } - }; - - GrpcAttributesExtractor grpcAttributesExtractor = - new GrpcAttributesExtractor( - GrpcRpcAttributesGetter.INSTANCE, Collections.singletonList(metadataKey)); - - Server server = - ServerBuilder.forPort(0) - .addService(greeter) - .intercept( - GrpcTelemetry.builder(testing.getOpenTelemetry()) - .addAttributeExtractor(grpcAttributesExtractor) - .build() - .newServerInterceptor()) - .build() - .start(); - - ManagedChannel channel = - createChannel( - ManagedChannelBuilder.forAddress("localhost", server.getPort()) - .intercept( - GrpcTelemetry.builder(testing.getOpenTelemetry()) - .addAttributeExtractor(grpcAttributesExtractor) - .build() - .newClientInterceptor())); - - Metadata extraMetadata = new Metadata(); - extraMetadata.put( - Metadata.Key.of(metadataKey, Metadata.ASCII_STRING_MARSHALLER), metadataValue); - - GreeterGrpc.GreeterBlockingStub client = - GreeterGrpc.newBlockingStub(channel) - .withInterceptors(MetadataUtils.newAttachHeadersInterceptor(extraMetadata)); - - Helloworld.Response response = - testing() - .runWithSpan( - "parent", - () -> client.sayHello(Helloworld.Request.newBuilder().setName("test").build())); - - OpenTelemetryAssertions.assertThat(response.getMessage()).isEqualTo("Hello test"); - - testing() - .waitAndAssertTraces( - trace -> - trace.hasSpansSatisfyingExactly( - span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), - span -> - span.hasName("example.Greeter/SayHello") - .hasKind(SpanKind.CLIENT) - .hasParent(trace.getSpan(0)) - .hasAttribute(attributeKey, metadataValueAsList), - span -> - span.hasName("example.Greeter/SayHello") - .hasKind(SpanKind.SERVER) - .hasParent(trace.getSpan(1)) - .hasAttribute(attributeKey, metadataValueAsList))); - } - @Test void metadataProvided() throws Exception { BindableService greeter = diff --git a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java index e06d70667466..5cebbbdaabb6 100644 --- a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java +++ b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java @@ -39,13 +39,18 @@ import io.grpc.reflection.v1alpha.ServerReflectionGrpc; import io.grpc.reflection.v1alpha.ServerReflectionRequest; import io.grpc.reflection.v1alpha.ServerReflectionResponse; +import io.grpc.stub.MetadataUtils; import io.grpc.stub.StreamObserver; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; import io.opentelemetry.instrumentation.testing.util.ThrowingRunnable; +import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions; import io.opentelemetry.sdk.trace.data.StatusData; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; +import java.util.Collections; +import java.util.List; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CountDownLatch; @@ -66,6 +71,7 @@ @TestInstance(TestInstance.Lifecycle.PER_CLASS) public abstract class AbstractGrpcTest { + protected static final String METADATA_KEY = "some-key"; protected abstract ServerBuilder configureServer(ServerBuilder server); @@ -1669,6 +1675,63 @@ public void sayHello( assertThat(error).hasValue(null); } + @Test + void setCapturedRequestMetadata() throws Exception { + String metadataAttributePrefix = "rpc.request.metadata."; + AttributeKey> attributeKey = + AttributeKey.stringArrayKey(metadataAttributePrefix + METADATA_KEY); + String metadataValue = "some-value"; + List metadataValueAsList = Collections.singletonList("some-value"); + + BindableService greeter = + new GreeterGrpc.GreeterImplBase() { + @Override + public void sayHello( + Helloworld.Request req, StreamObserver responseObserver) { + Helloworld.Response reply = + Helloworld.Response.newBuilder().setMessage("Hello " + req.getName()).build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } + }; + + Server server = configureServer(ServerBuilder.forPort(0).addService(greeter)).build().start(); + + ManagedChannel channel = createChannel(server); + + Metadata extraMetadata = new Metadata(); + extraMetadata.put( + Metadata.Key.of(METADATA_KEY, Metadata.ASCII_STRING_MARSHALLER), metadataValue); + + GreeterGrpc.GreeterBlockingStub client = + GreeterGrpc.newBlockingStub(channel) + .withInterceptors(MetadataUtils.newAttachHeadersInterceptor(extraMetadata)); + + Helloworld.Response response = + testing() + .runWithSpan( + "parent", + () -> client.sayHello(Helloworld.Request.newBuilder().setName("test").build())); + + OpenTelemetryAssertions.assertThat(response.getMessage()).isEqualTo("Hello test"); + + testing() + .waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName("example.Greeter/SayHello") + .hasKind(SpanKind.CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttribute(attributeKey, metadataValueAsList), + span -> + span.hasName("example.Greeter/SayHello") + .hasKind(SpanKind.SERVER) + .hasParent(trace.getSpan(1)) + .hasAttribute(attributeKey, metadataValueAsList))); + } + private ManagedChannel createChannel(Server server) throws Exception { ManagedChannelBuilder channelBuilder = configureClient(ManagedChannelBuilder.forAddress("localhost", server.getPort())); From 1b14f732b2f840737d2787bc886341c604c3f538 Mon Sep 17 00:00:00 2001 From: Tav Date: Tue, 8 Nov 2022 14:26:17 +0200 Subject: [PATCH 16/43] Initialized capturedRequestMetadata as empty list --- .../instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java index cf462e137c6c..fd741ab533a0 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java @@ -23,6 +23,7 @@ import io.opentelemetry.instrumentation.grpc.v1_6.internal.GrpcNetServerAttributesGetter; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.function.Function; import java.util.stream.Stream; @@ -50,7 +51,7 @@ public final class GrpcTelemetryBuilder { additionalClientExtractors = new ArrayList<>(); private boolean captureExperimentalSpanAttributes; - private List capturedRequestMetadata; + private List capturedRequestMetadata = Collections.emptyList(); GrpcTelemetryBuilder(OpenTelemetry openTelemetry) { this.openTelemetry = openTelemetry; From 511205064d4ed5fef5c6581bb472cba71080054c Mon Sep 17 00:00:00 2001 From: Tav Date: Tue, 8 Nov 2022 15:50:06 +0200 Subject: [PATCH 17/43] Removed . from comment --- .../instrumentation/grpc/v1_6/GrpcTelemetryBuilder.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/GrpcTelemetryBuilder.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java index fd741ab533a0..fc02fb215854 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java @@ -117,7 +117,7 @@ public GrpcTelemetryBuilder setCaptureExperimentalSpanAttributes( return this; } - /** Sets which metadata request values should be captured as span attributes. */ + /** Sets which metadata request values should be captured as span attributes */ @CanIgnoreReturnValue public GrpcTelemetryBuilder setCapturedRequestMetadata(List capturedRequestMetadata) { this.capturedRequestMetadata = capturedRequestMetadata; From 3699771e7729430fd6bd74d4dc768541062a605e Mon Sep 17 00:00:00 2001 From: Tav Date: Wed, 9 Nov 2022 09:28:33 +0200 Subject: [PATCH 18/43] Added . to comment --- .../instrumentation/grpc/v1_6/GrpcTelemetryBuilder.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/GrpcTelemetryBuilder.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java index fc02fb215854..fd741ab533a0 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java @@ -117,7 +117,7 @@ public GrpcTelemetryBuilder setCaptureExperimentalSpanAttributes( return this; } - /** Sets which metadata request values should be captured as span attributes */ + /** Sets which metadata request values should be captured as span attributes. */ @CanIgnoreReturnValue public GrpcTelemetryBuilder setCapturedRequestMetadata(List capturedRequestMetadata) { this.capturedRequestMetadata = capturedRequestMetadata; From 577d98ad9c8f17066f0a5e3575b2812ffb74b2c7 Mon Sep 17 00:00:00 2001 From: Tav Date: Wed, 9 Nov 2022 09:42:44 +0200 Subject: [PATCH 19/43] capture metadata config rpc -> grpc --- instrumentation/grpc-1.6/javaagent/build.gradle.kts | 2 +- .../javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/grpc-1.6/javaagent/build.gradle.kts b/instrumentation/grpc-1.6/javaagent/build.gradle.kts index 6f7ec4d7e77c..1d825b7015ea 100644 --- a/instrumentation/grpc-1.6/javaagent/build.gradle.kts +++ b/instrumentation/grpc-1.6/javaagent/build.gradle.kts @@ -33,6 +33,6 @@ tasks { // The agent context debug mechanism isn't compatible with the bridge approach which may add a // gRPC context to the root. jvmArgs("-Dotel.javaagent.experimental.thread-propagation-debugger.enabled=false") - jvmArgs("-Dotel.instrumentation.rpc.capture-metadata.request=some-key") + jvmArgs("-Dotel.instrumentation.grpc.capture-metadata.request=some-key") } } diff --git a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java index 30dd6c971b8b..47c74dd0afbe 100644 --- a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java +++ b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java @@ -32,7 +32,7 @@ public final class GrpcSingletons { List rpcRequestMetadata = InstrumentationConfig.get() - .getList("otel.instrumentation.rpc.capture-metadata.request", emptyList()); + .getList("otel.instrumentation.grpc.capture-metadata.request", emptyList()); GrpcTelemetry telemetry = GrpcTelemetry.builder(GlobalOpenTelemetry.get()) From 080970448aef478ab8602d645e943188710f48e2 Mon Sep 17 00:00:00 2001 From: Tav Date: Wed, 9 Nov 2022 18:41:23 +0200 Subject: [PATCH 20/43] dot --- .../instrumentation/grpc/v1_6/GrpcAttributesExtractor.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/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index e24fde8c441b..12ef5945811f 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -29,7 +29,7 @@ final class GrpcAttributesExtractor implements AttributesExtractor Date: Fri, 11 Nov 2022 11:20:06 -0800 Subject: [PATCH 21/43] Split into client/server config --- .../grpc/v1_6/GrpcSingletons.java | 10 ++++--- .../grpc/v1_6/GrpcTelemetryBuilder.java | 27 ++++++++++++++----- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java index 47c74dd0afbe..0f4e4edb5cc1 100644 --- a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java +++ b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java @@ -30,14 +30,18 @@ public final class GrpcSingletons { InstrumentationConfig.get() .getBoolean("otel.instrumentation.grpc.experimental-span-attributes", false); - List rpcRequestMetadata = + List clientRequestMetadata = InstrumentationConfig.get() - .getList("otel.instrumentation.grpc.capture-metadata.request", emptyList()); + .getList("otel.instrumentation.grpc.capture-metadata.client.request", emptyList()); + List serverRequestMetadata = + InstrumentationConfig.get() + .getList("otel.instrumentation.grpc.capture-metadata.server.request", emptyList()); GrpcTelemetry telemetry = GrpcTelemetry.builder(GlobalOpenTelemetry.get()) .setCaptureExperimentalSpanAttributes(experimentalSpanAttributes) - .setCapturedRequestMetadata(rpcRequestMetadata) + .setCapturedClientRequestMetadata(clientRequestMetadata) + .setCapturedServerRequestMetadata(serverRequestMetadata) .build(); CLIENT_INTERCEPTOR = telemetry.newClientInterceptor(); diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java index fd741ab533a0..07a19cd73208 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java @@ -51,7 +51,8 @@ public final class GrpcTelemetryBuilder { additionalClientExtractors = new ArrayList<>(); private boolean captureExperimentalSpanAttributes; - private List capturedRequestMetadata = Collections.emptyList(); + private List capturedClientRequestMetadata = Collections.emptyList(); + private List capturedServerRequestMetadata = Collections.emptyList(); GrpcTelemetryBuilder(OpenTelemetry openTelemetry) { this.openTelemetry = openTelemetry; @@ -117,10 +118,19 @@ public GrpcTelemetryBuilder setCaptureExperimentalSpanAttributes( return this; } - /** Sets which metadata request values should be captured as span attributes. */ + /** Sets which metadata request values should be captured as span attributes on client spans. */ @CanIgnoreReturnValue - public GrpcTelemetryBuilder setCapturedRequestMetadata(List capturedRequestMetadata) { - this.capturedRequestMetadata = capturedRequestMetadata; + public GrpcTelemetryBuilder setCapturedClientRequestMetadata( + List capturedClientRequestMetadata) { + this.capturedClientRequestMetadata = capturedClientRequestMetadata; + return this; + } + + /** Sets which metadata request values should be captured as span attributes on server spans. */ + @CanIgnoreReturnValue + public GrpcTelemetryBuilder setCapturedServerRequestMetadata( + List capturedServerRequestMetadata) { + this.capturedServerRequestMetadata = capturedServerRequestMetadata; return this; } @@ -148,9 +158,6 @@ public GrpcTelemetry build() { instrumenter -> instrumenter .setSpanStatusExtractor(new GrpcSpanStatusExtractor()) - .addAttributesExtractor( - new GrpcAttributesExtractor( - GrpcRpcAttributesGetter.INSTANCE, capturedRequestMetadata)) .addAttributesExtractors(additionalExtractors)); GrpcNetClientAttributesGetter netClientAttributesGetter = new GrpcNetClientAttributesGetter(); @@ -160,11 +167,17 @@ public GrpcTelemetry build() { .addAttributesExtractor(RpcClientAttributesExtractor.create(rpcAttributesGetter)) .addAttributesExtractor(NetClientAttributesExtractor.create(netClientAttributesGetter)) .addAttributesExtractors(additionalClientExtractors) + .addAttributesExtractor( + new GrpcAttributesExtractor( + GrpcRpcAttributesGetter.INSTANCE, capturedClientRequestMetadata)) .addOperationMetrics(RpcClientMetrics.get()); serverInstrumenterBuilder .addAttributesExtractor(RpcServerAttributesExtractor.create(rpcAttributesGetter)) .addAttributesExtractor( NetServerAttributesExtractor.create(new GrpcNetServerAttributesGetter())) + .addAttributesExtractor( + new GrpcAttributesExtractor( + GrpcRpcAttributesGetter.INSTANCE, capturedServerRequestMetadata)) .addOperationMetrics(RpcServerMetrics.get()); if (peerService != null) { From 80a8e694e69d50442eac2036fd7df14067e39de8 Mon Sep 17 00:00:00 2001 From: Tav Herzlich <44731477+Tavh@users.noreply.github.com> Date: Fri, 11 Nov 2022 22:33:07 +0200 Subject: [PATCH 22/43] Update build.gradle.kts --- instrumentation/grpc-1.6/javaagent/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/grpc-1.6/javaagent/build.gradle.kts b/instrumentation/grpc-1.6/javaagent/build.gradle.kts index 1d825b7015ea..582c2fc76457 100644 --- a/instrumentation/grpc-1.6/javaagent/build.gradle.kts +++ b/instrumentation/grpc-1.6/javaagent/build.gradle.kts @@ -33,6 +33,6 @@ tasks { // The agent context debug mechanism isn't compatible with the bridge approach which may add a // gRPC context to the root. jvmArgs("-Dotel.javaagent.experimental.thread-propagation-debugger.enabled=false") - jvmArgs("-Dotel.instrumentation.grpc.capture-metadata.request=some-key") + jvmArgs("-Dotel.instrumentation.grpc.capture-metadata.client.request=some-key") } } From ad399cebc78ef3586a599b8bea5d4272563ad8a6 Mon Sep 17 00:00:00 2001 From: Tav Herzlich <44731477+Tavh@users.noreply.github.com> Date: Fri, 11 Nov 2022 22:52:01 +0200 Subject: [PATCH 23/43] fix test --- .../io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java index bb76830f211f..0c3032d4c5f8 100644 --- a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java +++ b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java @@ -44,7 +44,7 @@ class GrpcTest extends AbstractGrpcTest { protected ServerBuilder configureServer(ServerBuilder server) { return server.intercept( GrpcTelemetry.builder(testing.getOpenTelemetry()) - .setCapturedRequestMetadata(Collections.singletonList(METADATA_KEY)) + .setCapturedClientRequestMetadata(Collections.singletonList(METADATA_KEY)) .build() .newServerInterceptor()); } @@ -53,7 +53,7 @@ protected ServerBuilder configureServer(ServerBuilder server) { protected ManagedChannelBuilder configureClient(ManagedChannelBuilder client) { return client.intercept( GrpcTelemetry.builder(testing.getOpenTelemetry()) - .setCapturedRequestMetadata(Collections.singletonList(METADATA_KEY)) + .setCapturedClientRequestMetadata(Collections.singletonList(METADATA_KEY)) .build() .newClientInterceptor()); } From 095e7e9ee50a72bc41cc40c37d061f10ebf425fb Mon Sep 17 00:00:00 2001 From: Tav Herzlich <44731477+Tavh@users.noreply.github.com> Date: Fri, 11 Nov 2022 23:13:41 +0200 Subject: [PATCH 24/43] fix test --- .../instrumentation/grpc/v1_6/AbstractGrpcTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java index 5cebbbdaabb6..1cbb789fa8c0 100644 --- a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java +++ b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java @@ -1677,7 +1677,7 @@ public void sayHello( @Test void setCapturedRequestMetadata() throws Exception { - String metadataAttributePrefix = "rpc.request.metadata."; + String metadataAttributePrefix = "rpc.client.request.metadata."; AttributeKey> attributeKey = AttributeKey.stringArrayKey(metadataAttributePrefix + METADATA_KEY); String metadataValue = "some-value"; From 50776df25eb9eff6df3feca76a06103c5064e055 Mon Sep 17 00:00:00 2001 From: Tav Date: Sun, 13 Nov 2022 10:15:52 +0200 Subject: [PATCH 25/43] modified grpc test --- .../grpc-1.6/javaagent/build.gradle.kts | 3 ++- .../instrumentation/grpc/v1_6/GrpcTest.java | 4 ++-- .../grpc/v1_6/AbstractGrpcTest.java | 22 ++++++++++++------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/instrumentation/grpc-1.6/javaagent/build.gradle.kts b/instrumentation/grpc-1.6/javaagent/build.gradle.kts index 582c2fc76457..a8c93d62b6a7 100644 --- a/instrumentation/grpc-1.6/javaagent/build.gradle.kts +++ b/instrumentation/grpc-1.6/javaagent/build.gradle.kts @@ -33,6 +33,7 @@ tasks { // The agent context debug mechanism isn't compatible with the bridge approach which may add a // gRPC context to the root. jvmArgs("-Dotel.javaagent.experimental.thread-propagation-debugger.enabled=false") - jvmArgs("-Dotel.instrumentation.grpc.capture-metadata.client.request=some-key") + jvmArgs("-Dotel.instrumentation.grpc.capture-metadata.client.request=some-client-key") + jvmArgs("-Dotel.instrumentation.grpc.capture-metadata.server.request=some-server-key") } } diff --git a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java index 0c3032d4c5f8..6120f7233ff6 100644 --- a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java +++ b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java @@ -44,7 +44,7 @@ class GrpcTest extends AbstractGrpcTest { protected ServerBuilder configureServer(ServerBuilder server) { return server.intercept( GrpcTelemetry.builder(testing.getOpenTelemetry()) - .setCapturedClientRequestMetadata(Collections.singletonList(METADATA_KEY)) + .setCapturedServerRequestMetadata(Collections.singletonList(SERVER_REQUEST_METADATA_KEY)) .build() .newServerInterceptor()); } @@ -53,7 +53,7 @@ protected ServerBuilder configureServer(ServerBuilder server) { protected ManagedChannelBuilder configureClient(ManagedChannelBuilder client) { return client.intercept( GrpcTelemetry.builder(testing.getOpenTelemetry()) - .setCapturedClientRequestMetadata(Collections.singletonList(METADATA_KEY)) + .setCapturedClientRequestMetadata(Collections.singletonList(SERVER_REQUEST_METADATA_KEY)) .build() .newClientInterceptor()); } diff --git a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java index 1cbb789fa8c0..6f4934ec98fc 100644 --- a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java +++ b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java @@ -71,7 +71,9 @@ @TestInstance(TestInstance.Lifecycle.PER_CLASS) public abstract class AbstractGrpcTest { - protected static final String METADATA_KEY = "some-key"; + protected static final String CLIENT_REQUEST_METADATA_KEY = "some-client-key"; + + protected static final String SERVER_REQUEST_METADATA_KEY = "some-server-key"; protected abstract ServerBuilder configureServer(ServerBuilder server); @@ -1677,11 +1679,13 @@ public void sayHello( @Test void setCapturedRequestMetadata() throws Exception { - String metadataAttributePrefix = "rpc.client.request.metadata."; - AttributeKey> attributeKey = - AttributeKey.stringArrayKey(metadataAttributePrefix + METADATA_KEY); + String metadataAttributePrefix = "rpc.request.metadata."; + AttributeKey> clientAttributeKey = + AttributeKey.stringArrayKey(metadataAttributePrefix + CLIENT_REQUEST_METADATA_KEY); + AttributeKey> serverAttributeKey = + AttributeKey.stringArrayKey(metadataAttributePrefix + SERVER_REQUEST_METADATA_KEY); String metadataValue = "some-value"; - List metadataValueAsList = Collections.singletonList("some-value"); + List metadataValueAsList = Collections.singletonList(metadataValue); BindableService greeter = new GreeterGrpc.GreeterImplBase() { @@ -1701,7 +1705,9 @@ public void sayHello( Metadata extraMetadata = new Metadata(); extraMetadata.put( - Metadata.Key.of(METADATA_KEY, Metadata.ASCII_STRING_MARSHALLER), metadataValue); + Metadata.Key.of(SERVER_REQUEST_METADATA_KEY, Metadata.ASCII_STRING_MARSHALLER), metadataValue); + extraMetadata.put( + Metadata.Key.of(CLIENT_REQUEST_METADATA_KEY, Metadata.ASCII_STRING_MARSHALLER), metadataValue); GreeterGrpc.GreeterBlockingStub client = GreeterGrpc.newBlockingStub(channel) @@ -1724,12 +1730,12 @@ public void sayHello( span.hasName("example.Greeter/SayHello") .hasKind(SpanKind.CLIENT) .hasParent(trace.getSpan(0)) - .hasAttribute(attributeKey, metadataValueAsList), + .hasAttribute(clientAttributeKey, metadataValueAsList), span -> span.hasName("example.Greeter/SayHello") .hasKind(SpanKind.SERVER) .hasParent(trace.getSpan(1)) - .hasAttribute(attributeKey, metadataValueAsList))); + .hasAttribute(serverAttributeKey, metadataValueAsList))); } private ManagedChannel createChannel(Server server) throws Exception { From a9122fd2b1a3a324a6dfcc7a79c944db83df6128 Mon Sep 17 00:00:00 2001 From: Tav Date: Sun, 13 Nov 2022 10:23:36 +0200 Subject: [PATCH 26/43] spotless --- .../opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java index 6120f7233ff6..0263f05e3512 100644 --- a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java +++ b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java @@ -44,7 +44,8 @@ class GrpcTest extends AbstractGrpcTest { protected ServerBuilder configureServer(ServerBuilder server) { return server.intercept( GrpcTelemetry.builder(testing.getOpenTelemetry()) - .setCapturedServerRequestMetadata(Collections.singletonList(SERVER_REQUEST_METADATA_KEY)) + .setCapturedServerRequestMetadata( + Collections.singletonList(SERVER_REQUEST_METADATA_KEY)) .build() .newServerInterceptor()); } @@ -53,7 +54,8 @@ protected ServerBuilder configureServer(ServerBuilder server) { protected ManagedChannelBuilder configureClient(ManagedChannelBuilder client) { return client.intercept( GrpcTelemetry.builder(testing.getOpenTelemetry()) - .setCapturedClientRequestMetadata(Collections.singletonList(SERVER_REQUEST_METADATA_KEY)) + .setCapturedClientRequestMetadata( + Collections.singletonList(SERVER_REQUEST_METADATA_KEY)) .build() .newClientInterceptor()); } From 130062175984c43f0426fcad5c246c869c9f93db Mon Sep 17 00:00:00 2001 From: Tav Date: Sun, 13 Nov 2022 10:28:42 +0200 Subject: [PATCH 27/43] Fixed test --- .../io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java index 0263f05e3512..08627e2eba2b 100644 --- a/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java +++ b/instrumentation/grpc-1.6/library/src/test/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTest.java @@ -55,7 +55,7 @@ protected ManagedChannelBuilder configureClient(ManagedChannelBuilder clie return client.intercept( GrpcTelemetry.builder(testing.getOpenTelemetry()) .setCapturedClientRequestMetadata( - Collections.singletonList(SERVER_REQUEST_METADATA_KEY)) + Collections.singletonList(CLIENT_REQUEST_METADATA_KEY)) .build() .newClientInterceptor()); } From e8ef646e7d1e21028e8e8eea80e1ca56c8f65d3a Mon Sep 17 00:00:00 2001 From: Tav Date: Sun, 13 Nov 2022 10:34:54 +0200 Subject: [PATCH 28/43] spotless --- .../instrumentation/grpc/v1_6/AbstractGrpcTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java index 6f4934ec98fc..0606e8e74b5b 100644 --- a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java +++ b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java @@ -1705,9 +1705,11 @@ public void sayHello( Metadata extraMetadata = new Metadata(); extraMetadata.put( - Metadata.Key.of(SERVER_REQUEST_METADATA_KEY, Metadata.ASCII_STRING_MARSHALLER), metadataValue); + Metadata.Key.of(SERVER_REQUEST_METADATA_KEY, Metadata.ASCII_STRING_MARSHALLER), + metadataValue); extraMetadata.put( - Metadata.Key.of(CLIENT_REQUEST_METADATA_KEY, Metadata.ASCII_STRING_MARSHALLER), metadataValue); + Metadata.Key.of(CLIENT_REQUEST_METADATA_KEY, Metadata.ASCII_STRING_MARSHALLER), + metadataValue); GreeterGrpc.GreeterBlockingStub client = GreeterGrpc.newBlockingStub(channel) From 45fa0e7854a9e2ea96f7eea4d874f311f98d98c1 Mon Sep 17 00:00:00 2001 From: Tav Date: Sun, 13 Nov 2022 21:15:19 +0200 Subject: [PATCH 29/43] changed test values --- .../grpc/v1_6/AbstractGrpcTest.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java index 0606e8e74b5b..3e30adbbf6b0 100644 --- a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java +++ b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java @@ -1684,8 +1684,8 @@ void setCapturedRequestMetadata() throws Exception { AttributeKey.stringArrayKey(metadataAttributePrefix + CLIENT_REQUEST_METADATA_KEY); AttributeKey> serverAttributeKey = AttributeKey.stringArrayKey(metadataAttributePrefix + SERVER_REQUEST_METADATA_KEY); - String metadataValue = "some-value"; - List metadataValueAsList = Collections.singletonList(metadataValue); + String serverMetadataValue = "server-value"; + String clientMetadataValue = "client-value"; BindableService greeter = new GreeterGrpc.GreeterImplBase() { @@ -1706,10 +1706,10 @@ public void sayHello( Metadata extraMetadata = new Metadata(); extraMetadata.put( Metadata.Key.of(SERVER_REQUEST_METADATA_KEY, Metadata.ASCII_STRING_MARSHALLER), - metadataValue); + serverMetadataValue); extraMetadata.put( Metadata.Key.of(CLIENT_REQUEST_METADATA_KEY, Metadata.ASCII_STRING_MARSHALLER), - metadataValue); + clientMetadataValue); GreeterGrpc.GreeterBlockingStub client = GreeterGrpc.newBlockingStub(channel) @@ -1732,12 +1732,14 @@ public void sayHello( span.hasName("example.Greeter/SayHello") .hasKind(SpanKind.CLIENT) .hasParent(trace.getSpan(0)) - .hasAttribute(clientAttributeKey, metadataValueAsList), + .hasAttribute(clientAttributeKey, + Collections.singletonList(clientMetadataValue)), span -> span.hasName("example.Greeter/SayHello") .hasKind(SpanKind.SERVER) .hasParent(trace.getSpan(1)) - .hasAttribute(serverAttributeKey, metadataValueAsList))); + .hasAttribute(serverAttributeKey, + Collections.singletonList(serverMetadataValue)))); } private ManagedChannel createChannel(Server server) throws Exception { From ba9cd2de4793ed4324956c4a0c51905d999e7a6d Mon Sep 17 00:00:00 2001 From: Tav Date: Sun, 13 Nov 2022 21:23:20 +0200 Subject: [PATCH 30/43] spotless --- .../instrumentation/grpc/v1_6/AbstractGrpcTest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java index 3e30adbbf6b0..c0b3a976f119 100644 --- a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java +++ b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java @@ -1732,13 +1732,14 @@ public void sayHello( span.hasName("example.Greeter/SayHello") .hasKind(SpanKind.CLIENT) .hasParent(trace.getSpan(0)) - .hasAttribute(clientAttributeKey, - Collections.singletonList(clientMetadataValue)), + .hasAttribute( + clientAttributeKey, Collections.singletonList(clientMetadataValue)), span -> span.hasName("example.Greeter/SayHello") .hasKind(SpanKind.SERVER) .hasParent(trace.getSpan(1)) - .hasAttribute(serverAttributeKey, + .hasAttribute( + serverAttributeKey, Collections.singletonList(serverMetadataValue)))); } From 28019197af67dc197efaaa68703f19288b8c8bdb Mon Sep 17 00:00:00 2001 From: Tav Date: Sun, 13 Nov 2022 21:48:37 +0200 Subject: [PATCH 31/43] comment --- .../instrumentation/grpc/v1_6/GrpcAttributesExtractor.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/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index 12ef5945811f..e24fde8c441b 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -29,7 +29,7 @@ final class GrpcAttributesExtractor implements AttributesExtractor Date: Mon, 14 Nov 2022 10:07:10 +0200 Subject: [PATCH 32/43] comment --- .../instrumentation/grpc/v1_6/GrpcAttributesExtractor.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/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index e24fde8c441b..12ef5945811f 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -29,7 +29,7 @@ final class GrpcAttributesExtractor implements AttributesExtractor Date: Wed, 16 Nov 2022 23:32:31 +0200 Subject: [PATCH 33/43] removed null checks --- .../grpc/v1_6/CapturedGrpcMetadataUtil.java | 4 +--- .../grpc/v1_6/GrpcAttributesExtractor.java | 10 ++++------ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java index c178e8e48964..690437f0f070 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java @@ -21,9 +21,7 @@ final class CapturedGrpcMetadataUtil { new ConcurrentHashMap<>(); static List lowercase(List names) { - if (names == null) { - return Collections.emptyList(); - } + return Collections.emptyList(); return unmodifiableList( names.stream().map(s -> s.toLowerCase(Locale.ROOT)).collect(Collectors.toList())); diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index 12ef5945811f..59632c6c881f 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -43,12 +43,10 @@ public void onEnd( attributes.put(SemanticAttributes.RPC_GRPC_STATUS_CODE, status.getCode().value()); } - if (capturedRequestMetadata != null) { - for (String key : capturedRequestMetadata) { - List value = getter.metadataValue(request, key); - if (!value.isEmpty()) { - attributes.put(requestAttributeKey(key), value); - } + for (String key : capturedRequestMetadata) { + List value = getter.metadataValue(request, key); + if (!value.isEmpty()) { + attributes.put(requestAttributeKey(key), value); } } } From 5c75eccbdf9b9a3e20bfcf5f4dbc999ee780d377 Mon Sep 17 00:00:00 2001 From: Tav Date: Wed, 16 Nov 2022 23:40:20 +0200 Subject: [PATCH 34/43] removed nullcheck --- .../instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java index 690437f0f070..25da7c8ca8fc 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java @@ -21,8 +21,6 @@ final class CapturedGrpcMetadataUtil { new ConcurrentHashMap<>(); static List lowercase(List names) { - return Collections.emptyList(); - return unmodifiableList( names.stream().map(s -> s.toLowerCase(Locale.ROOT)).collect(Collectors.toList())); } From e2fbb6c3b38da2ef628a62b92612e1e4c8301250 Mon Sep 17 00:00:00 2001 From: Tav Date: Thu, 17 Nov 2022 11:54:25 +0200 Subject: [PATCH 35/43] fixed compilation error --- .../instrumentation/grpc/v1_6/GrpcTelemetryBuilder.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/GrpcTelemetryBuilder.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java index 21fb59f2f850..5376cff084c6 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java @@ -192,7 +192,7 @@ public GrpcTelemetry build() { .addAttributesExtractor( new GrpcAttributesExtractor( GrpcRpcAttributesGetter.INSTANCE, capturedServerRequestMetadata)) - .addAttributesExtractors(additionalServerExtractors + .addAttributesExtractors(additionalServerExtractors) .addOperationMetrics(RpcServerMetrics.get()); if (peerService != null) { From bb30c18ba22873a4a5a66d76af5e4efc9b532e55 Mon Sep 17 00:00:00 2001 From: Tav Date: Thu, 17 Nov 2022 13:31:07 +0200 Subject: [PATCH 36/43] spotless --- .../instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java | 1 - 1 file changed, 1 deletion(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java index 25da7c8ca8fc..10f4ca3673d9 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java @@ -8,7 +8,6 @@ import static java.util.Collections.unmodifiableList; import io.opentelemetry.api.common.AttributeKey; -import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.concurrent.ConcurrentHashMap; From ffdb462a04e2010e1a63ef808a4f2ad0d633496e Mon Sep 17 00:00:00 2001 From: Tav Date: Sun, 27 Nov 2022 16:52:33 +0200 Subject: [PATCH 37/43] Moved metadata capture to request start --- .../grpc/v1_6/GrpcAttributesExtractor.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index 59632c6c881f..a5eed38433f8 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -29,16 +29,6 @@ final class GrpcAttributesExtractor implements AttributesExtractor Date: Sun, 27 Nov 2022 17:07:19 +0200 Subject: [PATCH 38/43] Moved metadata capture to request start --- .../grpc/v1_6/GrpcAttributesExtractor.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index a5eed38433f8..04c494a0257b 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -28,11 +28,7 @@ final class GrpcAttributesExtractor implements AttributesExtractor value = getter.metadataValue(request, key); if (!value.isEmpty()) { @@ -48,6 +44,8 @@ public void onEnd( GrpcRequest request, @Nullable Status status, @Nullable Throwable error) { - // Request attributes captured on request start. + if (status != null) { + attributes.put(SemanticAttributes.RPC_GRPC_STATUS_CODE, status.getCode().value()); + } } } From 0026c0daa80012526133643fe2597bcee103d828 Mon Sep 17 00:00:00 2001 From: Tav Date: Sun, 27 Nov 2022 17:22:50 +0200 Subject: [PATCH 39/43] spotless --- .../instrumentation/grpc/v1_6/GrpcAttributesExtractor.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index 04c494a0257b..ec73fbe57ed5 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -27,8 +27,7 @@ final class GrpcAttributesExtractor implements AttributesExtractor value = getter.metadataValue(request, key); if (!value.isEmpty()) { From 1ef28b61c662b79832a3ece7fabf9df467453210 Mon Sep 17 00:00:00 2001 From: Tav Date: Sun, 11 Dec 2022 12:40:07 +0200 Subject: [PATCH 40/43] moved attributes capture back to request end --- .../grpc/v1_6/GrpcAttributesExtractor.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index ec73fbe57ed5..730b78806eee 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -28,12 +28,7 @@ final class GrpcAttributesExtractor implements AttributesExtractor value = getter.metadataValue(request, key); - if (!value.isEmpty()) { - attributes.put(requestAttributeKey(key), value); - } - } + // Attributes are captured only at request end } @Override @@ -46,5 +41,11 @@ public void onEnd( if (status != null) { attributes.put(SemanticAttributes.RPC_GRPC_STATUS_CODE, status.getCode().value()); } + for (String key : capturedRequestMetadata) { + List value = getter.metadataValue(request, key); + if (!value.isEmpty()) { + attributes.put(requestAttributeKey(key), value); + } + } } } From 23a8daffb58d55c16734ddf8f5d853e9a4857495 Mon Sep 17 00:00:00 2001 From: Tav Date: Sun, 11 Dec 2022 12:42:38 +0200 Subject: [PATCH 41/43] revert comment --- .../instrumentation/grpc/v1_6/GrpcAttributesExtractor.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/GrpcAttributesExtractor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java index 730b78806eee..a8c5f292992d 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcAttributesExtractor.java @@ -28,7 +28,7 @@ final class GrpcAttributesExtractor implements AttributesExtractor Date: Fri, 13 Jan 2023 18:40:32 +0200 Subject: [PATCH 42/43] renamed prefix --- .../instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java | 2 +- .../instrumentation/grpc/v1_6/AbstractGrpcTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java index 10f4ca3673d9..da0cd2d618ba 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java @@ -15,7 +15,7 @@ import java.util.stream.Collectors; final class CapturedGrpcMetadataUtil { - private static final String RPC_REQUEST_METADATA_KEY_ATTRIBUTE_PREFIX = "rpc.request.metadata."; + private static final String RPC_REQUEST_METADATA_KEY_ATTRIBUTE_PREFIX = "rpc.grpc.request.metadata."; private static final ConcurrentMap>> requestKeysCache = new ConcurrentHashMap<>(); diff --git a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java index c0b3a976f119..ecd861aa974d 100644 --- a/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java +++ b/instrumentation/grpc-1.6/testing/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/AbstractGrpcTest.java @@ -1679,7 +1679,7 @@ public void sayHello( @Test void setCapturedRequestMetadata() throws Exception { - String metadataAttributePrefix = "rpc.request.metadata."; + String metadataAttributePrefix = "rpc.grpc.request.metadata."; AttributeKey> clientAttributeKey = AttributeKey.stringArrayKey(metadataAttributePrefix + CLIENT_REQUEST_METADATA_KEY); AttributeKey> serverAttributeKey = From bea9394612c94c505e9507e89d05b4ffa7796438 Mon Sep 17 00:00:00 2001 From: Tav Date: Fri, 13 Jan 2023 18:56:48 +0200 Subject: [PATCH 43/43] spotless --- .../instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java index da0cd2d618ba..aa46a91f8ddb 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/CapturedGrpcMetadataUtil.java @@ -15,7 +15,8 @@ import java.util.stream.Collectors; final class CapturedGrpcMetadataUtil { - private static final String RPC_REQUEST_METADATA_KEY_ATTRIBUTE_PREFIX = "rpc.grpc.request.metadata."; + private static final String RPC_REQUEST_METADATA_KEY_ATTRIBUTE_PREFIX = + "rpc.grpc.request.metadata."; private static final ConcurrentMap>> requestKeysCache = new ConcurrentHashMap<>();