Skip to content

feat(profiling): link task samples to origin task#19082

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 10 commits into
mainfrom
gyuheon0h/asyncio-origin-task
Jul 16, 2026
Merged

feat(profiling): link task samples to origin task#19082
gh-worker-dd-mergequeue-cf854d[bot] merged 10 commits into
mainfrom
gyuheon0h/asyncio-origin-task

Conversation

@gyuheon0h

@gyuheon0h gyuheon0h commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

PROF-15417
local

Description

When an asyncio task offloads blocking work to a ThreadPoolExecutor (await loop.run_in_executor(None, fn)), that work runs on a pool worker thread that has no asyncio task of its own. Today those samples can't be tied back to the task that submitted and is awaiting them.

This adds two pprof labels on the executor worker thread's samples:

  • origin task id -- id() of the submitting asyncio task
  • origin task name -- that task's name

origin task id is deliberately the same value the profiler already emits as task id for the task on its event-loop thread, so the offloaded work correlates back to the awaiting task with no new join key.

The futures integration already wraps ThreadPoolExecutor.submit and runs the work through an intermediate _wrap_execution on the worker thread. We capture the current asyncio task at submit time, and at execution time register a thread_id -> {task id, name} entry in a native per-thread map for the duration of the job, clearing it in a finally. The stack renderer stamps the labels by looking that map up by sampled thread id. The worker thread isnt predicted ahead of time, rather, the link is made on whichever worker the pool assigns and torn down when the job completes, so pool reuse is handled correctly.

Testing

Risks

Additional Notes

Copy link
Copy Markdown
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@gyuheon0h gyuheon0h changed the title Link task samples to origin task feat(profiling): link task samples to origin task Jul 15, 2026
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codeowners resolved as

ddtrace/contrib/internal/futures/threading.py                           @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/internal/datadog/profiling/dd_wrapper/include/libdatadog_helpers.hpp  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/include/sample.hpp        @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/sample.cpp            @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/CMakeLists.txt                 @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/__init__.py                    @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/__init__.pyi                   @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/_stack.pyi                     @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/include/origin_task_links.hpp  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/include/sampler.hpp            @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/src/origin_task_links.cpp      @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/src/sampler.cpp                @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/src/stack.cpp                  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/src/stack_renderer.cpp         @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/test/CMakeLists.txt            @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/test/test_origin_task_links.cpp  @DataDog/profiling-python
releasenotes/notes/profiling-asyncio-origin-task-c0ffee1234567890.yaml  @DataDog/apm-python
tests/contrib/futures/test_origin_task_linking.py                       @DataDog/apm-core-python @DataDog/apm-idm-python
tests/profiling/collector/test_asyncio_executor.py                      @DataDog/profiling-python

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Jul 15, 2026

Copy link
Copy Markdown

Circular import analysis

⚠️ Existing circular imports

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

Show existing cycles (showing 5 of 61 shortest)
ddtrace.internal.datastreams -> ddtrace.internal.datastreams.kombu -> ddtrace.internal.datastreams
ddtrace.internal.datastreams -> ddtrace.internal.datastreams.aiokafka -> ddtrace.internal.datastreams
ddtrace.internal.datastreams -> ddtrace.internal.datastreams.botocore -> ddtrace.internal.datastreams
ddtrace.internal.core -> ddtrace._trace.span -> ddtrace.internal.core
ddtrace.internal.datastreams -> ddtrace.internal.datastreams.kafka -> ddtrace.internal.datastreams

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

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

@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 83a5e40 | Docs | Datadog PR Page | Give us feedback!

@gyuheon0h gyuheon0h force-pushed the gyuheon0h/asyncio-origin-task branch 3 times, most recently from 2beae5a to 6293dda Compare July 15, 2026 17:22
@gyuheon0h gyuheon0h marked this pull request as ready for review July 15, 2026 21:22
@gyuheon0h gyuheon0h requested review from a team as code owners July 15, 2026 21:22
@gyuheon0h gyuheon0h force-pushed the gyuheon0h/asyncio-origin-task branch from 6293dda to da40178 Compare July 15, 2026 21:26
@gyuheon0h gyuheon0h force-pushed the gyuheon0h/asyncio-origin-task branch from da40178 to f233a6f Compare July 15, 2026 21:29

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6293ddaa8e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread ddtrace/contrib/internal/futures/threading.py Outdated
Comment thread ddtrace/internal/datadog/profiling/stack/src/origin_task_links.cpp Outdated
@gyuheon0h gyuheon0h force-pushed the gyuheon0h/asyncio-origin-task branch from f233a6f to bb5b050 Compare July 15, 2026 21:41
Comment thread ddtrace/contrib/internal/futures/threading.py Outdated
Comment thread ddtrace/contrib/internal/futures/threading.py Outdated
Comment thread ddtrace/contrib/internal/futures/threading.py
Comment thread ddtrace/contrib/internal/futures/threading.py
Comment thread ddtrace/internal/datadog/profiling/stack/include/origin_task_links.hpp Outdated
Comment thread ddtrace/internal/datadog/profiling/stack/src/origin_task_links.cpp Outdated
Comment thread ddtrace/internal/datadog/profiling/stack/src/stack.cpp Outdated
Comment thread ddtrace/internal/datadog/profiling/stack/src/stack.cpp Outdated
Comment thread ddtrace/internal/datadog/profiling/stack/src/stack_renderer.cpp
Comment thread ddtrace/internal/datadog/profiling/stack/__init__.py Outdated
@gyuheon0h gyuheon0h force-pushed the gyuheon0h/asyncio-origin-task branch from 0d6e038 to 2170d10 Compare July 16, 2026 15:37
@gyuheon0h gyuheon0h force-pushed the gyuheon0h/asyncio-origin-task branch from f167145 to 66176fd Compare July 16, 2026 15:59
@gyuheon0h gyuheon0h requested a review from KowalskiThomas July 16, 2026 15:59
Comment thread ddtrace/contrib/internal/futures/threading.py Outdated
@gyuheon0h gyuheon0h requested a review from brettlangdon July 16, 2026 16:54
Comment thread ddtrace/internal/datadog/profiling/stack/src/stack.cpp Outdated
Comment thread ddtrace/internal/datadog/profiling/stack/src/stack.cpp
Comment thread ddtrace/internal/datadog/profiling/stack/include/origin_task_links.hpp Outdated
@gyuheon0h gyuheon0h requested a review from taegyunkim July 16, 2026 18:43
Comment thread ddtrace/contrib/internal/futures/threading.py Outdated
Comment thread tests/profiling/collector/test_asyncio_executor.py

@taegyunkim taegyunkim left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With this PR, do we expect to see any difference on UI (timeline maybe?) for applications using ThreadPoolExecutor? or this is PR is a necessary step towards to have such changes?

@gyuheon0h

Copy link
Copy Markdown
Contributor Author

With this PR, do we expect to see any difference on UI (timeline maybe?) for applications using ThreadPoolExecutor? or this is PR is a necessary step towards to have such changes?

@taegyunkim Nothing changed in UI after this. But its a necessary step

@gyuheon0h gyuheon0h requested a review from taegyunkim July 16, 2026 19:28
Comment thread ddtrace/internal/datadog/profiling/stack/src/stack.cpp Outdated
@gyuheon0h gyuheon0h force-pushed the gyuheon0h/asyncio-origin-task branch from 4d1bffd to 519d509 Compare July 16, 2026 19:42
@gyuheon0h gyuheon0h requested a review from taegyunkim July 16, 2026 19:42
@gyuheon0h gyuheon0h force-pushed the gyuheon0h/asyncio-origin-task branch from 519d509 to ab894eb Compare July 16, 2026 19:45

@taegyunkim taegyunkim left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! now it's just formatting ci

@gyuheon0h gyuheon0h force-pushed the gyuheon0h/asyncio-origin-task branch from ab894eb to ef7b1ac Compare July 16, 2026 19:48
Comment thread ddtrace/contrib/internal/futures/threading.py

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

LGTM from an integrations standpoint

@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot merged commit 831271e into main Jul 16, 2026
522 checks passed
@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot deleted the gyuheon0h/asyncio-origin-task branch July 16, 2026 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants