Skip to content

Commit 7c4a3a0

Browse files
authored
Restore original scope (#3485)
* wip * cleanup * .
1 parent 118ae73 commit 7c4a3a0

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

sentry_sdk/integrations/opentelemetry/scope.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
from opentelemetry.context import get_value, set_value, attach, detach, get_current
55
from opentelemetry.trace import SpanContext, NonRecordingSpan, TraceFlags, use_span
66

7-
from sentry_sdk.scope import Scope, ScopeType
8-
from sentry_sdk.tracing import POTelSpan
97
from sentry_sdk.integrations.opentelemetry.consts import (
108
SENTRY_SCOPES_KEY,
119
SENTRY_FORK_ISOLATION_SCOPE_KEY,
1210
)
13-
11+
from sentry_sdk.scope import Scope, ScopeType
12+
from sentry_sdk.tracing import POTelSpan
1413
from sentry_sdk._types import TYPE_CHECKING
1514

1615
if TYPE_CHECKING:
1716
from typing import Tuple, Optional, Generator, Dict, Any
17+
from typing_extensions import Unpack
1818

1919
from sentry_sdk._types import SamplingContext
20+
from sentry_sdk.tracing import TransactionKwargs
2021

2122

2223
class PotelScope(Scope):
@@ -98,9 +99,17 @@ def _incoming_otel_span_context(self):
9899

99100
return span_context
100101

102+
def start_transaction(self, custom_sampling_context=None, **kwargs):
103+
# type: (Optional[SamplingContext], Unpack[TransactionKwargs]) -> POTelSpan
104+
"""
105+
.. deprecated:: 3.0.0
106+
This function is deprecated and will be removed in a future release.
107+
Use :py:meth:`sentry_sdk.start_span` instead.
108+
"""
109+
return self.start_span(custom_sampling_context=custom_sampling_context)
110+
101111
def start_span(self, custom_sampling_context=None, **kwargs):
102112
# type: (Optional[SamplingContext], Any) -> POTelSpan
103-
# TODO-neel-potel ideally want to remove the span argument, discuss with ivana
104113
return POTelSpan(**kwargs, scope=self)
105114

106115

sentry_sdk/scope.py

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
NoOpSpan,
2626
Span,
2727
Transaction,
28-
POTelSpan,
2928
)
3029
from sentry_sdk.utils import (
3130
capture_internal_exception,
@@ -696,13 +695,18 @@ def fingerprint(self, value):
696695
def transaction(self):
697696
# type: () -> Any
698697
# would be type: () -> Optional[Transaction], see https://github.com/python/mypy/issues/3004
699-
"""
700-
Return the transaction (root span) in the scope, if any.
698+
"""Return the transaction (root span) in the scope, if any."""
701699

702-
.. deprecated:: 3.0.0
703-
This property is deprecated. Use root_span instead.
704-
"""
705-
return self.root_span
700+
# there is no span/transaction on the scope
701+
if self._span is None:
702+
return None
703+
704+
# there is an orphan span on the scope
705+
if self._span.containing_transaction is None:
706+
return None
707+
# there is either a transaction (which is its own containing
708+
# transaction) or a non-orphan span on the scope
709+
return self._span.containing_transaction
706710

707711
@transaction.setter
708712
def transaction(self, value):
@@ -729,22 +733,6 @@ def transaction(self, value):
729733
if self._span and self._span.containing_transaction:
730734
self._span.containing_transaction.name = value
731735

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-
748736
def set_transaction_name(self, name, source=None):
749737
# type: (str, Optional[str]) -> None
750738
"""Set the transaction name and optionally the transaction source."""
@@ -953,10 +941,6 @@ def start_transaction(
953941
):
954942
# type: (Optional[Transaction], Optional[SamplingContext], Unpack[TransactionKwargs]) -> Union[Transaction, NoOpSpan]
955943
"""
956-
.. deprecated:: 3.0.0
957-
This function is deprecated and will be removed in a future release.
958-
Use :py:meth:`sentry_sdk.start_span` instead.
959-
960944
Start and return a transaction.
961945
962946
Start an existing transaction if given, otherwise create and start a new
@@ -987,12 +971,14 @@ def start_transaction(
987971
"""
988972
kwargs.setdefault("scope", self)
989973

974+
client = self.get_client()
975+
990976
try_autostart_continuous_profiler()
991977

992978
custom_sampling_context = custom_sampling_context or {}
993979

994980
# if we haven't been given a transaction, make one
995-
transaction = transaction or POTelSpan(**kwargs)
981+
transaction = Transaction(**kwargs)
996982

997983
# use traces_sample_rate, traces_sampler, and/or inheritance to make a
998984
# sampling decision
@@ -1011,10 +997,15 @@ def start_transaction(
1011997

1012998
transaction._profile = profile
1013999

1000+
# we don't bother to keep spans if we already know we're not going to
1001+
# send the transaction
1002+
max_spans = (client.options["_experiments"].get("max_spans")) or 1000
1003+
transaction.init_span_recorder(maxlen=max_spans)
1004+
10141005
return transaction
10151006

1016-
def start_span(self, span=None, custom_sampling_context=None, **kwargs):
1017-
# type: (Optional[Span], Optional[SamplingContext], Any) -> Span
1007+
def start_span(self, **kwargs):
1008+
# type: (Optional[Span], Any) -> Span
10181009
"""
10191010
Start a span whose parent is the currently active span, if any.
10201011
@@ -1024,24 +1015,25 @@ def start_span(self, span=None, custom_sampling_context=None, **kwargs):
10241015
10251016
For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Span`.
10261017
"""
1027-
kwargs.setdefault("scope", self)
1018+
with new_scope():
1019+
kwargs.setdefault("scope", self)
10281020

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

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
1024+
if span is None:
1025+
# New spans get the `trace_id` from the scope
1026+
if "trace_id" not in kwargs:
1027+
propagation_context = self.get_active_propagation_context()
1028+
if propagation_context is not None:
1029+
kwargs["trace_id"] = propagation_context.trace_id
10381030

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

1044-
return span
1036+
return span
10451037

10461038
def continue_trace(
10471039
self, environ_or_headers, op=None, name=None, source=None, origin=None

0 commit comments

Comments
 (0)