Skip to content
Open
Changes from all 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
19 changes: 17 additions & 2 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import warnings
from datetime import datetime, timedelta, timezone
from enum import Enum
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

import sentry_sdk
from sentry_sdk.consts import INSTRUMENTER, SPANDATA, SPANSTATUS, SPANTEMPLATE
Expand Down Expand Up @@ -386,6 +386,10 @@ def __repr__(self) -> str:
)

def __enter__(self) -> "Span":
if has_span_streaming_enabled(sentry_sdk.get_client().options):
self._context_manager_state = None # early return sentinel
return self

scope = self.scope or sentry_sdk.get_current_scope()
old_span = scope.span
scope.span = self
Expand All @@ -395,11 +399,21 @@ def __enter__(self) -> "Span":
def __exit__(
self, ty: "Optional[Any]", value: "Optional[Any]", tb: "Optional[Any]"
) -> None:
if (
hasattr(self, "_context_manager_state")
and self._context_manager_state is None
):
del self._context_manager_state
return

if value is not None and should_be_treated_as_error(ty, value):
self.set_status(SPANSTATUS.INTERNAL_ERROR)

with capture_internal_exceptions():
scope, old_span = self._context_manager_state
scope, old_span = cast(
"Tuple[sentry_sdk.Scope, Optional[Span]]",
self._context_manager_state,
)
del self._context_manager_state
self.finish(scope)
scope.span = old_span
Expand Down Expand Up @@ -1470,6 +1484,7 @@ def calculate_interest_rate(amount, rate, years):
EnvironHeaders,
_generate_sample_rand,
extract_sentrytrace_data,
has_span_streaming_enabled,
has_tracing_enabled,
maybe_create_breadcrumbs_from_span,
)
Loading