Skip to content

Commit

Permalink
Restore original scope (#3485)
Browse files Browse the repository at this point in the history
* wip

* cleanup

* .
  • Loading branch information
sentrivana authored Aug 30, 2024
1 parent 118ae73 commit 7c4a3a0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 48 deletions.
17 changes: 13 additions & 4 deletions sentry_sdk/integrations/opentelemetry/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
from opentelemetry.context import get_value, set_value, attach, detach, get_current
from opentelemetry.trace import SpanContext, NonRecordingSpan, TraceFlags, use_span

from sentry_sdk.scope import Scope, ScopeType
from sentry_sdk.tracing import POTelSpan
from sentry_sdk.integrations.opentelemetry.consts import (
SENTRY_SCOPES_KEY,
SENTRY_FORK_ISOLATION_SCOPE_KEY,
)

from sentry_sdk.scope import Scope, ScopeType
from sentry_sdk.tracing import POTelSpan
from sentry_sdk._types import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Tuple, Optional, Generator, Dict, Any
from typing_extensions import Unpack

from sentry_sdk._types import SamplingContext
from sentry_sdk.tracing import TransactionKwargs


class PotelScope(Scope):
Expand Down Expand Up @@ -98,9 +99,17 @@ def _incoming_otel_span_context(self):

return span_context

def start_transaction(self, custom_sampling_context=None, **kwargs):
# type: (Optional[SamplingContext], Unpack[TransactionKwargs]) -> POTelSpan
"""
.. deprecated:: 3.0.0
This function is deprecated and will be removed in a future release.
Use :py:meth:`sentry_sdk.start_span` instead.
"""
return self.start_span(custom_sampling_context=custom_sampling_context)

def start_span(self, custom_sampling_context=None, **kwargs):
# type: (Optional[SamplingContext], Any) -> POTelSpan
# TODO-neel-potel ideally want to remove the span argument, discuss with ivana
return POTelSpan(**kwargs, scope=self)


Expand Down
80 changes: 36 additions & 44 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
NoOpSpan,
Span,
Transaction,
POTelSpan,
)
from sentry_sdk.utils import (
capture_internal_exception,
Expand Down Expand Up @@ -696,13 +695,18 @@ 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.
"""Return the transaction (root span) in the scope, if any."""

.. deprecated:: 3.0.0
This property is deprecated. Use root_span instead.
"""
return self.root_span
# 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
# there is either a transaction (which is its own containing
# transaction) or a non-orphan span on the scope
return self._span.containing_transaction

@transaction.setter
def transaction(self, value):
Expand All @@ -729,22 +733,6 @@ 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 @@ -953,10 +941,6 @@ def start_transaction(
):
# type: (Optional[Transaction], Optional[SamplingContext], Unpack[TransactionKwargs]) -> Union[Transaction, NoOpSpan]
"""
.. deprecated:: 3.0.0
This function is deprecated and will be removed in a future release.
Use :py:meth:`sentry_sdk.start_span` instead.
Start and return a transaction.
Start an existing transaction if given, otherwise create and start a new
Expand Down Expand Up @@ -987,12 +971,14 @@ def start_transaction(
"""
kwargs.setdefault("scope", self)

client = self.get_client()

try_autostart_continuous_profiler()

custom_sampling_context = custom_sampling_context or {}

# if we haven't been given a transaction, make one
transaction = transaction or POTelSpan(**kwargs)
transaction = Transaction(**kwargs)

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

transaction._profile = profile

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

return transaction

def start_span(self, span=None, custom_sampling_context=None, **kwargs):
# type: (Optional[Span], Optional[SamplingContext], Any) -> Span
def start_span(self, **kwargs):
# type: (Optional[Span], Any) -> Span
"""
Start a span whose parent is the currently active span, if any.
Expand All @@ -1024,24 +1015,25 @@ def start_span(self, span=None, custom_sampling_context=None, **kwargs):
For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Span`.
"""
kwargs.setdefault("scope", self)
with new_scope():
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 = 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 = Span(**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

0 comments on commit 7c4a3a0

Please sign in to comment.