diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 20f8e95325..c19a0e523f 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -696,19 +696,13 @@ def fingerprint(self, value): def transaction(self): # type: () -> Any # would be type: () -> Optional[Transaction], see https://github.com/python/mypy/issues/3004 - """Return the transaction (root span) in the scope, if any.""" - - # there is no span/transaction on the scope - if self._span is None: - return None - - # there is an orphan span on the scope - if self._span.containing_transaction is None: - return None + """ + Return the transaction (root span) in the scope, if any. - # there is either a transaction (which is its own containing - # transaction) or a non-orphan span on the scope - return self._span.containing_transaction + .. deprecated:: 3.0.0 + This property is deprecated. Use root_span instead. + """ + return self.root_span @transaction.setter def transaction(self, value): @@ -735,6 +729,22 @@ def transaction(self, value): if self._span and self._span.containing_transaction: self._span.containing_transaction.name = value + @property + def root_span(self): + # type: () -> POTelSpan + """Return the root span in the scope, if any.""" + + # there is no span on the scope + if self._span is None: + return None + + # this is a root span + if self._span.root_span is None: + return self._span + + # get the topmost parent + return self._span.root_span + def set_transaction_name(self, name, source=None): # type: (str, Optional[str]) -> None """Set the transaction name and optionally the transaction source.""" @@ -1014,25 +1024,24 @@ def start_span(self, span=None, custom_sampling_context=None, **kwargs): For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Span`. """ - with new_scope(): - kwargs.setdefault("scope", self) + kwargs.setdefault("scope", self) - # get current span or transaction - span = span or self.span or self.get_isolation_scope().span + # get current span or transaction + span = span or self.span or self.get_isolation_scope().span - if span is None: - # New spans get the `trace_id` from the scope - if "trace_id" not in kwargs: - propagation_context = self.get_active_propagation_context() - if propagation_context is not None: - kwargs["trace_id"] = propagation_context.trace_id + if span is None: + # New spans get the `trace_id` from the scope + if "trace_id" not in kwargs: + propagation_context = self.get_active_propagation_context() + if propagation_context is not None: + kwargs["trace_id"] = propagation_context.trace_id - span = POTelSpan(**kwargs) - else: - # Children take `trace_id`` from the parent span. - span = span.start_child(**kwargs) + span = POTelSpan(**kwargs) + else: + # Children take `trace_id`` from the parent span. + span = span.start_child(**kwargs) - return span + return span def continue_trace( self, environ_or_headers, op=None, name=None, source=None, origin=None diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index ee30bc2cdb..6496327834 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -1252,6 +1252,10 @@ def __enter__(self): # set as the implicit current context self._ctx_token = context.attach(ctx) + # get the new scope that was forked on context.attach + self.scope = sentry_sdk.get_current_scope() + self.scope.span = self + return self def __exit__(self, ty, value, tb): @@ -1319,7 +1323,6 @@ def root_span(self): parent = None while True: - # XXX test if this actually works if self._otel_span.parent: parent = self._otel_span.parent else: