Skip to content

Rework shim between ContextManager and the legacy scope manager#11938

Open
mcculls wants to merge 1 commit into
masterfrom
mcculls/rework-legacy-ctx-mgr-shim
Open

Rework shim between ContextManager and the legacy scope manager#11938
mcculls wants to merge 1 commit into
masterfrom
mcculls/rework-legacy-ctx-mgr-shim

Conversation

@mcculls

@mcculls mcculls commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

What Does This Do

Rework Context -> legacy scope manager shim so it can be registered before instrumentation. Context requests are dispatched via AgentTracer to the currently registered tracer implementation. This helps migrate instrumentations to the Context API without requiring the tracer to be installed earlier than it is at the moment.

We deliberately don't install the legacy shim for dd-trace-ot / DDTracer because that artifact doesn't expose the Context API to its users, and none of the implementation needed for the OpenTracing API uses Context. Note that the legacy scope manager implementation deliberately avoids calling back into the Context API (except Context.root() but that's safe because it's a constant and does not result in a context/scope manager call.)

Motivation

Supports clean switching between the two context managers for complete end-to-end testing.

Additional Notes

Uses the isLegacyContextManagerEnabled feature-flag to decide whether to register the legacy scope manager or leave the new context manager in place. (Previously the legacy scope manager was unconditionally installed by the tracer.)

A few non-legacy tests are disabled because they mix both the old activeSpan() call and the Context API. These tests passed previously because the tracer unconditionally installed the legacy context manager regardless of the feature-flag. They will be re-enabled once migration makes them consistent in non-legacy mode (they're already consistent in legacy mode.)

Contributor Checklist

Jira ticket: [PROJ-IDENT]

@mcculls mcculls added comp: core Tracer core tag: no release notes Changes to exclude from release notes type: refactoring labels Jul 14, 2026
@datadog-datadog-us1-prod

This comment has been minimized.

@dd-octo-sts

dd-octo-sts Bot commented Jul 14, 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.09 s 13.93 s [+0.5%; +1.8%] (maybe worse)
startup:insecure-bank:tracing:Agent 12.99 s 13.04 s [-1.2%; +0.3%] (no difference)
startup:petclinic:appsec:Agent 16.93 s 16.71 s [+0.2%; +2.4%] (maybe worse)
startup:petclinic:iast:Agent 16.92 s 17.00 s [-1.2%; +0.3%] (no difference)
startup:petclinic:profiling:Agent 16.79 s 16.76 s [-1.6%; +1.9%] (no difference)
startup:petclinic:sca:Agent 16.93 s 16.85 s [-0.4%; +1.4%] (no difference)
startup:petclinic:tracing:Agent 15.65 s 16.01 s [-6.4%; +2.0%] (no difference)

Commit: 0f9aaf67 · 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/rework-legacy-ctx-mgr-shim branch from 7a1048e to 09eb365 Compare July 14, 2026 08:13
@mcculls mcculls requested a review from Copilot July 14, 2026 08:29
@mcculls

mcculls commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Reworks how the Datadog Context API’s ContextManager is wired when running with the legacy scope manager, so the legacy shim can be registered before instrumentation and can be toggled via isLegacyContextManagerEnabled. This helps support end-to-end testing across both context-manager implementations while keeping context requests routed through AgentTracer to the active tracer implementation.

Changes:

  • Adds Context-API bridging methods to AgentTracer.TracerAPI and introduces an installable LegacyContextManager shim.
  • Stops ContinuableScopeManager from self-registering as a ContextManager; instead, registration happens conditionally in Agent and CoreTracer.
  • Updates benchmarks/tests accordingly and temporarily disables a few non-legacy-mode context swap tests that currently mix legacy activeSpan() and the Context API.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentTracer.java Adds Context API bridging to TracerAPI and a legacy ContextManager shim + installer.
internal-api/build.gradle.kts Excludes the new legacy shim class from coverage.
dd-trace-core/src/main/java/datadog/trace/core/scopemanager/ContinuableScopeManager.java Removes ContextManager implementation/registration behavior from the legacy scope manager.
dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java Conditionally installs the legacy context manager shim and exposes Context API methods via TracerAPI.
dd-trace-core/src/jmh/java/datadog/context/ContextManagerBenchmark.java Updates benchmark to wrap ContinuableScopeManager now that it no longer implements ContextManager.
dd-trace-core/src/test/java/datadog/trace/core/scopemanager/ScopeManagerTest.java Registers the test tracer with AgentTracer so Context API shimming can resolve to the correct tracer.
dd-trace-core/src/test/java/datadog/trace/core/PendingTraceTestBase.java Same as above for pending-trace tests.
dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/Agent.java Conditionally installs the legacy context manager shim during agent bootstrap.
dd-java-agent/instrumentation/kafka/kafka-clients-3.8/src/test/groovy/KafkaClientTestBase.groovy Temporarily ignores a non-legacy context swap forked test.
dd-java-agent/instrumentation/kafka/kafka-clients-0.11/src/test/groovy/KafkaClientTestBase.groovy Temporarily ignores a non-legacy context swap forked test.
dd-java-agent/instrumentation/jms/javax-jms-1.1/src/test/groovy/JMS1Test.groovy Temporarily ignores a non-legacy context swap forked test.
dd-java-agent/instrumentation/aws-java/aws-java-sqs-2.0/src/test/java/SqsClientTest.java Temporarily disables non-legacy context swap forked tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 09eb3654f1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@pr-commenter

pr-commenter Bot commented Jul 14, 2026

Copy link
Copy Markdown

Kafka / consumer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch master mcculls/rework-legacy-ctx-mgr-shim
git_commit_date 1784055714 1784068623
git_commit_sha fbd28da 0f9aaf6
See matching parameters
Baseline Candidate
ci_job_date 1784069935 1784069935
ci_job_id 1859668779 1859668779
ci_pipeline_id 124651037 124651037
cpu_model Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
jdkVersion 11.0.25 11.0.25
jmhVersion 1.36 1.36
jvm /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/lib/jvm/java-11-openjdk-amd64/bin/java
jvmArgs -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/go/src/github.com/DataDog/apm-reliability/dd-trace-java/platform/src/consumer-benchmark/build/tmp/jmh -Duser.country=US -Duser.language=en -Duser.variant -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/go/src/github.com/DataDog/apm-reliability/dd-trace-java/platform/src/consumer-benchmark/build/tmp/jmh -Duser.country=US -Duser.language=en -Duser.variant
vmName OpenJDK 64-Bit Server VM OpenJDK 64-Bit Server VM
vmVersion 11.0.25+9-post-Ubuntu-1ubuntu122.04 11.0.25+9-post-Ubuntu-1ubuntu122.04

Summary

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

scenario Δ mean throughput
scenario:not-instrumented/KafkaConsumerBenchmark.benchConsume worse
[-18456.080op/s; -4119.641op/s] or [-6.186%; -1.381%]
See unchanged results
scenario Δ mean throughput
scenario:only-tracing-dsm-disabled-benchmarks/KafkaConsumerBenchmark.benchConsume same
scenario:only-tracing-dsm-enabled-benchmarks/KafkaConsumerBenchmark.benchConsume same

@mcculls mcculls force-pushed the mcculls/rework-legacy-ctx-mgr-shim branch from b04815d to 7209561 Compare July 14, 2026 09:06
@mcculls

mcculls commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

@mcculls mcculls force-pushed the mcculls/rework-legacy-ctx-mgr-shim branch from 7209561 to 1f47531 Compare July 14, 2026 09:13

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 720956194a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@pr-commenter

pr-commenter Bot commented Jul 14, 2026

Copy link
Copy Markdown

Kafka / producer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch master mcculls/rework-legacy-ctx-mgr-shim
git_commit_date 1784055714 1784068623
git_commit_sha fbd28da 0f9aaf6
See matching parameters
Baseline Candidate
ci_job_date 1784070034 1784070034
ci_job_id 1859682047 1859682047
ci_pipeline_id 124651037 124651037
cpu_model Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
jdkVersion 11.0.25 11.0.25
jmhVersion 1.36 1.36
jvm /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/lib/jvm/java-11-openjdk-amd64/bin/java
jvmArgs -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/go/src/github.com/DataDog/apm-reliability/dd-trace-java/platform/src/producer-benchmark/build/tmp/jmh -Duser.country=US -Duser.language=en -Duser.variant -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/go/src/github.com/DataDog/apm-reliability/dd-trace-java/platform/src/producer-benchmark/build/tmp/jmh -Duser.country=US -Duser.language=en -Duser.variant
vmName OpenJDK 64-Bit Server VM OpenJDK 64-Bit Server VM
vmVersion 11.0.25+9-post-Ubuntu-1ubuntu122.04 11.0.25+9-post-Ubuntu-1ubuntu122.04

Summary

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

scenario Δ mean throughput
scenario:not-instrumented/KafkaProduceBenchmark.benchProduce worse
[-66479.788op/s; -18988.134op/s] or [-3.611%; -1.031%]
See unchanged results
scenario Δ mean throughput
scenario:only-tracing-dsm-disabled-benchmarks/KafkaProduceBenchmark.benchProduce same
scenario:only-tracing-dsm-enabled-benchmarks/KafkaProduceBenchmark.benchProduce same

@mcculls mcculls force-pushed the mcculls/rework-legacy-ctx-mgr-shim branch 3 times, most recently from 6025621 to 3e30c64 Compare July 14, 2026 12:40
@pr-commenter

pr-commenter Bot commented Jul 14, 2026

Copy link
Copy Markdown

Debugger benchmarks

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
ci_job_date 1784052354 1784052702
end_time 2026-07-14T18:07:24 2026-07-14T18:13:13
git_branch master mcculls/rework-legacy-ctx-mgr-shim
git_commit_sha 1913e33 7cdd09c
start_time 2026-07-14T18:05:55 2026-07-14T18:11:43
See matching parameters
Baseline Candidate
ci_job_id 1858833454 1858833454
ci_pipeline_id 124598981 124598981
cpu_model Intel(R) Xeon(R) Platinum 8175M CPU @ 2.50GHz Intel(R) Xeon(R) Platinum 8175M CPU @ 2.50GHz
git_commit_date 1784051660 1784051660

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 10 metrics, 5 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
[-13.054µs; +33.020µs] or [-4.287%; +10.844%]
unstable
[-22.717µs; +45.895µs] or [-6.499%; +13.130%]
unstable
[-32.368µs; +57.958µs] or [-8.842%; +15.833%]
unstable
[-199.381µs; +30.019µs] or [-14.458%; +2.177%]
same
scenario:basic same same same unstable
[-235.135µs; -79.985µs] or [-18.562%; -6.314%]
same
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 (349.546 µs) : 325, 374
.   : milestone, 350,
basic (307.938 µs) : 300, 316
.   : milestone, 308,
loop (9.003 ms) : 8995, 9010
.   : milestone, 9003,
section candidate
noprobe (361.135 µs) : 322, 400
.   : milestone, 361,
basic (310.232 µs) : 300, 320
.   : milestone, 310,
loop (9.011 ms) : 8990, 9032
.   : milestone, 9011,
Loading
  • baseline results
Scenario Request median duration [CI 0.99]
noprobe 349.546 µs [325.443 µs, 373.648 µs]
basic 307.938 µs [300.162 µs, 315.713 µs]
loop 9.003 ms [8.995 ms, 9.01 ms]
  • candidate results
Scenario Request median duration [CI 0.99]
noprobe 361.135 µs [321.879 µs, 400.39 µs]
basic 310.232 µs [300.437 µs, 320.026 µs]
loop 9.011 ms [8.99 ms, 9.032 ms]

@mcculls mcculls force-pushed the mcculls/rework-legacy-ctx-mgr-shim branch from 3e30c64 to b2fe61d Compare July 14, 2026 14:36
@mcculls

mcculls commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 1 comment.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b2fe61d8dc

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 6 comments.

Comment thread dd-java-agent/instrumentation/jms/javax-jms-1.1/src/test/groovy/JMS1Test.groovy Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 43805c08e3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@mcculls mcculls force-pushed the mcculls/rework-legacy-ctx-mgr-shim branch from 43805c0 to 2be7a5f Compare July 14, 2026 20:49
@mcculls mcculls requested a review from Copilot July 14, 2026 21:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Comment thread dd-trace-core/src/test/java/datadog/trace/core/scopemanager/ScopeManagerTest.java Outdated
Comment thread dd-trace-core/src/test/java/datadog/trace/core/scopemanager/ScopeManagerTest.java Outdated
@mcculls mcculls force-pushed the mcculls/rework-legacy-ctx-mgr-shim branch 3 times, most recently from 7e0d21a to 42d2d7a Compare July 14, 2026 22:25
@mcculls mcculls requested a review from Copilot July 14, 2026 22:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.

…for proper swapping over to ThreadLocalContextManager.
@mcculls mcculls force-pushed the mcculls/rework-legacy-ctx-mgr-shim branch from 42d2d7a to 0f9aaf6 Compare July 14, 2026 22:37
@mcculls mcculls marked this pull request as ready for review July 14, 2026 23:31
@mcculls mcculls requested review from a team as code owners July 14, 2026 23:31
@mcculls mcculls requested review from AlexeyKuznetsov-DD, PerfectSlayer, mhlidd and vandonr and removed request for a team July 14, 2026 23:31

@datadog-datadog-us1-prod datadog-datadog-us1-prod Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Datadog Autotest: PASS

More details

Rework of Context API shim for legacy scope manager is architecturally sound. Changes defer Context manager registration to early bootstrap via AgentTracer.installLegacyContextManager(), before instrumentation starts. All new methods properly delegate through CoreTracer to ContinuableScopeManager, with safe NoopTracerAPI fallback. Thread-safety and initialization order are correct. Disabled tests are intentional migration artifacts.

Was this helpful? React 👍 or 👎

📊 Validated against 5 scenarios · Open Bits AI session

🤖 Datadog Autotest · Commit 0f9aaf6 · What is Autotest? · Any feedback? Reach out in #autotest

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

Labels

comp: core Tracer core tag: no release notes Changes to exclude from release notes type: refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants