Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sentry_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"get_isolation_scope",
"get_current_scope",
"get_current_span",
"get_current_streamed_span",
"get_traceparent",
"is_initialized",
"isolation_scope",
Expand Down
12 changes: 11 additions & 1 deletion sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def overload(x: "T") -> "T":
"get_isolation_scope",
"get_current_scope",
"get_current_span",
"get_current_streamed_span",
"get_traceparent",
"is_initialized",
"isolation_scope",
Expand Down Expand Up @@ -422,13 +423,22 @@ def set_measurement(name: str, value: float, unit: "MeasurementUnit" = "") -> No

def get_current_span(
scope: "Optional[Scope]" = None,
) -> "Optional[Union[Span, StreamedSpan]]":
) -> "Optional[Span]":
"""
Returns the currently active span if there is one running, otherwise `None`
"""
return tracing_utils.get_current_span(scope)


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)
Comment thread
alexander-alderman-webb marked this conversation as resolved.
Outdated
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
alexander-alderman-webb marked this conversation as resolved.
Outdated
def get_traceparent() -> "Optional[str]":
"""
Returns the traceparent either from the active span or from the scope.
Expand Down
19 changes: 15 additions & 4 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,9 @@
return

span = kwargs.pop("span", None)
span = span or self.span
if not span:
span_streaming = has_span_streaming_enabled(client.options)
span = self.streamed_span if span_streaming else self.span

if (
has_tracing_enabled(client.options)
Expand Down Expand Up @@ -877,22 +879,31 @@
session.update(user=value)

@property
def span(self) -> "Optional[Union[Span, StreamedSpan]]":
def span(self) -> "Optional[Span]":
"""Get/set current tracing span or transaction."""
return self._span
return self._span if isinstance(self._span, Span) else None

@span.setter
def span(self, span: "Optional[Union[Span, StreamedSpan]]") -> None:
def span(self, span: "Optional[Span]") -> None:
self._span = span
# XXX: this differs from the implementation in JS, there Scope.setSpan
# does not set Scope._transactionName.
if isinstance(span, Transaction):
transaction = span
if transaction.name:
self._transaction = transaction.name
if transaction.source:
self._transaction_info["source"] = transaction.source

Check warning on line 896 in sentry_sdk/scope.py

View check run for this annotation

@sentry/warden / warden: find-bugs

span.setter silently drops StreamedSpan segment metadata, breaking runtime backwards compatibility

The previous span.setter accepted Union[Span, StreamedSpan] and, when given a StreamedSpan segment, populated self._transaction and self._transaction_info from the span's name and 'sentry.span.source' attribute. The new span.setter only handles Transaction and silently ignores StreamedSpan inputs at runtime. Although the type annotation is now Optional[Span], existing callers (integrations, user code, third-party code) that still assign a StreamedSpan via scope.span = streamed_span will no longer have transaction metadata populated, causing events to be missing transaction names/sources and breaking segment linkage in streaming mode. The PR's stated goal is backwards compatibility, but this is only type-level — runtime behavior regressed.
Comment thread
alexander-alderman-webb marked this conversation as resolved.

@property
def streamed_span(self) -> "Optional[StreamedSpan]":
"""Get/set current tracing span."""
return self._span if isinstance(self._span, StreamedSpan) else None

@streamed_span.setter
def streamed_span(self, span: "Optional[StreamedSpan]") -> None:
self._span = span
Comment thread
alexander-alderman-webb marked this conversation as resolved.

# Also set _transaction and _transaction_info in streaming mode as this
# is used for populating events and linking them to segments
if (
Expand Down
13 changes: 12 additions & 1 deletion sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ def sync_wrapper(*args: "Any", **kwargs: "Any") -> "Any":

def get_current_span(
scope: "Optional[sentry_sdk.Scope]" = None,
) -> "Optional[Union[Span, StreamedSpan]]":
) -> "Optional[Span]":
"""
Returns the currently active span if there is one running, otherwise `None`
"""
Expand All @@ -1203,6 +1203,17 @@ def get_current_span(
return current_span


def get_current_streamed_span(
Comment thread
alexander-alderman-webb marked this conversation as resolved.
Outdated
scope: "Optional[sentry_sdk.Scope]" = None,
) -> "Optional[StreamedSpan]":
"""
Returns the currently active span if there is one running, otherwise `None`
"""
scope = scope or sentry_sdk.get_current_scope()
current_span = scope.streamed_span
return current_span


Comment thread
alexander-alderman-webb marked this conversation as resolved.
Outdated
def set_span_errored(span: "Optional[Union[Span, StreamedSpan]]" = None) -> None:
"""
Set the status of the current or given span to INTERNAL_ERROR.
Expand Down
Loading