Repository navigation
fix(profiling): attribute asyncio task samples to the task's own span - #21653
taegyunkim wants to merge 1 commit into
Conversation
Span labels were tracked per thread only, so when tasks on one event loop each started a span, a task's samples got whichever span was activated on the thread last. Also link each activated span to the running asyncio task, and label task samples with the task's span. A task with no span of its own still gets the thread's span, as before. Span labels are now added once the renderer knows whether a sample belongs to a task, a greenlet or the thread, because the first task on a thread reuses the thread's sample.
Codeowners resolved asResolved from the full PR diff against |
Dependency direction analysis
|
BenchmarksBenchmark execution time: 2026-11-10 17:23:24 Comparing candidate commit 0a56e24 in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 572 metrics, 9 unstable metrics, 1 flaky benchmarks without significant changes.
|
…21743) ## Description JIRA: [PROF-16348](https://datadoghq.atlassian.net/browse/PROF-16348) Adds a test for an eager task (Python 3.12+) that starts its own trace while its creator's span is still open. Every sample of the child's work must carry the child's span id, local root span id and endpoint, and none may carry the parent's. The test first checks that the child really starts its own trace, so a change in span parenting fails at that check instead of looking like a profiler bug. This passes on main, where task samples get the thread's latest span. It guards #21653. During an eager step, the sampler also emits the parent task, with the child's frames on top. Once task samples use their own task's span, that copy gets the parent's span. ## Testing The new test passes on main on Python 3.12, 3.13 and 3.14 (Linux aarch64, `scripts/run-tests`). With #21653 applied, it fails on 3.12. ## Risks None. Test only. ## Additional Notes None. [PROF-16348]: https://datadoghq.atlassian.net/browse/PROF-16348?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ Co-authored-by: taegyun.kim <taegyun.kim@datadoghq.com>
Description
JIRA: PROF-16348
The stack profiler labels each sample with the active span, tracked per thread. Tasks on one event loop share a thread, so when tasks each start a span, a task's samples get whichever span was activated on the thread last. For example, task A starts span A and awaits, task B starts span B, then A resumes: A's samples are labeled with span B.
This PR also links each activated span to the running asyncio task, using the
AsyncioTaskkey from #21621:_link_spanpassesid()of the running task tolink_spanandclear_span.Greenletdomain. Nothing links greenlet keys yet, so they keep the thread's span.Testing
test_stack_keeps_asyncio_task_endpoints_separateno longer xfails. The other four tests in the file still strict-xfail; they need the follow-ups.clear_spandoes not clear the task's link.link_spanandclear_spanarguments.scripts/run-tests: all oftests/profilingon Python 3.12; the asyncio, span link and threadpool tests on 3.9 and 3.14; the gevent tests on 3.12. Also the native tests and clang-tidy on the changed files.Added cost per span activation, on Linux aarch64:
For scale, one
tracer.trace()span inside a task costs about 51 µs on the same host with the profiler on, so this adds about 0.6% inside tasks and 0.15% outside asyncio.Risks
A task's span link is removed when the span finishes or the task activates no context, not when the task ends. If a task ends with its span still open, a new task at the same address can show that span until it finishes.
Additional Notes
None.