-
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.
Implement code origin support for grpc server entry spans (#7942)
* Add code origin support to the grpc server integration --------- Co-authored-by: Andrea Marziali <[email protected]>
- Loading branch information
1 parent
ad94ba0
commit f7a6771
Showing
6 changed files
with
403 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
70 changes: 70 additions & 0 deletions
70
...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,70 @@ | ||
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 datadog.trace.api.Platform; | ||
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", "grpc-server", "grpc-server-code-origin"); | ||
} | ||
|
||
@Override | ||
public String hierarchyMarkerType() { | ||
return "io.grpc.MethodDescriptor"; | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> hierarchyMatcher() { | ||
return METHOD_HANDLERS; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return super.isEnabled() && !Platform.isGraalVM(); | ||
} | ||
|
||
@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) { | ||
try { | ||
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 (Throwable e) { | ||
// service method not overridden on the impl. skipping instrumentation. | ||
} | ||
} | ||
} | ||
} catch (Throwable e) { | ||
// this should be logged somehow | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.