Warn on Using Span Inside Generator Functions#1430
Open
dhruv-ahuja wants to merge 5 commits intopydantic:mainfrom
Open
Warn on Using Span Inside Generator Functions#1430dhruv-ahuja wants to merge 5 commits intopydantic:mainfrom
dhruv-ahuja wants to merge 5 commits intopydantic:mainfrom
Conversation
0a17875 to
e52430e
Compare
e52430e to
16488d2
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
Author
|
@alexmojaki please review this code when convenient |
alexmojaki
requested changes
Oct 6, 2025
Contributor
alexmojaki
left a comment
There was a problem hiding this comment.
Please also add to the docs
| _span_name: str | None = None, | ||
| _level: LevelName | int | None = None, | ||
| _links: Sequence[tuple[SpanContext, otel_types.Attributes]] = (), | ||
| _warn_if_inside_generator: bool = True, |
Contributor
There was a problem hiding this comment.
Suggested change
| _warn_if_inside_generator: bool = True, | |
| _allow_generator: bool = False, |
to match logfire.instrument
|
|
||
| is_from_context_manager = False | ||
|
|
||
| # Check if this call is coming from inside a context manager generator by inspecting call stack frames |
Contributor
There was a problem hiding this comment.
I don't think this logic is generally correct. But where is it being tested anyway?
Contributor
There was a problem hiding this comment.
here's an example of this missing bad usage:
import logfire
logfire.configure()
class MyContextManager:
def __enter__(self):
self.span = logfire.span('MyContextManager') # correct usage
items = bad()
for _ in items:
break
logfire.info('after bad') # incorrectly appears under 'bad'
self.span.__enter__()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.span.__exit__(exc_type, exc_value, traceback)
return True
def bad():
with logfire.span('bad'): # incorrect usage
for x in range(3):
yield x
with MyContextManager():
logfire.info('Inside context manager') # incorrectly does not appear under 'MyContextManager'I suggest leaving this for now, it's OK for the user to get some redundant warnings. But it might be possible to follow up with improvement detecting the contextlib decorators.
| if caller_is_generator and _warn_if_inside_generator and not is_from_context_manager: | ||
| warnings.warn( | ||
| 'Span is inside a generator function. See https://logfire.pydantic.dev/docs/reference/advanced/generators/#move-the-span-outside-the-generator.', | ||
| RuntimeWarning, |
Contributor
There was a problem hiding this comment.
I think it should be a UserWarning
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.
Addresses the concerns mentioned in #1424, ensuring the span calls raise runtime warnings with apt documentation link, when used inside generator functions.
Test script output: