Repository navigation
fix(profiling): isolate asyncio task span attribution - #19736
taegyunkim wants to merge 23 commits into
Conversation
5ecd359 to
6e6d489
Compare
|
This replaces #19346 after reordering the stack. The shared logical execution-context infrastructure and asyncio consumer are now first so this branch can be built and tested directly in staging. |
🎉 All green!🧪 All tests passed 🔗 Commit SHA: e0170be | Docs | View more details | Give us feedback! |
Codeowners resolved asResolved from the full PR diff against |
Circular import analysis
|
Dependency direction analysis
|
1ef4500 to
38cc0d2
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 38cc0d2f60
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
emmettbutler
left a comment
There was a problem hiding this comment.
release note looks good
d3a462e to
415103d
Compare
43e6277 to
bdd394c
Compare
01cc2a8 to
0b57e33
Compare
fabbeef to
d92c58d
Compare
653909d to
8f8861f
Compare
Publish inherited attribution before eager task execution and preserve coroutine names and cancellation semantics. Revalidate publication across span finish and profiler resets, roll back allocation and finalizer failures, and release the GIL while waiting for loop-registration state. Add Python and native regression tests for eager execution, concurrent finish, lifecycle invalidation, cleanup failures, and lock contention.
Use ELF link-time wrapping around scalar new calls in an isolated test executable compiled from the production SpanLinks implementation. Delegate allocation and deallocation to the runtime so ASan and Valgrind retain their allocator interception and the failure injector remains active. Keep the allocation regression enabled in the Linux sanitizer and Valgrind matrix, document the linker requirement, and format the asyncio registration test declaration.
Compare custom-factory task names with an unprofiled baseline, since CPython 3.13.13 overwrites the factory name even when the requested name is None. Assert the original coroutine names at the factory boundary and on the task coroutine, covering both implicit and explicit task names.
…task-span-links # Conflicts: # ddtrace/profiling/_asyncio.py # ddtrace/profiling/collector/stack.py
7d55ec9 to
c1a0208
Compare
…task-span-links # Conflicts: # ddtrace/profiling/collector/stack.py
…ovider The task span-link changes call context_provider.active(), which mypy rejects now that ProfilerTracer types context_provider as object. BaseContextProvider lives in ddtrace.internal.native, so this keeps profiling free of tracing-product imports.
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.
- 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.
…ttribution-tests' into taegyunkim/profiling-task-span-links # Conflicts: # tests/profiling/collector/test_stack_asyncio_endpoint_labels.py
## Description JIRA: [PROF-16348](https://datadoghq.atlassian.net/browse/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 [PROF-16348]: https://datadoghq.atlassian.net/browse/PROF-16348?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ Co-authored-by: taegyun.kim <taegyun.kim@datadoghq.com>
…task-span-links # Conflicts: # tests/profiling/collector/test_stack_asyncio_endpoint_labels.py
Description
JIRA: PROF-16348
Fixes incorrect span and endpoint labels in asyncio profiles. Tasks are sampled independently, but their attribution currently comes from the shared event-loop thread, so one task can be labeled with another task's span.
This PR:
_span_links.py.Supports asyncio and uvloop task-creation APIs, including eager tasks, without task-switch hooks. This changes profile attribution, not tracing context propagation.
Testing
#21236 added end-to-end tests that fail on
main, markedxfail(strict=True). This PR removes those markers, so its diff shows the cases it fixes:It also adds regression tests for cases that pass on
mainonly because samples fall back to the thread's span: uvloopgather, loops started withoutset_event_loop, finished spans in a copied Context, and eager tasks with an inherited Context.Latest validation
Merged
origin/mainat45470538bb(which includes #21236) ine0170beb88. The merge commits are SSH-signed.scripts/run-tests -ron Python 3.14 (4e4a3f7e4205) and 3.11 (331e573a0885). It includestest_stack_asyncio_endpoint_labels.py,test_asyncio_span_links.pyandtest_stack_span_links.py.e94c45592f17).scripts/lint typingpassed for the whole project after typingProfilerTracer.context_providerasBaseContextProvider.The existing stack validation is documented in the dependent PRs, including AnyIO, gevent, native sanitizer, Valgrind, and staging coverage.
Risks
This changes attribution bookkeeping in profiler hot paths and adds lifecycle coordination across task creation, span activation/finish, profiler restart, and fork. The implementation bounds that risk by keeping tracing context propagation unchanged, using private profiler-only maps, validating span generation and lifetime before publication, and cleaning mappings on every supported lifecycle boundary. Regression tests cover concurrent tasks, custom task factories, eager tasks, finished spans, restarts, and forks.
Additional Notes
This is the base of the stack. AnyIO worker attribution (#19737) and gevent attribution (#19420) are separate follow-ups stacked on this PR.
Direct
asyncio.Task(...)construction bypasses creation hooks. Before Python 3.12, creation-time attribution is omitted when a custom task factory makes the inherited context unverifiable.Pre-review checklist
Before submitting your PR for review, please make sure the following have
been done/checked.