-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code origin support to the grpc server integration
- Loading branch information
1 parent
ac4977a
commit 7c2e3a1
Showing
6 changed files
with
393 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...rc/main/java/datadog/trace/instrumentation/grpc/server/MethodHandlersInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package datadog.trace.instrumentation.grpc.server; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.nameEndsWith; | ||
import static datadog.trace.bootstrap.debugger.spanorigin.CodeOriginInfo.entry; | ||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter.ForTypeHierarchy; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import java.lang.reflect.Method; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(InstrumenterModule.class) | ||
public class MethodHandlersInstrumentation extends InstrumenterModule.Tracing | ||
implements ForTypeHierarchy { | ||
private static final ElementMatcher<TypeDescription> METHOD_HANDLERS = | ||
nameEndsWith("$MethodHandlers"); | ||
|
||
public MethodHandlersInstrumentation() { | ||
super("grpc-server-code-origin"); | ||
} | ||
|
||
@Override | ||
public String hierarchyMarkerType() { | ||
return "io.grpc.MethodDescriptor"; | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> hierarchyMatcher() { | ||
return METHOD_HANDLERS; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
transformer.applyAdvice( | ||
isConstructor().and(takesArguments(2)), | ||
MethodHandlersInstrumentation.class.getName() + "$BuildAdvice"); | ||
} | ||
|
||
public static class BuildAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter(@Advice.Argument(0) Object serviceImpl) { | ||
Class<?> serviceClass = serviceImpl.getClass(); | ||
Class<?> superclass = serviceClass.getSuperclass(); | ||
if (superclass != null) { | ||
for (Method method : superclass.getDeclaredMethods()) { | ||
try { | ||
entry(serviceClass.getDeclaredMethod(method.getName(), method.getParameterTypes())); | ||
} catch (NoSuchMethodException e) { | ||
// service method not overridden on the impl. skipping instrumentation. | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.