Skip to content

fix: Introduce _get_current_streamed_span() to keep types backwards compatible#6177

Merged
alexander-alderman-webb merged 16 commits intomasterfrom
webb/get-current-streamed-span
Apr 30, 2026
Merged

fix: Introduce _get_current_streamed_span() to keep types backwards compatible#6177
alexander-alderman-webb merged 16 commits intomasterfrom
webb/get-current-streamed-span

Conversation

@alexander-alderman-webb
Copy link
Copy Markdown
Contributor

@alexander-alderman-webb alexander-alderman-webb commented Apr 30, 2026

…atible

Description

Create streaming-lifecycle variants of the tracing_utils.get_current_span() function and the Scope.span property. The existing functions are narrowed to be backwards compatible for users of the transaction-based span lifecycle, allowing users to upgrade their SDK version without their type checkers raising errors.

Note that this breaks all current uses of these functions when the streaming lifecycle option is enabled (which is experimental). All call sites internal to the SDK are updated.

The Scope.span property returns None if a StreamedSpan is held, and Scope.streamed_span returns None if a Span is held on the scope.

Issues

Closes #6166

Reminders

@alexander-alderman-webb alexander-alderman-webb changed the title fix: Introduce get_current_streamed_span to keep types backwards comp… fix: Introduce get_current_streamed_span() to keep types backwards compatible Apr 30, 2026
Comment thread sentry_sdk/scope.py Outdated
Comment thread sentry_sdk/api.py Outdated
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 30, 2026

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 7.59s

All tests are passing successfully.

❌ Patch coverage is 59.09%. Project has 14939 uncovered lines.

Files with missing lines (7)
File Patch % Lines
tracing_utils.py 42.84% ⚠️ 451 Missing and 42 partials
starlette.py 5.61% ⚠️ 370 Missing
scope.py 71.73% ⚠️ 253 Missing and 84 partials
traces.py 69.28% ⚠️ 94 Missing and 21 partials
fastapi.py 15.96% ⚠️ 79 Missing
api.py 64.20% ⚠️ 58 Missing
__init__.py 94.66% ⚠️ 15 Missing and 15 partials

Generated by Codecov Action

Comment thread sentry_sdk/scope.py
Comment thread sentry_sdk/integrations/starlette.py
Comment thread sentry_sdk/scope.py Outdated
@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review April 30, 2026 11:08
@alexander-alderman-webb alexander-alderman-webb requested a review from a team as a code owner April 30, 2026 11:08
Comment thread sentry_sdk/tracing_utils.py Outdated
@alexander-alderman-webb alexander-alderman-webb marked this pull request as draft April 30, 2026 11:12
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit e0824df. Configure here.

Comment thread sentry_sdk/tracing_utils.py Outdated
Comment thread sentry_sdk/api.py Outdated
Copy link
Copy Markdown
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TY for untangling the two 🙏🏻

Left two small questions and a comment.

Comment thread sentry_sdk/scope.py
Comment thread sentry_sdk/scope.py
Comment thread sentry_sdk/api.py Outdated
Comment on lines +433 to +439
def get_current_streamed_span(
scope: "Optional[Scope]" = None,
) -> "Optional[StreamedSpan]":
"""
Returns the currently active streamed span if there is one running, otherwise `None`
"""
return tracing_utils.get_current_streamed_span(scope)
Copy link
Copy Markdown
Contributor

@sentrivana sentrivana Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd not expose this as a public API function, at least while the functionality is experimental. Maybe we can move this to traces.py, or at least prefix it with an underscore.

Would also be good to add more context to the docstring so that users know when this can be used (i.e., only if trace_lifecycle="stream").

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's a great point, it would be best to not expose too much. I've limited the API surface to _get_current_streamed_span in traces.py and expanded the docstring

Comment thread sentry_sdk/scope.py
@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review April 30, 2026 11:47
@alexander-alderman-webb alexander-alderman-webb changed the title fix: Introduce get_current_streamed_span() to keep types backwards compatible fix: Introduce _get_current_streamed_span() to keep types backwards compatible Apr 30, 2026
Copy link
Copy Markdown
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏🏻

@alexander-alderman-webb alexander-alderman-webb merged commit 98294ce into master Apr 30, 2026
155 checks passed
@alexander-alderman-webb alexander-alderman-webb deleted the webb/get-current-streamed-span branch April 30, 2026 13:46
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.

span.set_data does not exist on StreamedSpan

2 participants