1- package datadog .trace .instrumentation .java .concurrent .structuredconcurrency ;
1+ package datadog .trace .instrumentation .java .concurrent .structuredconcurrency21 ;
22
3+ import static datadog .environment .JavaVirtualMachine .isJavaVersionBetween ;
4+ import static datadog .trace .bootstrap .InstrumentationContext .get ;
35import static datadog .trace .bootstrap .instrumentation .java .concurrent .AdviceUtils .capture ;
46import static java .util .Collections .singletonMap ;
57import static net .bytebuddy .matcher .ElementMatchers .isConstructor ;
68
79import com .google .auto .service .AutoService ;
8- import datadog .environment .JavaVirtualMachine ;
910import datadog .trace .agent .tooling .Instrumenter ;
1011import datadog .trace .agent .tooling .InstrumenterModule ;
1112import datadog .trace .bootstrap .ContextStore ;
12- import datadog .trace .bootstrap .InstrumentationContext ;
1313import datadog .trace .bootstrap .instrumentation .java .concurrent .State ;
1414import java .util .Map ;
15- import net .bytebuddy .asm .Advice ;
15+ import net .bytebuddy .asm .Advice .OnMethodExit ;
16+ import net .bytebuddy .asm .Advice .This ;
1617
1718/**
1819 * This instrumentation captures the active span scope at StructuredTaskScope task creation
19- * (SubtaskImpl). The scope is then activate and close through the Runnable instrumentation
20- * (SubtaskImpl implementation Runnable).
20+ * (SubtaskImpl). The scope is then activate and close through the {@link Runnable} instrumentation
21+ * (SubtaskImpl implementing {@link Runnable} ).
2122 */
2223@ SuppressWarnings ("unused" )
2324@ AutoService (InstrumenterModule .class )
24- public class StructuredTaskScopeInstrumentation extends InstrumenterModule .Tracing
25+ public class StructuredTaskScope21Instrumentation extends InstrumenterModule .Tracing
2526 implements Instrumenter .ForBootstrap , Instrumenter .ForSingleType , Instrumenter .HasMethodAdvice {
2627
27- public StructuredTaskScopeInstrumentation () {
28- super ("java_concurrent" , "structured_task_scope " );
28+ public StructuredTaskScope21Instrumentation () {
29+ super ("java_concurrent" , "structured-task-scope" , "structured-task-scope-21 " );
2930 }
3031
3132 @ Override
@@ -35,13 +36,12 @@ public String instrumentedType() {
3536
3637 @ Override
3738 public boolean isEnabled () {
38- return JavaVirtualMachine . isJavaVersionAtLeast (21 ) && super .isEnabled ();
39+ return isJavaVersionBetween (21 , 25 ) && super .isEnabled ();
3940 }
4041
4142 @ Override
4243 public Map <String , String > contextStore () {
43- return singletonMap (
44- "java.util.concurrent.StructuredTaskScope.SubtaskImpl" , State .class .getName ());
44+ return singletonMap (Runnable .class .getName (), State .class .getName ());
4545 }
4646
4747 @ Override
@@ -50,15 +50,17 @@ public void methodAdvice(MethodTransformer transformer) {
5050 }
5151
5252 public static final class ConstructorAdvice {
53- @ Advice .OnMethodExit
54- public static <T > void captureScope (
55- @ Advice .This Object task // StructuredTaskScope.SubtaskImpl (can't use the type)
56- ) {
57- ContextStore <Object , State > contextStore =
58- InstrumentationContext .get (
59- "java.util.concurrent.StructuredTaskScope.SubtaskImpl" ,
60- "datadog.trace.bootstrap.instrumentation.java.concurrent.State" );
61- capture (contextStore , task );
53+ /**
54+ * Captures task scope to be restored at the start of VirtualThread.run() method by {@link
55+ * Runnable} instrumentation.
56+ *
57+ * @param subTaskImpl The StructuredTaskScope.SubtaskImpl object (the advice are compile
58+ * against Java 8 so the type from JDK25 can't be referred, using {@link Object} instead
59+ */
60+ @ OnMethodExit (suppress = Throwable .class )
61+ public static void captureScope (@ This Object subTaskImpl ) {
62+ ContextStore <Runnable , State > contextStore = get (Runnable .class , State .class );
63+ capture (contextStore , (Runnable ) subTaskImpl );
6264 }
6365 }
6466}
0 commit comments