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.
[
opentelemetry-instrumentation-genai-openai] Record response telemetry for asyncwith_streaming_response#610New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
[
opentelemetry-instrumentation-genai-openai] Record response telemetry for asyncwith_streaming_response#610Changes from all commits
fea1952474cedbdfda651d4fd6d45c6b16243cb3b5File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pulls the async path into #491: a stream the caller doesn't drain now emits no span at all, where before the close fallback still ended it (empty). Once
_self_parsedis set the fallback steps aside and nothing else finalizes.Fails here, passes on
main:#491 is hard in general because a plain stream gives no signal that the caller left. Here there is one -
__aexit__closes the http response - and the anthropic package already uses it: see_finalize_close_fallback/_afinalize_close_fallbackininstrumentation/opentelemetry-instrumentation-genai-anthropic/src/opentelemetry/instrumentation/genai/anthropic/_raw_response.py, which close the stream wrapper instead of suppressing themselves. Both stream wrappers guard on_self_finalized, so a drained stream is unaffected.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks! Rethinking this: instead of tracking the wrapper separately, I reused the existing
_self_finalizecallback. It starts asinvocation.stopand is swapped forwrapper.closeonceparse()builds one, so whichever close hook fires just runs whatever is currently there. The field is cleared when the callback is taken, so finalization happens once and the re-entrant close finds nothing left to run.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Race: if the httpx response closes while this coroutine is still pending (e.g. the caller calls
parse()without immediately awaiting it, as your owntest_close_before_awaiting_parse_finalizes_oncedoes), the close fallback already finalizes the span here, empty. When this coroutine then resolves anyway,_wrap_parsedstill builds and returns a stream wrapper; draining it callsinvocation.stop()again, which silently no-ops (span already ended), so whatever attributes it collected are dropped.Fix: in
_wrap_parsed, check whether the close fallback already fired before building the wrapper:_self_finalizeis only ever cleared by the close fallback, so this is a reliable signal without adding new state.Regression test (fails on this branch, passes with the fix above):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the deleted comment, I posted a suggestion and then went to verify it, and that turned out to be wrong. I've updated according to your suggestion, thanks!
Uh oh!
There was an error while loading. Please reload this page.