-
Notifications
You must be signed in to change notification settings - Fork 223
fix(observe): handle generator context propagation #1383
New 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
Merged
Merged
Conversation
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
Contributor
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.
3 files reviewed, 1 comment
370f69c to
5841a59
Compare
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
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.
Important
Enhances context propagation in generators using new wrapper classes in
observe.py, ensuring trace continuity with comprehensive testing.ContextPreservedSyncGeneratorWrapperandContextPreservedAsyncGeneratorWrapperinobserve.pyto maintain context during generator iteration.contextvars.copy_context()to capture and preserve context for each generator iteration.observe.pyfor async generators.test_decorators.pyfor sync and async generator context preservation, exception handling, and trace hierarchy.observe.pyto use new wrapper classes.This description was created by
for b50dae4. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Overview
Updated On: 2025-09-26 15:22:13 UTC
Summary
This PR implements generator context propagation for the
@observedecorator to fix an issue where OpenTelemetry context was lost during generator iteration, causing traces to break when generators were consumed outside of their original scope.Key Changes
ContextPreservedSyncGeneratorWrapperandContextPreservedAsyncGeneratorWrapper) that preserve the execution context for each iterationcontextvars.copy_context()to capture the active span context when the generator is created, then runs each iteration within that preserved contextasyncio.create_taskdoesn't support thecontextparameterTechnical Implementation
The fix addresses the core issue where generators returned by decorated functions would lose their OpenTelemetry context when consumed later (e.g., by streaming responses in FastAPI). The new wrapper classes ensure that:
This change maintains backward compatibility while ensuring proper trace continuity for streaming/generator use cases.
Confidence Score: 4/5
Important Files Changed
File Analysis
Sequence Diagram
sequenceDiagram participant User participant Observer as @observe Decorator participant Context as contextvars.Context participant Wrapper as GeneratorWrapper participant Original as Original Generator participant Span as LangfuseSpan User->>Observer: Call decorated function Observer->>Context: copy_context() Context-->>Observer: preserved_context Observer->>Original: Execute function Original-->>Observer: Return generator Observer->>Wrapper: Create wrapper class Observer->>Wrapper: Pass (generator, context, span) Observer-->>User: Return wrapper Note over User, Wrapper: Later consumption by user code User->>Wrapper: __iter__() or __aiter__() Wrapper-->>User: Return self loop For each item User->>Wrapper: __next__() or __anext__() Wrapper->>Context: context.run(next, generator) Context->>Original: Execute in preserved context Original-->>Context: Yield item Context-->>Wrapper: Return item Wrapper->>Wrapper: items.append(item) Wrapper-->>User: Return item end User->>Wrapper: __next__() (no more items) Wrapper->>Original: StopIteration/StopAsyncIteration Wrapper->>Span: update(output=joined_items) Wrapper->>Span: end() Wrapper-->>User: Re-raise StopIteration