Skip to content

fix(profiling): correlate AnyIO worker profiles - #19556

Merged
taegyunkim merged 1 commit into
taegyunkim/profiling-logical-span-linksfrom
taegyunkim/profiling-anyio-span-links
Aug 17, 2026
Merged

taegyunkim merged 1 commit into
taegyunkim/profiling-logical-span-linksfrom
taegyunkim/profiling-anyio-span-links

Conversation

@taegyunkim

@taegyunkim taegyunkim commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Description

Problem

AnyIO copies the active tracing context when anyio.to_thread.run_sync offloads synchronous work, but entering that copied context does not emit a tracing activation event. The profiler therefore associates the request span with the asyncio task while worker-thread samples remain unattributed.

Solution

Wrap the callable submitted through AnyIO's public thread-offload API. On worker entry, publish the inherited profiler span metadata to the physical thread after validating its generation and span lifetime. Clear the physical mapping unconditionally when the callable exits so pooled workers cannot retain attribution across requests.

This reuses the existing native physical-thread mapping and finished-span cleanup. It does not change tracing semantics or add native code.

Review stack

Please review this stack in order:

Order PR Purpose
1 #19420 Shared logical execution-context links and the gevent fix
2 #19346 Reuse the mechanism for asyncio tasks
3 This PR Propagate asyncio attribution into AnyIO worker threads

Testing

  • Python 3.12 profiling suite with AnyIO installed after the latest restack: 388 passed, 46 skipped, 2 xpassed
  • Focused test_anyio_to_thread_span_links regression passed
  • Rebuilt and pruned Riot lockfiles after the latest profiling matrix update
  • scripts/lint cformat
  • scripts/lint checks
  • git diff --check

Risks

  • The wrapper runs only for AnyIO thread offloads while profiling is active.
  • Cleanup is unconditional in a finally block, preventing pooled workers from retaining stale attribution.
  • Weak source-span validation prevents copied contexts from republishing finished or collected spans.
  • Failures are contained to missing profile attribution and do not alter AnyIO or tracing semantics.

Additional Notes

The focused regression covers positive endpoint/span attribution and cleanup when the same AnyIO worker is reused outside the trace. The profiling Riot lockfiles are regenerated for the added AnyIO dependency. The merged base includes #19529's hardened reused-worker sampling assertion.

Release note: fix-anyio-worker-profile-attribution-abbf595ce67e6ad1.yaml.

Pre-review checklist

  • The changes have been tested in real services in staging
  • The PR does not result in new crashes

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Aug 6, 2026 •

Copy link
Copy Markdown

Circular import analysis

⚠️ Existing circular imports

There are 5 circular imports that already exist on the base branch and have not been changed by this PR.

ddtrace.contrib.internal.pytorch._distributed -> ddtrace.contrib.internal.pytorch._rank_root -> ddtrace.contrib.internal.pytorch._distributed
ddtrace.contrib.internal.django.patch -> ddtrace.contrib.internal.django.response -> ddtrace.contrib.internal.django.patch
ddtrace.errortracking._handled_exceptions.bytecode_injector -> ddtrace.errortracking._handled_exceptions.callbacks -> ddtrace.errortracking._handled_exceptions.collector -> ddtrace.errortracking._handled_exceptions.bytecode_reporting -> ddtrace.errortracking._handled_exceptions.bytecode_injector
ddtrace.llmobs -> ddtrace.llmobs._evaluators -> ddtrace.llmobs._evaluators.format -> ddtrace.llmobs._experiment -> ddtrace.llmobs
ddtrace.appsec._asm_request_context -> ddtrace.appsec._iast._iast_request_context_base -> ddtrace.appsec._iast._iast_env -> ddtrace.appsec._iast.reporter -> ddtrace.appsec._exploit_prevention.stack_traces -> ddtrace.appsec._asm_request_context

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Aug 6, 2026 •

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against taegyunkim/profiling-task-span-links using the target branch CODEOWNERS file.
CODEOWNERS team requests not listed below are not required by the current file set.

