Add peer.service to grpc javaagent instrumentation
MODIFY
instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java
instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java
@@ -8,24 +8,43 @@ package io.opentelemetry.javaagent.instrumentation.grpc.v1_6;
import io.grpc.ClientInterceptor;
import io.grpc.Context;
import io.grpc.ServerInterceptor;
+import io.grpc.Status;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.config.Config;
+import io.opentelemetry.instrumentation.grpc.v1_6.GrpcRequest;
import io.opentelemetry.instrumentation.grpc.v1_6.GrpcTracing;
+import io.opentelemetry.instrumentation.grpc.v1_6.GrpcTracingBuilder;
import io.opentelemetry.instrumentation.grpc.v1_6.internal.ContextStorageBridge;
+import io.opentelemetry.javaagent.instrumentation.api.instrumenter.PeerServiceAttributesExtractor;
// Holds singleton references.
public final class GrpcSingletons {
- private static final GrpcTracing TRACING =
+
+ public static final ClientInterceptor CLIENT_INTERCEPTOR;
+
+ public static final ServerInterceptor SERVER_INTERCEPTOR;
+
+ public static final Context.Storage STORAGE = new ContextStorageBridge();
+
+ static {
+ GrpcTracingBuilder builder =
GrpcTracing.newBuilder(GlobalOpenTelemetry.get())
.setCaptureExperimentalSpanAttributes(
Config.get()
.getBooleanProperty(
- "otel.instrumentation.grpc.experimental-span-attributes", false))
- .build();
+ "otel.instrumentation.grpc.experimental-span-attributes", false));
- public static final ClientInterceptor CLIENT_INTERCEPTOR = TRACING.newClientInterceptor();
+ PeerServiceAttributesExtractor<GrpcRequest, Status> peerServiceExtractor =
+ PeerServiceAttributesExtractor.createUsingReflection(
+ "io.opentelemetry.instrumentation.grpc.v1_6.GrpcNetAttributesExtractor");
+ if (peerServiceExtractor != null) {
+ builder.addAttributeExtractor(peerServiceExtractor);
+ }
- public static final ServerInterceptor SERVER_INTERCEPTOR = TRACING.newServerInterceptor();
+ GrpcTracing tracing = builder.build();
+ CLIENT_INTERCEPTOR = tracing.newClientInterceptor();
+ SERVER_INTERCEPTOR = tracing.newServerInterceptor();
+ }
- public static final Context.Storage STORAGE = new ContextStorageBridge();
+ private GrpcSingletons() {}
}

MODIFY
instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequest.java
instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcRequest.java
@@ -10,7 +10,7 @@ import io.grpc.MethodDescriptor;
import java.net.SocketAddress;
import org.checkerframework.checker.nullness.qual.Nullable;
-final class GrpcRequest {
+public final class GrpcRequest {
private final MethodDescriptor<?, ?> method;
@Nullable private final Metadata metadata;
@@ -26,15 +26,17 @@ final class GrpcRequest {
this.remoteAddress = remoteAddress;
}
- MethodDescriptor<?, ?> getMethod() {
+ public MethodDescriptor<?, ?> getMethod() {
return method;
}
- Metadata getMetadata() {
+ @Nullable
+ public Metadata getMetadata() {
return metadata;
}
- SocketAddress getRemoteAddress() {
+ @Nullable
+ public SocketAddress getRemoteAddress() {
return remoteAddress;
}

Add peer.service to grpc javaagent instrumentation (#3357)