Fix asyncio to_thread instrumentation to measure actual function execution - #4901
Open
bourbonkk wants to merge 2 commits into
Open
Fix asyncio to_thread instrumentation to measure actual function execution#4901bourbonkk wants to merge 2 commits into
to_thread instrumentation to measure actual function execution#4901bourbonkk wants to merge 2 commits into
Conversation
trace_to_thread returned the function unwrapped and recorded the span and metrics at wrap time, so durations were ~0 and exceptions raised in the worker thread were never observed. Return a functools.wraps wrapper that records around the real call instead. Drop the _is_instrumented guard: wrappers are created per call, and marking the user's function made every call after the first invisible. Fixes open-telemetry#4900
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.
Description
trace_to_threadreturned the target function unwrapped and recorded the span and metrics at wrap time: theasyncio to_thread-<name>span ended before the function started running in the worker thread,asyncio.process.durationrecorded ~0, and exceptions raised by the function were never observed (statewas always"finished").This change makes
trace_to_threadreturn afunctools.wrapswrapper that starts the timer/span and records metrics around the actual call, making the existing exception handling reachable.It also drops the
_is_instrumentedguard in this path: the wrapper is created perto_thread()call so there is no re-wrapping risk, and marking the user's function meant every call after the first was not instrumented at all.Fixes #4900
Type of change
How Has This Been Tested?
time.sleep(0.1)execution, exception path (span ERROR status + exception event, metricstate="exception"), repeated calls each instrumented.instrumentation-asynciotest suite passes on Python 3.10, 3.12, and 3.14.Does This PR Require a Core Repo Change?
Checklist:
🤖 Generated with Claude Code