Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chasleslr
Copy link

@chasleslr chasleslr commented May 9, 2025

PR Description

Fix support for wrapping generator functions with tracer.wrap() and ensure they generate traces in Datadog
Related issue: #5403


Summary of the problem

Currently, when a generator function is wrapped with tracer.wrap(), two major issues arise:

  1. Accessing the current span fails:
    Inside the wrapped generator, tracer.current_span() returns None.
    As a result, any attempt to set tags or interact with the span raises:

    AttributeError: 'NoneType' object has no attribute 'set_tag'
    

    Example:

    @tracer.wrap()
    def foobar():
        current_span = tracer.current_span()
        current_span.set_tag("hello", "world")
        yield 1

    Calling list(foobar()) → crashes with AttributeError.

  2. No traces are reported to Datadog:
    Even if the generator runs without explicit span interaction, no traces are emitted to Datadog.
    This is because the tracer.wrap() decorator does not maintain the span context during generator iteration (next() or async for), so the span never gets properly activated or closed.


Root cause

  • The wrap() logic does not correctly handle Python generators (def ... yield) or async generators (async def ... yield).
  • Without this, both local span interactions (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:

    • Ensure tracer.current_span() is available.
    • Finalize the span after the generator is exhausted or on error.
    • Report traces to Datadog as expected.
  • Add dedicated support for async generators:

    • Use an async for wrapper.
    • Maintain the tracing context.

With this change:

  • Spans inside generators work (current_span() is valid).
  • Traces from generator functions are correctly sent to Datadog.

How to reproduce + verify the fix

Minimal reproducible example:

@tracer.wrap()
def foobar():
    current_span = tracer.current_span()
    current_span.set_tag("hello", "world")
    yield 1

assert list(foobar()) == [1]

Expected result:

  • No errors.
  • Span is created and tagged.
  • Trace is visible in Datadog with the tag hello: world.

Added test cases:

  • Sync generator with span tags.
  • Async generator with span tags.
  • Backward compatibility for sync/async functions.

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant