Skip to content

Add context capture controls and preserve flags across context switches - #13311

Draft
mcculls wants to merge 1 commit into
mcculls/context-attach-reset-asyncfrom
mcculls/context-capture-flag
Draft

mcculls wants to merge 1 commit into
mcculls/context-attach-reset-asyncfrom
mcculls/context-capture-flag

Conversation

@mcculls

@mcculls mcculls commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

What Does This Do

Add capture controls and checkpoint handles so context switches preserve execution-unit flags. Attaching or resuming a different context enables capture; closing the scope restores the previous flags. Reattaching the current context leaves flags unchanged.

swap() now returns a ContextHandle rather than a Context. Checkpoints save flags without keeping a trace open; continuations retain that responsibility.

Motivation

Prepare instrumentation callers to migrate from legacy async-propagation controls to the Context API while keeping existing scope-stack behavior compatible. Capture suppression and context installation remain separate, so coroutine and fiber scheduling can preserve the context even when continuation capture is disabled.

Additional Notes

Stacked on #13310; targets mcculls/context-attach-reset-async.

Coroutines and ZIO fibers preserve the launching flow's flags. Virtual threads start with capture enabled and preserve their own flag changes across remounts. Akka/Pekko HTTP handlers and OTel manual attachments preserve surrounding capture suppression.

The legacy adapter seeds independent scope stacks for initial coroutine/fiber installation and restores the caller's live scope boundary when switching back on the same thread. This lets existing AWS checkpoint callers restore their original scopes.

@mcculls mcculls added type: feature Enhancements and improvements tag: no release notes Changes to exclude from release notes comp: context propagation Trace context propagation tag: ai generated Largely based on code generated by an AI or LLM labels Nov 9, 2026
@datadog-prod-us1-5

datadog-prod-us1-5 Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 40.38%

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 99f3227 | Docs | Give us feedback!

@dd-octo-sts

dd-octo-sts Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.05 s 14.00 s [-0.3%; +1.0%] (no difference)
startup:insecure-bank:tracing:Agent 12.92 s 12.99 s [-1.1%; -0.0%] (maybe better)
startup:petclinic:appsec:Agent 17.78 s 17.54 s [+0.3%; +2.4%] (maybe worse)
startup:petclinic:iast:Agent 17.50 s 17.10 s [-2.3%; +6.9%] (no difference)
startup:petclinic:profiling:Agent 17.43 s 17.26 s [-0.1%; +2.1%] (no difference)
startup:petclinic:sca:Agent 17.80 s 17.68 s [-0.6%; +1.9%] (no difference)
startup:petclinic:tracing:Agent 16.64 s 16.86 s [-2.3%; -0.3%] (maybe better)

Commit: 34527ff2 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

@mcculls
mcculls force-pushed the mcculls/context-capture-flag branch 2 times, most recently from 9fe310d to b54643f Compare November 12, 2026 12:21
@mcculls
mcculls added this pull request to stack #13397 November 12, 2026 12:21
@pr-commenter

pr-commenter Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Debugger benchmarks

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
ci_job_date 1794780400 1794780727
end_time 2026-11-15T22:08:08 2026-11-15T22:13:35
git_branch master mcculls/context-capture-flag
git_commit_sha e8e4235 34527ff
start_time 2026-11-15T22:06:41 2026-11-15T22:12:08
See matching parameters
Baseline Candidate
ci_job_id 2255929431 2255929431
ci_pipeline_id 153369328 153369328
cpu_model Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
git_commit_date 1794779638 1794779638

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 9 metrics, 6 unstable metrics.

See unchanged results
scenario Δ mean agg_http_req_duration_min Δ mean agg_http_req_duration_p50 Δ mean agg_http_req_duration_p75 Δ mean agg_http_req_duration_p99 Δ mean throughput
scenario:noprobe unstable
[-34.812µs; +17.783µs] or [-10.848%; +5.542%]
unstable
[-47.816µs; +29.039µs] or [-13.439%; +8.162%]
unstable
[-59.823µs; +37.796µs] or [-16.111%; +10.179%]
unstable
[-66.599µs; +153.857µs] or [-5.502%; +12.711%]
same
scenario:basic same same same unstable
[-112.793µs; +170.102µs] or [-10.576%; +15.949%]
unstable
[-70.759op/s; +176.467op/s] or [-3.113%; +7.765%]
scenario:loop same same same same same
Request duration reports for reports
gantt
    title reports - request duration [CI 0.99] : candidate=None, baseline=None
    dateFormat X
    axisFormat %s
section baseline
noprobe (355.797 µs) : 311, 401
.   : milestone, 356,
basic (328.693 µs) : 324, 334
.   : milestone, 329,
loop (8.097 ms) : 8034, 8160
.   : milestone, 8097,
section candidate
noprobe (346.408 µs) : 321, 371
.   : milestone, 346,
basic (328.987 µs) : 324, 334
.   : milestone, 329,
loop (8.099 ms) : 8037, 8162
.   : milestone, 8099,
Loading
  • baseline results
Scenario Request median duration [CI 0.99]
noprobe 355.797 µs [310.625 µs, 400.969 µs]
basic 328.693 µs [323.535 µs, 333.851 µs]
loop 8.097 ms [8.034 ms, 8.16 ms]
  • candidate results
Scenario Request median duration [CI 0.99]
noprobe 346.408 µs [321.397 µs, 371.42 µs]
basic 328.987 µs [323.806 µs, 334.168 µs]
loop 8.099 ms [8.037 ms, 8.162 ms]

@mcculls
mcculls force-pushed the mcculls/context-capture-flag branch 3 times, most recently from 37cdab0 to 34527ff Compare November 15, 2026 21:59
Introduce Context capture controls as the migration target for legacy
async-propagation callers, retaining scope-stack-backed behavior through
the legacy adapter. Disabled capture returns a no-op continuation;
direct context installation remains possible. Attaching or resuming a
different context resets flags, enabling capture; closing its scope
restores the previous flags. Reattaching the current context leaves
flags unchanged.

Introduce ContextHandle and checkpoint() to save a context and
execution-unit flags without changing the active context or keeping a
trace open. Move swap() to ContextHandle and return a ContextHandle
rather than a Context; the legacy ScopeContext now implements
ContextHandle instead of Context. Swapping to a different context resets
flags or restores those saved in a checkpoint. The returned handle
checkpoints the displaced context. Same-context swaps leave flags
unchanged.

Use checkpoint handles across coroutine, fiber, actor, and poller
context switches. Coroutines and ZIO fibers continue the launching
logical flow, so their non-legacy initial checkpoints preserve its flags
across scheduling boundaries. Legacy-mode checkpoints seed independent
scope stacks for initial installation and restore live scope boundaries
when returning to a displaced caller on the same thread. Their
continuations retain the trace when capture is enabled. Virtual threads
start new execution units, so they begin with a raw context and capture
enabled, independently of temporary suppression in the creator. Flags
set within the virtual thread persist across remounts.

Make Akka and Pekko HTTP handlers inherit the enclosing flow's capture
flag. Use the Context capture controls in the OpenTelemetry shim so
manual attachments also preserve suppression through the selected
context manager.

Cover capture suppression, attach and resume flag resets, scope
restoration, checkpoints, cross-thread context switches, and custom
context providers. Add agent-backed virtual-thread coverage for launch
from enabled and suppressed parents, flag preservation across remounts,
and isolation between parent and child flags. Update context tracking
guidance and benchmarks for the expanded API.
@mcculls
mcculls force-pushed the mcculls/context-capture-flag branch from 360e090 to 99f3227 Compare November 16, 2026 23:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: context propagation Trace context propagation tag: ai generated Largely based on code generated by an AI or LLM tag: no release notes Changes to exclude from release notes type: feature Enhancements and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant