Skip to content

Commit f980b97

Browse files
authored
Set span on scope (#3472)
1 parent 268a524 commit f980b97

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

sentry_sdk/scope.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -696,19 +696,13 @@ def fingerprint(self, value):
696696
def transaction(self):
697697
# type: () -> Any
698698
# would be type: () -> Optional[Transaction], see https://github.com/python/mypy/issues/3004
699-
"""Return the transaction (root span) in the scope, if any."""
700-
701-
# there is no span/transaction on the scope
702-
if self._span is None:
703-
return None
704-
705-
# there is an orphan span on the scope
706-
if self._span.containing_transaction is None:
707-
return None
699+
"""
700+
Return the transaction (root span) in the scope, if any.
708701
709-
# there is either a transaction (which is its own containing
710-
# transaction) or a non-orphan span on the scope
711-
return self._span.containing_transaction
702+
.. deprecated:: 3.0.0
703+
This property is deprecated. Use root_span instead.
704+
"""
705+
return self.root_span
712706

713707
@transaction.setter
714708
def transaction(self, value):
@@ -735,6 +729,22 @@ def transaction(self, value):
735729
if self._span and self._span.containing_transaction:
736730
self._span.containing_transaction.name = value
737731

732+
@property
733+
def root_span(self):
734+
# type: () -> POTelSpan
735+
"""Return the root span in the scope, if any."""
736+
737+
# there is no span on the scope
738+
if self._span is None:
739+
return None
740+
741+
# this is a root span
742+
if self._span.root_span is None:
743+
return self._span
744+
745+
# get the topmost parent
746+
return self._span.root_span
747+
738748
def set_transaction_name(self, name, source=None):
739749
# type: (str, Optional[str]) -> None
740750
"""Set the transaction name and optionally the transaction source."""
@@ -1014,25 +1024,24 @@ def start_span(self, span=None, custom_sampling_context=None, **kwargs):
10141024
10151025
For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Span`.
10161026
"""
1017-
with new_scope():
1018-
kwargs.setdefault("scope", self)
1027+
kwargs.setdefault("scope", self)
10191028

1020-
# get current span or transaction
1021-
span = span or self.span or self.get_isolation_scope().span
1029+
# get current span or transaction
1030+
span = span or self.span or self.get_isolation_scope().span
10221031

1023-
if span is None:
1024-
# New spans get the `trace_id` from the scope
1025-
if "trace_id" not in kwargs:
1026-
propagation_context = self.get_active_propagation_context()
1027-
if propagation_context is not None:
1028-
kwargs["trace_id"] = propagation_context.trace_id
1032+
if span is None:
1033+
# New spans get the `trace_id` from the scope
1034+
if "trace_id" not in kwargs:
1035+
propagation_context = self.get_active_propagation_context()
1036+
if propagation_context is not None:
1037+
kwargs["trace_id"] = propagation_context.trace_id
10291038

1030-
span = POTelSpan(**kwargs)
1031-
else:
1032-
# Children take `trace_id`` from the parent span.
1033-
span = span.start_child(**kwargs)
1039+
span = POTelSpan(**kwargs)
1040+
else:
1041+
# Children take `trace_id`` from the parent span.
1042+
span = span.start_child(**kwargs)
10341043

1035-
return span
1044+
return span
10361045

10371046
def continue_trace(
10381047
self, environ_or_headers, op=None, name=None, source=None, origin=None

sentry_sdk/tracing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,10 @@ def __enter__(self):
12521252
# set as the implicit current context
12531253
self._ctx_token = context.attach(ctx)
12541254

1255+
# get the new scope that was forked on context.attach
1256+
self.scope = sentry_sdk.get_current_scope()
1257+
self.scope.span = self
1258+
12551259
return self
12561260

12571261
def __exit__(self, ty, value, tb):
@@ -1319,7 +1323,6 @@ def root_span(self):
13191323

13201324
parent = None
13211325
while True:
1322-
# XXX test if this actually works
13231326
if self._otel_span.parent:
13241327
parent = self._otel_span.parent
13251328
else:

0 commit comments

Comments
 (0)