Repository navigation
Fix context propagation for delayed Netty scheduled task #12541
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
gh-worker-dd-mergequeue-cf854d
merged 14 commits into
master
from
andrea.marziali/fix-netty-scheduled-future-context
Sep 28, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
24279fb
Move scheduled Netty task handling to Netty instrumentation
amarziali 572e99c
redisign
amarziali a5ba81c
correctness and clean up comments
amarziali 3a9cabf
use the right guard
amarziali b4c1280
Make the guard consistent
amarziali 79cb926
Merge remote-tracking branch 'origin/master' into andrea.marziali/fix…
amarziali ab033b9
Preserve Netty scheduling context through completion listeners
amarziali d4ddefc
Add netty concurrent as runtime test depedency to dependent modules
amarziali 503e80d
cleanup codeowners
amarziali 4a6fa5b
minimize module impacts
amarziali 9152ef3
Add suppression for local session store continuation and test
amarziali 949a8bb
more doc
amarziali 6a50c3a
spotless
amarziali f6f1f44
improve comments
amarziali 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
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
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
116 changes: 116 additions & 0 deletions
116
...ava/datadog/trace/instrumentation/netty40/concurrent/NettyPromiseTaskInstrumentation.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,116 @@ | ||
| package datadog.trace.instrumentation.netty40.concurrent; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.declaresMethod; | ||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.extendsClass; | ||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.hasSuperType; | ||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface; | ||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.nameEndsWith; | ||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils.endTaskScope; | ||
| import static datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils.startTaskScope; | ||
| import static java.util.Collections.singletonMap; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.context.ContextScope; | ||
| 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 java.util.concurrent.RunnableFuture; | ||
| import java.util.concurrent.ScheduledFuture; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
| public final class NettyPromiseTaskInstrumentation extends InstrumenterModule.ContextTracking | ||
| implements Instrumenter.ForTypeHierarchy, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public NettyPromiseTaskInstrumentation() { | ||
| // Share both names with RunnableFutureInstrumentation because it captures continuations that | ||
| // this instrumentation activates for scheduled tasks; either name must enable both halves. | ||
| super("java_concurrent", "runnable-future"); | ||
| } | ||
|
|
||
| @Override | ||
| public String hierarchyMarkerType() { | ||
| return RunnableFuture.class.getName(); | ||
| } | ||
|
|
||
| @Override | ||
| public ElementMatcher<TypeDescription> hierarchyMatcher() { | ||
| return extendsClass(nameEndsWith(".netty.util.concurrent.PromiseTask")) | ||
| .and(implementsInterface(named(RunnableFuture.class.getName()))) | ||
| .and(hasSuperType(declaresMethod(named("runTask")))); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> contextStore() { | ||
| return singletonMap("java.util.concurrent.RunnableFuture", State.class.getName()); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] {packageName + ".ScheduledTaskScope"}; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| isMethod().and(named("runTask")).and(takesNoArguments()), | ||
| getClass().getName() + "$RunTask"); | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(named("run")) | ||
| .and(takesNoArguments()) | ||
| .and(isDeclaredBy(nameEndsWith(".netty.util.concurrent.ScheduledFutureTask"))), | ||
| getClass().getName() + "$Run"); | ||
| } | ||
|
|
||
| public static final class RunTask { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void activate(@Advice.This RunnableFuture<?> task) { | ||
| if (task instanceof ScheduledFuture) { | ||
| ContextScope scope = | ||
| startTaskScope(InstrumentationContext.get(RunnableFuture.class, State.class), task); | ||
| if (scope != null) { | ||
| ScheduledTaskScope state = ScheduledTaskScope.CURRENT.get(); | ||
| if (state == null) { | ||
| state = new ScheduledTaskScope(); | ||
| ScheduledTaskScope.CURRENT.set(state); | ||
| } | ||
| state.scope = scope; | ||
| state.depth++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static final class Run { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static ContextScope before(@Advice.Local("depth") int depth) { | ||
| ScheduledTaskScope state = ScheduledTaskScope.CURRENT.get(); | ||
| if (state != null) { | ||
| depth = state.depth; | ||
| return state.scope; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
|
amarziali marked this conversation as resolved.
|
||
| public static void after( | ||
| @Advice.Enter ContextScope previous, @Advice.Local("depth") int depth) { | ||
| ScheduledTaskScope state = ScheduledTaskScope.CURRENT.get(); | ||
| if (state != null && state.depth > depth) { | ||
| ContextScope scope = state.scope; | ||
| state.scope = previous; | ||
| state.depth = depth; | ||
| endTaskScope(scope); | ||
| } | ||
| } | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
....0/src/main/java/datadog/trace/instrumentation/netty40/concurrent/ScheduledTaskScope.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,12 @@ | ||
| package datadog.trace.instrumentation.netty40.concurrent; | ||
|
|
||
| import datadog.context.ContextScope; | ||
|
|
||
| /** Transfers the runTask() scope to run() so completion listeners retain the task context. */ | ||
| public final class ScheduledTaskScope { | ||
| public static final ThreadLocal<ScheduledTaskScope> CURRENT = new ThreadLocal<>(); | ||
|
|
||
| public ContextScope scope; | ||
| // Track activations separately: resuming the same context can reuse the outer scope instance. | ||
| public int depth; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.