Skip to content

Commit 72b60f4

Browse files
committed
feat(concurrency): Improve JDK 21 structured task scope instrumentation
Add instrumentation alias Limit activation to Java < 25 Fix context store declaration and usage to match RunnableInstrumentation Remove unused generic
1 parent 84ac625 commit 72b60f4

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

dd-java-agent/instrumentation/java-concurrent/java-concurrent-21/src/main/java/datadog/trace/instrumentation/java/concurrent/structuredconcurrency/StructuredTaskScopeInstrumentation.java renamed to dd-java-agent/instrumentation/java-concurrent/java-concurrent-21/src/main/java/datadog/trace/instrumentation/java/concurrent/structuredconcurrency21/StructuredTaskScope21Instrumentation.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
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;
35
import static datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils.capture;
46
import static java.util.Collections.singletonMap;
57
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
68

79
import com.google.auto.service.AutoService;
8-
import datadog.environment.JavaVirtualMachine;
910
import datadog.trace.agent.tooling.Instrumenter;
1011
import datadog.trace.agent.tooling.InstrumenterModule;
1112
import datadog.trace.bootstrap.ContextStore;
12-
import datadog.trace.bootstrap.InstrumentationContext;
1313
import datadog.trace.bootstrap.instrumentation.java.concurrent.State;
1414
import 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
}

dd-smoke-tests/concurrent/java-21/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ plugins {
55

66
ext {
77
minJavaVersionForTests = JavaVersion.VERSION_21
8+
maxJavaVersionForTests = JavaVersion.VERSION_25
89
}
910

1011
apply from: "$rootDir/gradle/java.gradle"

0 commit comments

Comments
 (0)