|
| 1 | +package datadog.trace.instrumentation.java.concurrent.structuredconcurrency; |
| 2 | + |
| 3 | +import static datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils.capture; |
| 4 | +import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.ExcludeType.FORK_JOIN_TASK; |
| 5 | +import static java.util.Collections.singleton; |
| 6 | +import static java.util.Collections.singletonMap; |
| 7 | +import static net.bytebuddy.matcher.ElementMatchers.isConstructor; |
| 8 | + |
| 9 | +import com.google.auto.service.AutoService; |
| 10 | +import datadog.trace.agent.tooling.ExcludeFilterProvider; |
| 11 | +import datadog.trace.agent.tooling.Instrumenter; |
| 12 | +import datadog.trace.agent.tooling.InstrumenterModule; |
| 13 | +import datadog.trace.api.Platform; |
| 14 | +import datadog.trace.bootstrap.ContextStore; |
| 15 | +import datadog.trace.bootstrap.InstrumentationContext; |
| 16 | +import datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter; |
| 17 | +import datadog.trace.bootstrap.instrumentation.java.concurrent.State; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Map; |
| 20 | +import net.bytebuddy.asm.Advice; |
| 21 | + |
| 22 | +/** |
| 23 | + * This instrumentation captures the active span scope at StructuredTaskScope task creation |
| 24 | + * (SubtaskImpl). The scope is then activate and close through the Runnable instrumentation |
| 25 | + * (SubtaskImpl implementation Runnable). |
| 26 | + */ |
| 27 | +@SuppressWarnings("unused") |
| 28 | +@AutoService(InstrumenterModule.class) |
| 29 | +public class StructuredTaskScopeInstrumentation extends InstrumenterModule.Tracing |
| 30 | + implements Instrumenter.ForBootstrap, Instrumenter.ForSingleType, ExcludeFilterProvider { |
| 31 | + |
| 32 | + public StructuredTaskScopeInstrumentation() { |
| 33 | + super("java_concurrent", "structured_task_scope"); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public String instrumentedType() { |
| 38 | + return "java.util.concurrent.StructuredTaskScope$SubtaskImpl"; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public boolean isEnabled() { |
| 43 | + return Platform.isJavaVersionAtLeast(21) && super.isEnabled(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Map<String, String> contextStore() { |
| 48 | + return singletonMap( |
| 49 | + "java.util.concurrent.StructuredTaskScope.SubtaskImpl", State.class.getName()); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void methodAdvice(MethodTransformer transformer) { |
| 54 | + transformer.applyAdvice(isConstructor(), getClass().getName() + "$ConstructorAdvice"); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Map<ExcludeFilter.ExcludeType, ? extends Collection<String>> excludedClasses() { |
| 59 | + // Prevent the ForkJoinPool instrumentation to enable the task scope too early on the carrier |
| 60 | + // thread rather than on the expected running thread, which is virtual by default. |
| 61 | + return singletonMap( |
| 62 | + FORK_JOIN_TASK, singleton("java.util.concurrent.ForkJoinTask$RunnableExecuteAction")); |
| 63 | + } |
| 64 | + |
| 65 | + public static final class ConstructorAdvice { |
| 66 | + @Advice.OnMethodExit |
| 67 | + public static <T> void captureScope( |
| 68 | + @Advice.This Object task // StructuredTaskScope.SubtaskImpl (can't use the type) |
| 69 | + ) { |
| 70 | + ContextStore<Object, State> contextStore = |
| 71 | + InstrumentationContext.get( |
| 72 | + "java.util.concurrent.StructuredTaskScope.SubtaskImpl", |
| 73 | + "datadog.trace.bootstrap.instrumentation.java.concurrent.State"); |
| 74 | + capture(contextStore, task, true); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments