Skip to content

Rework shim between ContextManager and the legacy scope manager#11938

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

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 13.97 s 13.92 s [-0.3%; +1.0%] (no difference)
startup:insecure-bank:tracing:Agent 12.94 s 13.00 s [-1.1%; +0.1%] (no difference)
startup:petclinic:appsec:Agent 16.87 s 16.53 s [+1.1%; +3.0%] (significantly worse)
startup:petclinic:iast:Agent 16.77 s 16.86 s [-1.4%; +0.3%] (no difference)
startup:petclinic:profiling:Agent 16.59 s 16.77 s [-2.0%; -0.0%] (maybe better)
startup:petclinic:sca:Agent 16.84 s 16.66 s [+0.1%; +2.0%] (maybe worse)
startup:petclinic:tracing:Agent 15.62 s 16.19 s [-7.8%; +0.7%] (no difference)

Commit: 43805c08 · 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 1784019333 1784051660
git_commit_sha 871f5b6 7cdd09c
See matching parameters
Baseline Candidate
ci_job_date 1784052878 1784052878
ci_job_id 1858833510 1858833510
ci_pipeline_id 124598981 124598981
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 0 performance regressions! Performance is the same for 3 metrics, 0 unstable metrics.

See unchanged results
scenario Δ mean throughput
scenario:not-instrumented/KafkaConsumerBenchmark.benchConsume same
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 1784056855
git_commit_sha fbd28da 43805c0
See matching parameters
Baseline Candidate
ci_job_date 1784058047 1784058047
ci_job_id 1859137283 1859137283
ci_pipeline_id 124617272 124617272
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 0 performance regressions! Performance is the same for 3 metrics, 0 unstable metrics.

See unchanged results
scenario Δ mean throughput
scenario:not-instrumented/KafkaProduceBenchmark.benchProduce same
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".

@mcculls mcculls force-pushed the mcculls/rework-legacy-ctx-mgr-shim branch 2 times, most recently from 7cdd09c to 43805c0 Compare July 14, 2026 19:23
@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 14 out of 14 changed files in this pull request and generated 6 comments.

@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".

Comment on lines +361 to +363
if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
AgentTracer.installLegacyContextManager()
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reset the context manager when running non-legacy specs

In the Spock instrumentation suite, once a legacy spec executes this branch, ContextProviders.customManager remains the legacy shim; DDSpecification.allowContextTesting() is cached and will not reset it for the next spec. A later spec that sets legacy.context-manager.enabled=false simply skips this block, so its Context API calls still route through the legacy scope manager and the intended non-legacy mode is not actually exercised. Reset the manager back to the test/default manager when this flag is false, or re-allow context testing before each spec.

Useful? React with 👍 / 👎.

…for proper swapping over to ThreadLocalContextManager.
@mcculls mcculls force-pushed the mcculls/rework-legacy-ctx-mgr-shim branch from 43805c0 to 2be7a5f Compare July 14, 2026 20:49
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