Skip to content

Improve scope-span management #3472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
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
63 changes: 36 additions & 27 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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."""
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
Loading