refactor(profiling): replace bytecode wrapping with sys.monitoring (PROF-14200) - #18389
Closed
vlad-scherbich wants to merge 3 commits into
Closed
Conversation
Contributor
|
BenchmarksBenchmark execution time: 2026-06-01 21:06:16 Comparing candidate commit 0af646f in PR branch Found 0 performance improvements and 3 performance regressions! Performance is the same for 597 metrics, 9 unstable metrics. scenario:iastaspects-stringio_aspect
scenario:iastaspectsospath-ospathbasename_aspect
scenario:span-start
|
vlad-scherbich
force-pushed
the
vlad/ddtracepy-315-profiling-asyncio-monitoring
branch
from
June 2, 2026 20:50
0af646f to
e5a713d
Compare
Codeowners resolved as |
vlad-scherbich
force-pushed
the
vlad/ddtracepy-315-profiling-only
branch
2 times, most recently
from
June 3, 2026 14:32
fb4efb9 to
9564e7a
Compare
vlad-scherbich
force-pushed
the
vlad/ddtracepy-315-profiling-asyncio-monitoring
branch
from
June 4, 2026 18:04
e5a713d to
49b7af0
Compare
vlad-scherbich
force-pushed
the
vlad/ddtracepy-315-profiling-only
branch
from
June 5, 2026 19:51
217187a to
e1f83fe
Compare
vlad-scherbich
force-pushed
the
vlad/ddtracepy-315-profiling-asyncio-monitoring
branch
from
June 5, 2026 19:51
49b7af0 to
311f1cf
Compare
vlad-scherbich
force-pushed
the
vlad/ddtracepy-315-profiling-only
branch
from
June 5, 2026 20:21
e1f83fe to
905a3d8
Compare
vlad-scherbich
force-pushed
the
vlad/ddtracepy-315-profiling-asyncio-monitoring
branch
from
June 5, 2026 20:23
311f1cf to
2f7c86f
Compare
…direct patching in _asyncio.py Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…5+ only Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
vlad-scherbich
force-pushed
the
vlad/ddtracepy-315-profiling-only
branch
from
June 8, 2026 12:41
905a3d8 to
49c1ffe
Compare
vlad-scherbich
force-pushed
the
vlad/ddtracepy-315-profiling-asyncio-monitoring
branch
from
June 8, 2026 12:41
2f7c86f to
670d7db
Compare
Contributor
|
This pull request has been automatically closed after a period of inactivity. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
← Prev PR: #17624
3.14 vs 3.15 (table view)
3.14 vs 3.15 (flame graph): python
3.14 vs 3.15 (flame graph): native
Description
Refactors
ddtrace/profiling/_asyncio.pyto replace thefunctools.partial + wrap()monkey-patching mechanism with direct attribute replacement throughout, and introduces asys.monitoringPY_RETURN hook layer on Python 3.15+ for task-creation hooks.Context: The existing
wrap()approach intercepts calls by replacing a function in its owning namespace with a partial-applied closure. Direct attribute replacement (_original = f; def _patched(...): ...; module.f = _patched) achieves identical runtime behavior with less indirection and no dependency onget_argument_value.sys.monitoringfor task-creation hooks (Python 3.15+):asyncio.create_taskandTaskGroup.create_taskreturn the newTaskobject, which we need in order to callstack.link_tasks(parent, task).sys.monitoringCALL/PY_START events don't expose callee arguments, but PY_RETURN gives us the return value directly — making it a natural fit for these two sites. On older Python the same sites fall back to standard attribute replacement.Changes
set_event_loop,_GatheringFuture.__init__,_wait,as_completed(asyncio) andnew_event_loop,set_event_loop(uvloop): converted fromwrap()closures to direct_original_X / _patched_Xattribute replacementcreate_task,TaskGroup.create_task:sys.monitoringPY_RETURN hook on Python 3.15+; direct attribute replacement on older versions_register_return_hook(),_unregister_all_return_hooks(),_py_return_dispatch(),_py_return_handlersdict,_monitoring_tool_idfunctools.partial,get_argument_value, andwrapimportssys.monitoringpath is gated at>= (3, 15)— not rolled out to 3.12–3.14 yet (see TODO in code for follow-up)Testing
CI:
tests/profiling/collector/test_asyncio.py,tests/profiling/test_scheduler.pyManual A/B validation
py3.14 (baseline) vs py3.15 on
movies-abservice — covers experiment window from branch start to present:Risks
Low. The behavioral contract is unchanged — the same events trigger the same
link_tasks/weak_link_tasks/track_asyncio_loopcalls. The main risk is a subtle argument-passing error in the direct-replacement wrappers; these are covered by the asyncio profiling tests.Additional Notes
sys.monitoringtool IDs 4–5 are used (free custom slots; 0–3 are reserved for debugger, coverage, profiler, optimizer). Tool 5 is tried first to minimize conflicts._memalloc_tb.cppis bundled in this branch.