-
Notifications
You must be signed in to change notification settings - Fork 436
feat(tracer): add support for generators #13377
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
base: main
Are you sure you want to change the base?
Conversation
PR Security UpdateAll commits in this PR up to and including 056d361 have been reviewed and marked safe by SDLC security. For any questions, please reach out to #ci-for-external-contributors-collab on Slack. |
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 new feature needs a release note. Aside from that, the change looks great.
@@ -284,6 +284,29 @@ def wrapped_function(param, kw_param=None): | |||
(dict(name="wrap.overwrite", service="webserver", meta=dict(args="(42,)", kwargs="{'kw_param': 42}")),), | |||
) | |||
|
|||
def test_tracer_wrap_generator(self): |
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.
Have you considered adding a test that validates the duration of these spans?
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.
Good point -- updated in c6029f5.
Oops -- I just added a release note in 056d361. ✅ |
PR Description
Fix support for wrapping generator functions with
tracer.wrap()
and ensure they generate traces in DatadogRelated issue: #5403
Summary of the problem
Currently, when a generator function is wrapped with
tracer.wrap()
, two major issues arise:Accessing the current span fails:
Inside the wrapped generator,
tracer.current_span()
returnsNone
.As a result, any attempt to set tags or interact with the span raises:
Example:
Calling
list(foobar())
→ crashes withAttributeError
.Traces are reported to Datadog with incorrect duration:
Even if the generator runs without explicit span interaction, traces emitted to Datadog do not correctly report the execution time of the function.
This is because the
tracer.wrap()
decorator does not maintain the span context during generator iteration (next()
orasync for
), so the span gets opened and closed at the same time.Root cause
wrap()
logic does not correctly handle Python generators (def ... yield
) or async generators (async def ... yield
).current_span()
) and backend reporting (sending traces to Datadog) break.Proposed fix
This PR updates the
tracer.wrap()
decorator to:Add proper handling for sync generators:
tracer.current_span()
is available.Add dedicated support for async generators:
async for
wrapper.With this change:
current_span()
is valid).How to reproduce + verify the fix
Minimal reproducible example:
Expected result:
hello: world
.Added test cases:
Checklist
Reviewer Checklist