.riot/requirements/102f90c.txt                                          @DataDog/apm-python
.riot/requirements/123fe6d.txt                                          @DataDog/apm-python
.riot/requirements/1358d6f.txt                                          @DataDog/apm-python
.riot/requirements/1536db2.txt                                          @DataDog/apm-python
.riot/requirements/166b409.txt                                          @DataDog/apm-python
.riot/requirements/1769f2f.txt                                          @DataDog/apm-python
.riot/requirements/17e8650.txt                                          @DataDog/apm-python
.riot/requirements/18899c4.txt                                          @DataDog/apm-python
.riot/requirements/192ae03.txt                                          @DataDog/apm-python
.riot/requirements/19760b6.txt                                          @DataDog/apm-python
.riot/requirements/1b3ec30.txt                                          @DataDog/apm-python
.riot/requirements/1b6b6f3.txt                                          @DataDog/apm-python
.riot/requirements/1cc05ce.txt                                          @DataDog/apm-python
.riot/requirements/1ce715d.txt                                          @DataDog/apm-python
.riot/requirements/1e8c101.txt                                          @DataDog/apm-python
.riot/requirements/1ee1365.txt                                          @DataDog/apm-python
.riot/requirements/1f5c0e7.txt                                          @DataDog/apm-python
.riot/requirements/1f9d7e4.txt                                          @DataDog/apm-python
.riot/requirements/1fa3e75.txt                                          @DataDog/apm-python
.riot/requirements/3237e3e.txt                                          @DataDog/apm-python
.riot/requirements/3773935.txt                                          @DataDog/apm-python
.riot/requirements/3f2bc26.txt                                          @DataDog/apm-python
.riot/requirements/4098903.txt                                          @DataDog/apm-python
.riot/requirements/49786a9.txt                                          @DataDog/apm-python
.riot/requirements/52f10ce.txt                                          @DataDog/apm-python
.riot/requirements/92d03a6.txt                                          @DataDog/apm-python
.riot/requirements/9d54513.txt                                          @DataDog/apm-python
.riot/requirements/b97b2de.txt                                          @DataDog/apm-python
.riot/requirements/c348a9b.txt                                          @DataDog/apm-python
.riot/requirements/c5a1cc4.txt                                          @DataDog/apm-python
.riot/requirements/ce078a5.txt                                          @DataDog/apm-python
.riot/requirements/d6f1bb3.txt                                          @DataDog/apm-python
.riot/requirements/da845dd.txt                                          @DataDog/apm-python
.riot/requirements/ef5fc7d.txt                                          @DataDog/apm-python
ddtrace/internal/datadog/profiling/stack/__init__.py                    @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/__init__.pyi                   @DataDog/profiling-python
ddtrace/profiling/_asyncio.py                                           @DataDog/profiling-python
releasenotes/notes/fix-anyio-worker-profile-attribution-abbf595ce67e6ad1.yaml  @DataDog/apm-python
riotfile.py                                                             @DataDog/apm-python
tests/profiling/collector/test_asyncio_executor.py                      @DataDog/profiling-python

@datadog-prod-us1-6

This comment has been minimized.

@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-anyio-span-links branch from 65af43a to 39c9f71 Compare August 7, 2026 02:47
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-task-span-links branch from 7f285d6 to 8eae04c Compare August 7, 2026 03:48
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-anyio-span-links branch from 39c9f71 to 4dc91bf Compare August 7, 2026 03:48
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-task-span-links branch from 8eae04c to d4cf68d Compare August 11, 2026 18:53
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-anyio-span-links branch from 4dc91bf to b5d8e2d Compare August 11, 2026 18:54
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

There are 255 dependency direction violations that already exist on the base branch and have not been changed by this PR.

Show existing violations (showing 5 of 255 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=134)
ddtrace.llmobs._utils -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=132)
ddtrace.internal.openfeature._span_enrichment -×-> ddtrace.trace  (product:openfeature -> product:tracing, score=132)
ddtrace.llmobs._integrations.langchain -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=132)
ddtrace.llmobs._integrations.vllm -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=132)

To see all violations, download the layers-base.json and layers-pr.json artifacts from this CI job and run:

uv run --script scripts/import-analysis/layers.py compare layers-base.json layers-pr.json

@pr-commenter

pr-commenter Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-08-17 19:37:09

Comparing candidate commit 6c7ea9a in PR branch taegyunkim/profiling-anyio-span-links with baseline commit 3e902cc in branch main.

📊 Benchmarking dashboard

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

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

scenario:httppropagationinject-ids_only

  • 🟥 execution_time [+2.378µs; +2.552µs] or [+10.976%; +11.780%]

scenario:iastaspects-upper_aspect

  • 🟥 execution_time [+46.554µs; +52.914µs] or [+19.380%; +22.027%]

scenario:iastaspectsospath-ospathbasename_aspect

  • 🟥 execution_time [+133.835µs; +143.768µs] or [+33.149%; +35.609%]

scenario:iastaspectssplit-rsplit_aspect

  • 🟥 execution_time [+14.652µs; +20.047µs] or [+10.354%; +14.167%]

