-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
instrument continuation (un)mounting
- Loading branch information
1 parent
7afb3eb
commit 8fa2800
Showing
6 changed files
with
144 additions
and
4 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
62 changes: 62 additions & 0 deletions
62
...adog/trace/instrumentation/java/concurrent/virtualthread/ContinuationInstrumentation.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,62 @@ | ||
package datadog.trace.instrumentation.java.concurrent.virtualthread; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPrivate; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import datadog.trace.api.Platform; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
import datadog.trace.bootstrap.instrumentation.api.ProfilerContext; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
@AutoService(InstrumenterModule.class) | ||
public class ContinuationInstrumentation extends InstrumenterModule.Tracing | ||
implements Instrumenter.ForBootstrap, Instrumenter.ForSingleType { | ||
public ContinuationInstrumentation() { | ||
super("java_concurrent", "virtual-thread-continuation"); | ||
} | ||
|
||
@Override | ||
public String instrumentedType() { | ||
return "jdk.internal.vm.Continuation"; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return Platform.isJavaVersionAtLeast(19) && super.isEnabled(); | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
transformer.applyAdvice( | ||
isMethod().and(takesArguments(0)).and(isPrivate()).and(named("mount")), | ||
getClass().getName() + "$Mount"); | ||
transformer.applyAdvice( | ||
isMethod().and(takesArguments(0)).and(isPrivate()).and(named("unmount")), | ||
getClass().getName() + "$Unmount"); | ||
} | ||
|
||
public static final class Mount { | ||
@Advice.OnMethodEnter | ||
public static void mount() { | ||
AgentScope scope = AgentTracer.activeScope(); | ||
if (scope != null && scope.span().context() instanceof ProfilerContext) { | ||
AgentTracer.get() | ||
.getProfilingContext() | ||
.setContext((ProfilerContext) scope.span().context()); | ||
} | ||
} | ||
} | ||
|
||
public static final class Unmount { | ||
@Advice.OnMethodExit | ||
public static void unmount() { | ||
AgentTracer.get().getProfilingContext().clearContext(); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...tation/java-concurrent/java-concurrent-21/src/test/groovy/ContinuationMountingTest.groovy
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,38 @@ | ||
import datadog.trace.agent.test.AgentTestRunner | ||
|
||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace | ||
|
||
class ContinuationMountingTest extends AgentTestRunner { | ||
|
||
@Override | ||
protected void configurePreAgent() { | ||
super.configurePreAgent() | ||
injectSysConfig("dd.profiling.enabled", "true") | ||
} | ||
|
||
def "test continuation mounting intercepted"() { | ||
setup: | ||
def spanName = UUID.randomUUID().toString() | ||
def duration = new AtomicLong() | ||
|
||
when: | ||
runUnderTrace(spanName, { | ||
Thread.ofVirtual() | ||
.name("test thread") | ||
.start({ | ||
long start = System.nanoTime() | ||
for (int i = 0; i < 3; i++) { | ||
Thread.sleep(20) | ||
long now = System.nanoTime() | ||
duration.addAndGet(now - start) | ||
start = now | ||
} | ||
}).join(5000) | ||
}) | ||
|
||
then: | ||
TEST_PROFILING_CONTEXT_INTEGRATION.getActivationBalance(spanName) == 0 | ||
} | ||
} |
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