-
Notifications
You must be signed in to change notification settings - Fork 347
fix(okhttp): capture scope at AsyncCall.<init> to keep traces intact under virtual-thread Dispatcher
#11479
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
Open
dougqh
wants to merge
8
commits into
master
Choose a base branch
from
fix/okhttp-virtual-thread-dispatcher-trace-contamination
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(okhttp): capture scope at AsyncCall.<init> to keep traces intact under virtual-thread Dispatcher
#11479
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
58311a9
fix(okhttp): capture scope at AsyncCall.<init> to keep traces intact …
dougqh 84979ca
fix(okhttp): address review feedback on virtual-thread dispatcher tes…
dougqh 269f42c
fix(okhttp): split AsyncCall scope capture by major version, verify 4…
dougqh 3418718
Merge branch 'master' into fix/okhttp-virtual-thread-dispatcher-trace…
dougqh fbcd0a1
Add okhttp-4 integration to supported-configurations.json
dougqh a1adf8e
Address review feedback on okhttp AsyncCall instrumentation
dougqh d17f523
Restore JDK 24 cap on okhttp vthread suites (CI stability)
dougqh 664f90c
Add deterministic promotion-from-finished() contamination test
dougqh 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
77 changes: 77 additions & 0 deletions
77
...ttp-3.0/src/main/java/datadog/trace/instrumentation/okhttp3/AsyncCallInstrumentation.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,77 @@ | ||
| package datadog.trace.instrumentation.okhttp3; | ||
|
|
||
| import static datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils.capture; | ||
| import static java.util.Collections.singletonMap; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.bootstrap.InstrumentationContext; | ||
| import datadog.trace.bootstrap.instrumentation.java.concurrent.State; | ||
| import java.util.Map; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| /** | ||
| * Captures the active scope at {@code AsyncCall.<init>} (i.e. the moment {@code | ||
| * Call.enqueue(callback)} was invoked on the user's thread) and stores it in the {@code | ||
| * ContextStore<Runnable, State>} shared with the rest of the concurrent instrumentation. {@code | ||
| * RunnableInstrumentation} then re-activates that scope on {@code AsyncCall.run()} entry, which | ||
| * overrides whatever scope {@code TaskRunner.run()} (or {@code beforeExecute}) put in place from | ||
| * the dispatcher's worker thread. | ||
| * | ||
| * <p>Without this, {@code TaskRunnerInstrumentation} captures whatever scope happens to be active | ||
| * on the worker thread when {@code Dispatcher.promoteAndExecute()} dequeues and submits the call — | ||
| * and when promotion runs from inside {@code Dispatcher.finished()} (i.e. recursively from a | ||
| * <em>different</em> AsyncCall's run()), that scope belongs to the finishing call, not to the | ||
| * caller who actually enqueued this AsyncCall. Result: under concurrent OkHttp load, {@code | ||
| * okhttp.request} spans cross-contaminate between traces. | ||
| * | ||
| * <p>{@code AsyncCall} is an inner class of {@code RealCall} and transitively implements {@link | ||
| * Runnable}. This module targets OkHttp 3.x, where it lives at {@code okhttp3.RealCall$AsyncCall}. | ||
| * OkHttp 4.x+ relocated it to {@code okhttp3.internal.connection.RealCall$AsyncCall}, which is | ||
| * handled by the separate {@code okhttp-4.0} module. | ||
| */ | ||
| @AutoService(InstrumenterModule.class) | ||
| public final class AsyncCallInstrumentation extends InstrumenterModule.ContextTracking | ||
| implements Instrumenter.ForKnownTypes, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public AsyncCallInstrumentation() { | ||
| // Re-use the existing "okhttp" / "okhttp-3" instrumentation names so we don't introduce a | ||
| // separately-toggleable feature flag (DD_TRACE_OKHTTP_ASYNC_CALL_ENABLED). The capture here | ||
| // is conceptually part of the OkHttp instrumentation — if you disable OkHttp tracing, you | ||
| // also disable this capture, which is the right behavior. | ||
| // | ||
| // This is a ContextTracking module (like RunnableInstrumentation, which consumes the state we | ||
| // write) rather than a Tracing module: its sole job is to propagate the captured scope through | ||
| // the shared ContextStore<Runnable, State>, not to create spans of its own. | ||
|
dougqh marked this conversation as resolved.
Outdated
|
||
| super("okhttp", "okhttp-3"); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] knownMatchingTypes() { | ||
| return new String[] { | ||
| "okhttp3.RealCall$AsyncCall", // OkHttp 3.x | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> contextStore() { | ||
| // Same Runnable -> State store that RunnableInstrumentation reads from. | ||
| return singletonMap("java.lang.Runnable", State.class.getName()); | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice(isConstructor(), getClass().getName() + "$Construct"); | ||
| } | ||
|
|
||
| public static final class Construct { | ||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void captureScope(@Advice.This Runnable asyncCall) { | ||
| // AdviceUtils.capture is a no-op when async propagation is disabled or there's no active | ||
| // span — same behavior as the rest of the concurrent instrumentation. | ||
| capture(InstrumentationContext.get(Runnable.class, State.class), asyncCall); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.