Skip to content

test(profiling): add asyncio task span attribution tests - #21236

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 7 commits into
mainfrom
taegyunkim/profiling-task-span-attribution-tests
Oct 26, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 7 commits into
mainfrom
taegyunkim/profiling-task-span-attribution-tests

Conversation

@taegyunkim

@taegyunkim taegyunkim commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

JIRA: PROF-16348

The stack profiler tracks span labels per thread, not per asyncio task. Tasks on the same event loop share a thread, so a task's samples get the span and endpoint of whichever span was activated on the thread last, even if it belongs to another task.

These tests cover:

Test What happens on main Python
keeps_asyncio_task_endpoints_separate Task A's samples get task B's endpoint and span id. all
does_not_use_thread_endpoint_for_untraced_asyncio_task An untraced task gets a traced task's endpoint and span id. all
respects_explicit_empty_child_task_context A task created with an empty Context gets its creator's endpoint. 3.11+
omits_unverifiable_pre312_custom_factory_context Same, for a task made by a custom task factory. < 3.12
respects_explicit_empty_context_for_eager_task Same, for an eager task, in its first step and after it resumes. 3.12+

They're all xfail(strict=True). #19736 fixes this and removes the markers, so its diff shows which cases it fixes.

Testing

Ran the file on 3.9 to 3.14. Every test xfails or is skipped for its version. To make sure they fail because of the bug and not something else, I also checked that:

  • with --runxfail, each test fails at its first label assertion, and
  • with the label assertions removed, each test passes (samples exist for every task).

Risks

None, test-only. Like other wall-time tests these are timing-sensitive, and a strict xfail that passes by chance fails the test.

Additional Notes

Add end-to-end tests that run the stack profiler with the tracer and check
the trace endpoint and span id labels on asyncio task samples.

On main, span labels are tracked per thread, not per task, so a task can
pick up another task's endpoint, an untraced task can borrow a traced
task's span, and a task started with an empty Context still gets the
creator's endpoint. Every test here fails on main and is marked
xfail(strict=True), so the fix in #19736 has to remove the markers when
it makes them pass.
@taegyunkim taegyunkim added the changelog/no-changelog A changelog entry is not required for this PR. label Oct 23, 2026
@cit-pr-commenter-54b7da

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.

tests/profiling/collector/test_stack_asyncio_endpoint_labels.py         @DataDog/profiling-python

@cit-pr-commenter-54b7da

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

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

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

Show existing violations (showing 5 of 53 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=122)
ddtrace.internal.ci_visibility.api._base -×-> ddtrace.trace  (product:ci_visibility -> product:tracing, score=120)
ddtrace.internal.openfeature._span_enrichment -×-> ddtrace.trace  (product:openfeature -> product:tracing, score=120)
ddtrace.internal.ci_visibility.recorder -×-> ddtrace.trace  (product:ci_visibility -> product:tracing, score=120)
ddtrace.aiguard._api_client -×-> ddtrace.trace  (product:aiguard -> product:tracing, score=120)

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 23, 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: a7f7798 | Docs | View more details | Give us feedback!

- Check span id and local root span id for concurrent traced tasks.
- Eager test: drop the single-value EMPTY_CONTEXT parameter, rename it,
  use asyncio.run, and check the eager first step and the resumed step
  on their own samples.
@taegyunkim taegyunkim added the Profiling Continous Profling label Oct 26, 2026
@taegyunkim
taegyunkim marked this pull request as ready for review October 26, 2026 15:49
@taegyunkim
taegyunkim requested a review from a team as a code owner October 26, 2026 15:49
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T15:58:56.163888Z a7f7798 New commits
🔒 Security Review ✅ Completed 2026-10-26T15:59:16.478574Z a7f7798 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@KowalskiThomas KowalskiThomas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Testing only so to be honest I haven't really reviewed it!

@taegyunkim

Copy link
Copy Markdown
Contributor Author

Testing only so to be honest I haven't really reviewed it!

@KowalskiThomas 😉

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

Bits Code Review: PASS

More details

The strict expected-failure markers match the known per-thread attribution bug. Static review found no actionable defect in the task synchronization, version guards, or label assertions.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 9cd0f91

@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit df94d88 into main Oct 26, 2026
449 checks passed
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the taegyunkim/profiling-task-span-attribution-tests branch October 26, 2026 18:44
taegyunkim added a commit that referenced this pull request Oct 28, 2026
Turn on per-task attribution for asyncio tasks where the profiler
installs its task key writers. A sample of an asyncio task now carries
the span of that task's own key, or no span, instead of the span that
any task on the thread activated last.

Remove the strict xfails that #21236 added to the asyncio endpoint label
tests. Add end-to-end tests that decode profiles for inherited, eager,
finished, OpenTelemetry, uvloop and asyncio.Runner cases. The
concurrent-task tests check that every sample of each task carries that
task's span or no span, and they fail with per-task attribution off.

Some checks only count a task's own samples. Echion, the native stack
walker, can render one task's frames under another task in two cases
that predate this change: a sample that overlaps the moment a task
starts or stops waiting, and uvloop set up through uvloop.install() or
a policy before Python 3.11, where it does not find the loop boundary.
gh-worker-dd-mergequeue-cf854d Bot pushed a commit that referenced this pull request Nov 10, 2026
## Description

JIRA: [PROF-16348](https://datadoghq.atlassian.net/browse/PROF-16348)

The stack profiler stores each thread's active span in `SpanLinks`, keyed by thread id. All asyncio tasks and gevent greenlets on one thread share that entry, so their samples all get the span that was activated last on the thread. The xfail tests in `tests/profiling/collector/test_stack_asyncio_endpoint_labels.py` (#21236) show this for asyncio.

This PR keys `SpanLinks` by execution unit instead: a `(domain, identifier)` pair, where the domain is `Thread`, `AsyncioTask` or `Greenlet`. Follow-up PRs will write task and greenlet keys, so each one carries its own span.

This PR only changes the existing methods and their callers:

- `link_span`, `unlink_span` and `get_active_span` (renamed from `get_active_span_from_thread_id`, since it no longer takes a thread id) take a `SpanLinkKey`.
- The callers in `stack.cpp` and `stack_renderer.cpp` pass `SpanLinkKey::thread(id)`.
- Only thread keys are written, so behavior is unchanged.

## Testing

- The existing `SpanLinks` C++ tests now pass thread keys.
- New `KeysWithEqualIdentifiersInDifferentDomainsAreSeparate`: a thread key and a task key with the same id don't affect each other.
- Ran locally on Linux aarch64: the native `stack_test` suite, and clang-tidy with CI's checks on the changed files.

## Risks

None. Only thread keys are written, and each method does the same thing for them as before.

## Additional Notes

No release note: an internal refactor with no user-visible change.




[PROF-16348]: https://datadoghq.atlassian.net/browse/PROF-16348?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

Co-authored-by: taegyun.kim <taegyun.kim@datadoghq.com>
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. Profiling Continous Profling testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants