@@ -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
0 commit comments