Skip to content

refactor(profiling): key native span links by execution unit - #21297

Closed
taegyunkim wants to merge 7 commits into
mainfrom
taegyunkim/profiling-keyed-span-links
Closed

taegyunkim wants to merge 7 commits into
mainfrom
taegyunkim/profiling-keyed-span-links

Conversation

@taegyunkim

@taegyunkim taegyunkim commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Description

The stack profiler stores one active span per OS thread in the native SpanLinks map. That's wrong when the span changes on the same thread: asyncio tasks, greenlets, and thread pool workers (see the xfail tests from #21236).

This PR reworks only the native storage so later PRs can link spans per thread, task, or greenlet. Nothing calls the new operations yet. The existing thread-only operations behave as before.

  • retarget moves links to the parent when a span finishes. Links to a Context, like a thread pool worker's copied context, stay put.
  • A per-key birth number keeps the sampler from mixing up a freed task with a new one at the same address.
  • A lifecycle token drops writes that race a profiler restart.

Testing

New SpanLinksKeyed tests. Span link tests pass under ASan+UBSan, TSan, and valgrind, and tests/profiling passes on CPython 3.12.

Risks

link_span and unlink_finished_span are on the hot path and reimplemented here. Existing tests cover them.

Additional Notes

Python bindings follow in a stacked PR.

@taegyunkim taegyunkim added the changelog/no-changelog A changelog entry is not required for this PR. label Oct 27, 2026
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against main using the target branch CODEOWNERS file.
CODEOWNERS team requests not listed below are not required by the current file set.

ddtrace/internal/datadog/profiling/stack/include/span_links.hpp         @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/src/span_links.cpp             @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/test/test_span_links.cpp       @DataDog/profiling-python

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

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

Show existing violations (showing 5 of 83 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=120)
ddtrace.llmobs._utils -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=118)
ddtrace.llmobs._telemetry -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=118)
ddtrace.llmobs._integrations.anthropic -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=118)
ddtrace.llmobs._integrations.openai_agents -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=118)

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

@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 15991fb | Docs | View more details | Give us feedback!

@pr-commenter

pr-commenter Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-11-09 21:20:26

Comparing candidate commit 15991fb in PR branch taegyunkim/profiling-keyed-span-links with baseline commit 7c3de6e in branch main.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 588 metrics, 9 unstable metrics, 1 flaky benchmarks without significant changes.

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 ----------------------------------'

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 [-733.981ns; +677.746ns] or [-6.838%; +6.314%]

scenario:coreapiscenario-core_dispatch_50_listeners

  • unstable execution_time [-1799.685ns; +1885.722ns] or [-8.972%; +9.401%]

scenario:coreapiscenario-core_dispatch_exception_listeners

  • unstable execution_time [-1816.549ns; +1733.469ns] or [-9.330%; +8.903%]

scenario:coreapiscenario-core_dispatch_listeners

  • unstable execution_time [-356.950ns; +360.996ns] or [-8.238%; +8.332%]

scenario:coreapiscenario-core_dispatch_no_args_listeners

  • unstable execution_time [-210.749ns; +226.932ns] or [-7.733%; +8.326%]

scenario:coreapiscenario-core_dispatch_with_results_1_listener

  • unstable execution_time [-85.700ns; +91.900ns] or [-5.780%; +6.198%]

scenario:coreapiscenario-core_dispatch_with_results_50_listeners

  • unstable execution_time [-4722.884ns; +4748.539ns] or [-9.279%; +9.329%]

scenario:coreapiscenario-core_dispatch_with_results_listeners

  • unstable execution_time [-1017.598ns; +933.725ns] or [-9.440%; +8.662%]

scenario:packagesupdateimporteddependencies-import_many_stdlib_cached

  • unstable execution_time [-49.123µs; +62.042µs] or [-8.534%; +10.779%]

Known flaky benchmarks

These benchmarks are marked as flaky and will not trigger a failure. Modify FLAKY_BENCHMARKS_REGEX to control which benchmarks are marked as flaky.

Known flaky benchmarks without significant changes:

  • scenario:packagespackageforrootmodulemapping-cache_off

…ministic

Bind the entry's optional to a local before dereferencing it so
bugprone-unchecked-optional-access can see the has_value() check.

The contention test relied on two threads overlapping on the mutex,
which valgrind's serialized scheduler never produced. Move the
try-lock-then-hook logic into run_locked and test it with a mutex that
another thread holds until the hook runs.
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-keyed-span-links branch from 1b28267 to 3ea6750 Compare October 28, 2026 18:13
@taegyunkim

Copy link
Copy Markdown
Contributor Author

Closing in favor of #21621.

@taegyunkim taegyunkim closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/no-changelog A changelog entry is not required for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant