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..0d12781177dd 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 @@ -48,6 +48,8 @@ public final class GrpcTelemetryBuilder { additionalExtractors = new ArrayList<>(); private final List> additionalClientExtractors = new ArrayList<>(); + private final List> + additionalServerExtractors = new ArrayList<>(); private boolean captureExperimentalSpanAttributes; @@ -78,6 +80,18 @@ public GrpcTelemetryBuilder addClientAttributeExtractor( return this; } + /** + * Adds an extra server-only {@link AttributesExtractor} to invoke to set attributes to + * instrumented items. The {@link AttributesExtractor} will be executed after all default + * extractors. + */ + @CanIgnoreReturnValue + public GrpcTelemetryBuilder addServerAttributeExtractor( + AttributesExtractor attributesExtractor) { + additionalServerExtractors.add(attributesExtractor); + return this; + } + /** Sets custom client {@link SpanNameExtractor} via transform function. */ @CanIgnoreReturnValue public GrpcTelemetryBuilder setClientSpanNameExtractor( @@ -154,6 +168,7 @@ public GrpcTelemetry build() { .addAttributesExtractor(RpcServerAttributesExtractor.create(rpcAttributesGetter)) .addAttributesExtractor( NetServerAttributesExtractor.create(new GrpcNetServerAttributesGetter())) + .addAttributesExtractors(additionalServerExtractors) .addOperationMetrics(RpcServerMetrics.get()); if (peerService != null) { 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..373dfbfe72f2 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 @@ -35,6 +35,7 @@ class GrpcTest extends AbstractGrpcTest { static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); private static final AttributeKey CUSTOM_KEY = AttributeKey.stringKey("customKey"); + private static final AttributeKey CUSTOM_KEY2 = AttributeKey.stringKey("customKey2"); private static final Metadata.Key CUSTOM_METADATA_KEY = Metadata.Key.of("customMetadataKey", Metadata.ASCII_STRING_MARSHALLER); @@ -56,6 +57,11 @@ protected InstrumentationExtension testing() { return testing; } + /** + * metadataProvided. testing as well 2 extra methods: {@link + * GrpcTelemetryBuilder#addClientAttributeExtractor} and {@link + * GrpcTelemetryBuilder#addServerAttributeExtractor} + */ @Test void metadataProvided() throws Exception { BindableService greeter = @@ -76,6 +82,7 @@ public void sayHello( .intercept( GrpcTelemetry.builder(testing.getOpenTelemetry()) .addAttributeExtractor(new CustomAttributesExtractor()) + .addServerAttributeExtractor(new CustomAttributesExtractorV2("serverSideValue")) .build() .newServerInterceptor()) .build() @@ -87,6 +94,8 @@ public void sayHello( .intercept( GrpcTelemetry.builder(testing.getOpenTelemetry()) .addAttributeExtractor(new CustomAttributesExtractor()) + .addClientAttributeExtractor( + new CustomAttributesExtractorV2("clientSideValue")) .build() .newClientInterceptor())); @@ -117,11 +126,13 @@ public void sayHello( span.hasName("example.Greeter/SayHello") .hasKind(SpanKind.CLIENT) .hasParent(trace.getSpan(0)) + .hasAttribute(CUSTOM_KEY2, "clientSideValue") .hasAttribute(CUSTOM_KEY, "customValue"), span -> span.hasName("example.Greeter/SayHello") .hasKind(SpanKind.SERVER) .hasParent(trace.getSpan(1)) + .hasAttribute(CUSTOM_KEY2, "serverSideValue") .hasAttribute(CUSTOM_KEY, "customValue"))); } @@ -149,4 +160,29 @@ public void onEnd( } } } + + private static class CustomAttributesExtractorV2 + implements AttributesExtractor { + + private final String valueOfKey2; + + public CustomAttributesExtractorV2(String valueOfKey2) { + this.valueOfKey2 = valueOfKey2; + } + + @Override + public void onStart( + AttributesBuilder attributes, Context parentContext, GrpcRequest grpcRequest) { + + attributes.put(CUSTOM_KEY2, valueOfKey2); + } + + @Override + public void onEnd( + AttributesBuilder attributes, + Context context, + GrpcRequest grpcRequest, + @Nullable Status status, + @Nullable Throwable error) {} + } }