Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.instrumentation.grpc.v1_6.GrpcSingletons.MANAGED_CHANNEL_BUILDER_INSTRUMENTED;
import static net.bytebuddy.matcher.ElementMatchers.declaresField;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;

import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannelBuilder;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.util.List;
Expand Down Expand Up @@ -47,11 +47,9 @@ public static class AddInterceptorAdvice {
public static void addInterceptor(
@Advice.This ManagedChannelBuilder<?> builder,
@Advice.FieldValue("interceptors") List<ClientInterceptor> interceptors) {
VirtualField<ManagedChannelBuilder<?>, Boolean> instrumented =
VirtualField.find(ManagedChannelBuilder.class, Boolean.class);
if (!Boolean.TRUE.equals(instrumented.get(builder))) {
if (!Boolean.TRUE.equals(MANAGED_CHANNEL_BUILDER_INSTRUMENTED.get(builder))) {
interceptors.add(0, GrpcSingletons.CLIENT_INTERCEPTOR);
instrumented.set(builder, true);
MANAGED_CHANNEL_BUILDER_INSTRUMENTED.set(builder, true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

Expand Down Expand Up @@ -41,15 +42,11 @@ public static Context.Storage onEnter() {
return GrpcSingletons.getStorage();
}

@AssignReturned.ToReturned
@Advice.OnMethodExit
public static void onExit(
@Advice.Return(readOnly = false) Context.Storage storage,
@Advice.Enter Context.Storage ourStorage) {
if (ourStorage != null) {
storage = ourStorage;
} else {
storage = GrpcSingletons.setStorage(storage);
}
public static Context.Storage onExit(
@Advice.Return Context.Storage originalStorage, @Advice.Enter Context.Storage ourStorage) {
return ourStorage != null ? ourStorage : GrpcSingletons.setStorage(originalStorage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class GrpcInstrumentationModule extends InstrumentationModule {
public class GrpcInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {
public GrpcInstrumentationModule() {
super("grpc", "grpc-1.6");
}
Expand All @@ -25,4 +27,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
new GrpcContextInstrumentation(),
new GrpcServerBuilderInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.instrumentation.grpc.v1_6.GrpcSingletons.SERVER_BUILDER_INSTRUMENTED;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import io.grpc.ServerBuilder;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.CallDepth;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand Down Expand Up @@ -44,22 +44,20 @@ public void transform(TypeTransformer transformer) {
public static class BuildAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.This ServerBuilder<?> serverBuilder,
@Advice.Local("otelCallDepth") CallDepth callDepth) {
callDepth = CallDepth.forClass(ServerBuilder.class);
if (callDepth.getAndIncrement() == 0) {
VirtualField<ServerBuilder<?>, Boolean> instrumented =
VirtualField.find(ServerBuilder.class, Boolean.class);
if (!Boolean.TRUE.equals(instrumented.get(serverBuilder))) {
serverBuilder.intercept(GrpcSingletons.SERVER_INTERCEPTOR);
instrumented.set(serverBuilder, true);
}
public static CallDepth onEnter(@Advice.This ServerBuilder<?> serverBuilder) {
CallDepth callDepth = CallDepth.forClass(ServerBuilder.class);
if (callDepth.getAndIncrement() > 0) {
return callDepth;
}
if (!Boolean.TRUE.equals(SERVER_BUILDER_INSTRUMENTED.get(serverBuilder))) {
serverBuilder.intercept(GrpcSingletons.SERVER_INTERCEPTOR);
SERVER_BUILDER_INSTRUMENTED.set(serverBuilder, true);
}
return callDepth;
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(@Advice.Local("otelCallDepth") CallDepth callDepth) {
public static void onExit(@Advice.Enter CallDepth callDepth) {
callDepth.decrementAndGet();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

import io.grpc.ClientInterceptor;
import io.grpc.Context;
import io.grpc.ManagedChannelBuilder;
import io.grpc.ServerBuilder;
import io.grpc.ServerInterceptor;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.instrumentation.grpc.v1_6.GrpcTelemetry;
import io.opentelemetry.instrumentation.grpc.v1_6.internal.ContextStorageBridge;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
Expand All @@ -20,6 +23,13 @@
// Holds singleton references.
public final class GrpcSingletons {

public static final VirtualField<ManagedChannelBuilder<?>, Boolean>
MANAGED_CHANNEL_BUILDER_INSTRUMENTED =
VirtualField.find(ManagedChannelBuilder.class, Boolean.class);

public static final VirtualField<ServerBuilder<?>, Boolean> SERVER_BUILDER_INSTRUMENTED =
VirtualField.find(ServerBuilder.class, Boolean.class);

public static final ClientInterceptor CLIENT_INTERCEPTOR;

public static final ServerInterceptor SERVER_INTERCEPTOR;
Expand Down
Loading