-
Notifications
You must be signed in to change notification settings - Fork 331
Improve VirtualThread context tracking instrumentation #11009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f52825e
fix(virtual-thread): Prevent duplicate instrumentation conflict betwe…
PerfectSlayer fdb3c9f
fix(virtual-thread): Fix duplicate instrumentation of afterDone
PerfectSlayer d0e39d9
feat(virtual-thread): Use context swapping instead of scope activation
PerfectSlayer 45d06be
feat(virtual-thread): Use context swapping instead of scope activation
PerfectSlayer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 5 additions & 4 deletions
9
.../src/main/java/datadog/trace/bootstrap/instrumentation/java/lang/VirtualThreadHelper.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| package datadog.trace.bootstrap.instrumentation.java.lang; | ||
|
|
||
| /** This class is a helper for the java-lang-21.0 {@code VirtualThreadInstrumentation}. */ | ||
| public final class VirtualThreadHelper { | ||
| public static final String VIRTUAL_THREAD_CLASS_NAME = "java.lang.VirtualThread"; | ||
|
|
||
| /** | ||
| * {@link datadog.trace.bootstrap.instrumentation.api.AgentScope} class name as string literal. | ||
| * This is mandatory for {@link datadog.trace.bootstrap.ContextStore} API call. | ||
| * {@link VirtualThreadState} class name as string literal. This is mandatory for {@link | ||
| * datadog.trace.bootstrap.ContextStore} API call. | ||
| */ | ||
| public static final String AGENT_SCOPE_CLASS_NAME = | ||
| "datadog.trace.bootstrap.instrumentation.api.AgentScope"; | ||
| public static final String VIRTUAL_THREAD_STATE_CLASS_NAME = | ||
| "datadog.trace.bootstrap.instrumentation.java.lang.VirtualThreadState"; | ||
| } |
46 changes: 46 additions & 0 deletions
46
...p/src/main/java/datadog/trace/bootstrap/instrumentation/java/lang/VirtualThreadState.java
This file contains hidden or 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,46 @@ | ||
| package datadog.trace.bootstrap.instrumentation.java.lang; | ||
|
|
||
| import datadog.context.Context; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentScope.Continuation; | ||
|
|
||
| /** | ||
| * This class holds the saved context and scope continuation for a virtual thread. | ||
| * | ||
| * <p>Used by java-lang-21.0 {@code VirtualThreadInstrumentation} to swap the entire scope stack on | ||
| * mount/unmount. | ||
| */ | ||
| public final class VirtualThreadState { | ||
| /** The virtual thread's saved context (scope stack snapshot). */ | ||
| private Context context; | ||
|
|
||
| /** Prevents the enclosing context scope from completing before the virtual thread finishes. */ | ||
| private final Continuation continuation; | ||
|
|
||
| /** The carrier thread's saved context, set between mount and unmount. */ | ||
| private Context previousContext; | ||
|
|
||
| public VirtualThreadState(Context context, Continuation continuation) { | ||
| this.context = context; | ||
| this.continuation = continuation; | ||
| } | ||
|
|
||
| /** Called on mount: swaps the virtual thread's context into the carrier thread. */ | ||
| public void onMount() { | ||
| this.previousContext = this.context.swap(); | ||
| } | ||
|
|
||
| /** Called on unmount: restores the carrier thread's original context. */ | ||
| public void onUnmount() { | ||
| if (this.previousContext != null) { | ||
| this.context = this.previousContext.swap(); | ||
| this.previousContext = null; | ||
| } | ||
| } | ||
|
|
||
| /** Called on termination: releases the trace continuation. */ | ||
| public void onTerminate() { | ||
| if (this.continuation != null) { | ||
| this.continuation.cancel(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, that's the kind of API that I was imagining