Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch gRPC instrumentation to instrument public ServerBuilder class. #1839

Merged
merged 4 commits into from
Dec 7, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 1 addition & 7 deletions instrumentation/grpc-1.5/javaagent/grpc-1.5-javaagent.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ muzzle {
pass {
group = "io.grpc"
module = "grpc-core"
versions = "[1.5.0, 1.33.0)" // see https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/1453
versions = "[1.5.0,)"
}
}

Expand All @@ -35,11 +35,5 @@ dependencies {
testLibrary group: 'io.grpc', name: 'grpc-protobuf', version: grpcVersion
testLibrary group: 'io.grpc', name: 'grpc-stub', version: grpcVersion

// see https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/1453
latestDepTestLibrary group: 'io.grpc', name: 'grpc-core', version: '1.32.+'
latestDepTestLibrary group: 'io.grpc', name: 'grpc-netty', version: '1.32.+'
latestDepTestLibrary group: 'io.grpc', name: 'grpc-protobuf', version: '1.32.+'
latestDepTestLibrary group: 'io.grpc', name: 'grpc-stub', version: '1.32.+'

testImplementation project(':instrumentation:grpc-1.5:testing')
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

package io.opentelemetry.javaagent.instrumentation.grpc.v1_5;

import static java.util.Collections.singletonMap;
import static io.opentelemetry.javaagent.tooling.ClassLoaderMatcher.hasClassesNamed;
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.safeHasSuperType;
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.ServerInterceptor;
import io.grpc.ServerBuilder;
import io.opentelemetry.instrumentation.grpc.v1_5.server.TracingServerInterceptor;
import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import java.util.List;
import java.util.Collections;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
Expand All @@ -21,33 +25,36 @@

public class GrpcServerBuilderInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<ClassLoader> classLoaderOptimization() {
return hasClassesNamed("io.grpc.ServerBuilder");
}

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("io.grpc.internal.AbstractServerImplBuilder");
return safeHasSuperType(named("io.grpc.ServerBuilder"));
}

@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
isMethod().and(named("build")),
GrpcServerBuilderInstrumentation.class.getName() + "$AddInterceptorAdvice");
return Collections.singletonMap(
isMethod().and(isPublic()).and(named("build")).and(takesArguments(0)),
GrpcServerBuilderInstrumentation.class.getName() + "$BuildAdvice");
}

public static class AddInterceptorAdvice {
public static class BuildAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void addInterceptor(
@Advice.FieldValue("interceptors") List<ServerInterceptor> interceptors) {
boolean shouldRegister = true;
for (ServerInterceptor interceptor : interceptors) {
if (interceptor instanceof TracingServerInterceptor) {
shouldRegister = false;
break;
}
}
if (shouldRegister) {
interceptors.add(TracingServerInterceptor.newInterceptor());
public static void onEnter(@Advice.This ServerBuilder<?> serverBuilder) {
int callDepth = CallDepthThreadLocalMap.incrementCallDepth(ServerBuilder.class);
if (callDepth == 0) {
serverBuilder.intercept(TracingServerInterceptor.newInterceptor());
}
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(@Advice.This ServerBuilder<?> serverBuilder) {
CallDepthThreadLocalMap.decrementCallDepth(ServerBuilder.class);
}
}
}