scenario:telemetryaddmetric-1-count-metric-1-times

  • 🟥 execution_time [+454.905ns; +497.777ns] or [+17.342%; +18.976%]

scenario:tracer-small

  • 🟥 execution_time [+31.574µs; +33.584µs] or [+9.352%; +9.947%]

Unstable benchmarks

These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.

scenario:coreapiscenario-context_with_data_listeners

  • unstable execution_time [-839.381ns; +645.427ns] or [-7.593%; +5.838%]

scenario:coreapiscenario-core_dispatch_1_listener

  • unstable execution_time [-38.640ns; +27.734ns] or [-6.302%; +4.523%]

scenario:coreapiscenario-core_dispatch_50_listeners

  • unstable execution_time [-1762.859ns; +1530.267ns] or [-10.314%; +8.953%]

scenario:coreapiscenario-core_dispatch_exception_listeners

  • unstable execution_time [-1574.748ns; +920.555ns] or [-11.938%; +6.979%]

scenario:coreapiscenario-core_dispatch_listeners

  • unstable execution_time [-354.429ns; +298.641ns] or [-9.598%; +8.087%]

scenario:coreapiscenario-core_dispatch_no_args_listeners

  • unstable execution_time [-240.092ns; +278.477ns] or [-8.198%; +9.508%]

scenario:coreapiscenario-core_dispatch_with_results_1_listener

  • unstable execution_time [-91.592ns; +54.247ns] or [-7.826%; +4.635%]

scenario:coreapiscenario-core_dispatch_with_results_50_listeners

  • unstable execution_time [-3615.326ns; +4418.153ns] or [-8.941%; +10.926%]

scenario:coreapiscenario-core_dispatch_with_results_listeners

  • unstable execution_time [-879.612ns; +670.820ns] or [-10.716%; +8.172%]

scenario:packagesupdateimporteddependencies-import_many_stdlib_cached

  • unstable execution_time [-66.505µs; +53.859µs] or [-10.347%; +8.380%]

@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-task-span-links branch from d4cf68d to 6a57deb Compare August 12, 2026 15:04
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-anyio-span-links branch from b5d8e2d to 435c1db Compare August 12, 2026 15:05
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-task-span-links branch from 6a57deb to 1550a86 Compare August 12, 2026 15:18
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-anyio-span-links branch 2 times, most recently from 86707b6 to 34de381 Compare August 13, 2026 15:08
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-task-span-links branch 2 times, most recently from 3750446 to 94e436d Compare August 13, 2026 15:10
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-anyio-span-links branch from 34de381 to 5240d90 Compare August 13, 2026 15:10
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-task-span-links branch from 94e436d to 61588e2 Compare August 13, 2026 18:17
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-anyio-span-links branch 2 times, most recently from 3718a42 to 607f5c9 Compare August 13, 2026 19:44
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-task-span-links branch 2 times, most recently from 08220b6 to f0490a5 Compare August 14, 2026 16:00
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-anyio-span-links branch from 607f5c9 to 4fdd955 Compare August 14, 2026 16:00
@taegyunkim taegyunkim added the Profiling Continous Profling label Aug 14, 2026
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-task-span-links branch from f0490a5 to 5c53913 Compare August 17, 2026 18:33
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-anyio-span-links branch from 4fdd955 to 1f5977f Compare August 17, 2026 18:33
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-task-span-links branch from 5c53913 to ed14a06 Compare August 17, 2026 19:03
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-anyio-span-links branch from 1f5977f to 6c7ea9a Compare August 17, 2026 19:03
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-anyio-span-links branch from 6c7ea9a to 6b510d0 Compare August 17, 2026 21:12
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-task-span-links branch from ed14a06 to 5ecd359 Compare August 17, 2026 21:12
Base automatically changed from taegyunkim/profiling-task-span-links to taegyunkim/profiling-logical-span-links August 17, 2026 21:12
@taegyunkim
taegyunkim merged commit 6b510d0 into taegyunkim/profiling-logical-span-links Aug 17, 2026
@taegyunkim
taegyunkim deleted the taegyunkim/profiling-anyio-span-links branch August 17, 2026 21:12
@taegyunkim
taegyunkim restored the taegyunkim/profiling-anyio-span-links branch August 17, 2026 21:13
@taegyunkim

Copy link
Copy Markdown
Contributor Author

Superseded by #19737 after reordering the stack to asyncio, then AnyIO, then gevent. GitHub marked this PR merged when its old base branch was restacked above its head; no code was merged to main.

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

Labels

Profiling Continous Profling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant