Skip to content

Commit 7c2e3a1

Browse files
committed
Add code origin support to the grpc server integration
1 parent ac4977a commit 7c2e3a1

File tree

6 files changed

+393
-0
lines changed

6 files changed

+393
-0
lines changed

dd-java-agent/agent-bootstrap/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies {
2121
api project(':internal-api')
2222
api project(':internal-api:internal-api-9')
2323
api project(':dd-java-agent:agent-logging')
24+
api project(':dd-java-agent:agent-debugger:debugger-bootstrap')
2425
api libs.slf4j
2526
// ^ Generally a bad idea for libraries, but we're shadowing.
2627

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/CapturingTestBase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ public static LogProbe.Builder createProbeBuilder(
351351

352352
protected TestSnapshotListener installProbes(
353353
Configuration configuration, ProbeDefinition... probes) {
354+
354355
config = mock(Config.class);
355356
when(config.isDebuggerEnabled()).thenReturn(true);
356357
when(config.isDebuggerClassFileDumpEnabled()).thenReturn(true);

dd-java-agent/instrumentation/grpc-1.5/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ dependencies {
3939
testImplementation group: 'io.grpc', name: 'grpc-protobuf', version: grpcVersion
4040
testImplementation group: 'io.grpc', name: 'grpc-stub', version: grpcVersion
4141
testImplementation group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
42+
testImplementation project(':dd-java-agent:agent-debugger')
43+
testImplementation libs.bundles.mockito
4244

4345
latestDepTestImplementation sourceSets.test.output // include the protobuf generated classes
4446
latestDepTestCompileOnly group: 'io.grpc', name: 'grpc-core', version: '1.+'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package datadog.trace.instrumentation.grpc.server;
2+
3+
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.nameEndsWith;
4+
import static datadog.trace.bootstrap.debugger.spanorigin.CodeOriginInfo.entry;
5+
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
6+
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
7+
8+
import com.google.auto.service.AutoService;
9+
import datadog.trace.agent.tooling.Instrumenter.ForTypeHierarchy;
10+
import datadog.trace.agent.tooling.InstrumenterModule;
11+
import java.lang.reflect.Method;
12+
import net.bytebuddy.asm.Advice;
13+
import net.bytebuddy.description.type.TypeDescription;
14+
import net.bytebuddy.matcher.ElementMatcher;
15+
16+
@AutoService(InstrumenterModule.class)
17+
public class MethodHandlersInstrumentation extends InstrumenterModule.Tracing
18+
implements ForTypeHierarchy {
19+
private static final ElementMatcher<TypeDescription> METHOD_HANDLERS =
20+
nameEndsWith("$MethodHandlers");
21+
22+
public MethodHandlersInstrumentation() {
23+
super("grpc-server-code-origin");
24+
}
25+
26+
@Override
27+
public String hierarchyMarkerType() {
28+
return "io.grpc.MethodDescriptor";
29+
}
30+
31+
@Override
32+
public ElementMatcher<TypeDescription> hierarchyMatcher() {
33+
return METHOD_HANDLERS;
34+
}
35+
36+
@Override
37+
public void methodAdvice(MethodTransformer transformer) {
38+
transformer.applyAdvice(
39+
isConstructor().and(takesArguments(2)),
40+
MethodHandlersInstrumentation.class.getName() + "$BuildAdvice");
41+
}
42+
43+
public static class BuildAdvice {
44+
45+
@Advice.OnMethodEnter(suppress = Throwable.class)
46+
public static void onEnter(@Advice.Argument(0) Object serviceImpl) {
47+
Class<?> serviceClass = serviceImpl.getClass();
48+
Class<?> superclass = serviceClass.getSuperclass();
49+
if (superclass != null) {
50+
for (Method method : superclass.getDeclaredMethods()) {
51+
try {
52+
entry(serviceClass.getDeclaredMethod(method.getName(), method.getParameterTypes()));
53+
} catch (NoSuchMethodException e) {
54+
// service method not overridden on the impl. skipping instrumentation.
55+
}
56+
}
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)