Skip to content

Replace old Span/Transaction completely with POTelSpan #3966

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
Jan 20, 2025
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
2 changes: 1 addition & 1 deletion sentry_sdk/ai/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import sentry_sdk.utils
from sentry_sdk import start_span
from sentry_sdk.tracing import POTelSpan as Span
from sentry_sdk.tracing import Span
from sentry_sdk.utils import ContextVar

from typing import TYPE_CHECKING
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
if TYPE_CHECKING:
from typing import Any

from sentry_sdk.tracing import POTelSpan as Span
from sentry_sdk.tracing import Span
from sentry_sdk.utils import logger


Expand Down
8 changes: 4 additions & 4 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from sentry_sdk import tracing_utils, Client
from sentry_sdk._init_implementation import init
from sentry_sdk.tracing import POTelSpan, Transaction, trace
from sentry_sdk.tracing import trace
from sentry_sdk.crons import monitor

# TODO-neel-potel make 2 scope strategies/impls and switch
Expand Down Expand Up @@ -239,7 +239,7 @@ def flush(


def start_span(**kwargs):
# type: (type.Any) -> POTelSpan
# type: (type.Any) -> Span
"""
Start and return a span.

Expand All @@ -256,10 +256,10 @@ def start_span(**kwargs):


def start_transaction(
transaction=None, # type: Optional[Transaction]
transaction=None, # type: Optional[Span]
**kwargs, # type: Unpack[TransactionKwargs]
):
# type: (...) -> POTelSpan
# type: (...) -> Span
"""
.. deprecated:: 3.0.0
This function is deprecated and will be removed in a future release.
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sentry_sdk.consts import OP, SPANDATA, SPANSTATUS
from sentry_sdk.ai.utils import set_data_normalized
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.tracing import POTelSpan as Span
from sentry_sdk.tracing import Span
from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.utils import logger, capture_internal_exceptions

Expand Down
8 changes: 4 additions & 4 deletions sentry_sdk/integrations/opentelemetry/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)
from sentry_sdk.integrations.opentelemetry.utils import trace_state_from_baggage
from sentry_sdk.scope import Scope, ScopeType
from sentry_sdk.tracing import POTelSpan
from sentry_sdk.tracing import Span
from sentry_sdk._types import TYPE_CHECKING

if TYPE_CHECKING:
Expand Down Expand Up @@ -128,7 +128,7 @@ def _incoming_otel_span_context(self):
return span_context

def start_transaction(self, **kwargs):
# type: (Unpack[TransactionKwargs]) -> POTelSpan
# type: (Unpack[TransactionKwargs]) -> Span
"""
.. deprecated:: 3.0.0
This function is deprecated and will be removed in a future release.
Expand All @@ -137,8 +137,8 @@ def start_transaction(self, **kwargs):
return self.start_span(**kwargs)

def start_span(self, **kwargs):
# type: (Any) -> POTelSpan
return POTelSpan(**kwargs, scope=self)
# type: (Any) -> Span
return Span(**kwargs, scope=self)


_INITIAL_CURRENT_SCOPE = None
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/opentelemetry/span_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def force_flush(self, timeout_millis=30000):
def _add_root_span(self, span, parent_span):
# type: (Span, AbstractSpan) -> None
"""
This is required to make POTelSpan.root_span work
This is required to make Span.root_span work
since we can't traverse back to the root purely with otel efficiently.
"""
if parent_span != INVALID_SPAN and not parent_span.get_span_context().is_remote:
Expand Down
10 changes: 5 additions & 5 deletions sentry_sdk/integrations/rust_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import sentry_sdk
from sentry_sdk.integrations import Integration
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.tracing import POTelSpan as SentrySpan
from sentry_sdk.tracing import Span
from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE


Expand Down Expand Up @@ -169,7 +169,7 @@ def _include_tracing_fields(self) -> bool:
else self.include_tracing_fields
)

def on_event(self, event: str, _span_state: Optional[SentrySpan]) -> None:
def on_event(self, event: str, _span_state: Optional[Span]) -> None:
deserialized_event = json.loads(event)
metadata = deserialized_event.get("metadata", {})

Expand All @@ -183,7 +183,7 @@ def on_event(self, event: str, _span_state: Optional[SentrySpan]) -> None:
elif event_type == EventTypeMapping.Event:
process_event(deserialized_event)

def on_new_span(self, attrs: str, span_id: str) -> Optional[SentrySpan]:
def on_new_span(self, attrs: str, span_id: str) -> Optional[Span]:
attrs = json.loads(attrs)
metadata = attrs.get("metadata", {})

Expand Down Expand Up @@ -220,11 +220,11 @@ def on_new_span(self, attrs: str, span_id: str) -> Optional[SentrySpan]:

return span

def on_close(self, span_id: str, span: Optional[SentrySpan]) -> None:
def on_close(self, span_id: str, span: Optional[Span]) -> None:
if span is not None:
span.__exit__(None, None, None)

def on_record(self, span_id: str, values: str, span: Optional[SentrySpan]) -> None:
def on_record(self, span_id: str, values: str, span: Optional[Span]) -> None:
if span is not None:
deserialized_values = json.loads(values)
for key, value in deserialized_values.items():
Expand Down
4 changes: 2 additions & 2 deletions sentry_sdk/integrations/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
_request_headers_to_span_attributes,
)
from sentry_sdk.sessions import track_session
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_ROUTE
from sentry_sdk.tracing import Span, TRANSACTION_SOURCE_ROUTE
from sentry_sdk.utils import (
ContextVar,
capture_internal_exceptions,
Expand Down Expand Up @@ -157,7 +157,7 @@ def __call__(self, environ, start_response):

def _sentry_start_response( # type: ignore
old_start_response, # type: StartResponse
transaction, # type: Optional[Transaction]
transaction, # type: Optional[Span]
status, # type: str
response_headers, # type: WsgiResponseHeaders
exc_info=None, # type: Optional[WsgiExcInfo]
Expand Down
23 changes: 12 additions & 11 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
SENTRY_TRACE_HEADER_NAME,
NoOpSpan,
Span,
POTelSpan,
Transaction,
)
from sentry_sdk.utils import (
capture_internal_exception,
Expand Down Expand Up @@ -677,7 +675,7 @@ def clear(self):
self.clear_breadcrumbs()
self._should_capture = True # type: bool

self._span = None # type: Optional[POTelSpan]
self._span = None # type: Optional[Span]
self._session = None # type: Optional[Session]
self._force_auto_session_tracking = None # type: Optional[bool]

Expand Down Expand Up @@ -707,7 +705,7 @@ def fingerprint(self, value):
@property
def transaction(self):
# type: () -> Any
# would be type: () -> Optional[Transaction], see https://github.com/python/mypy/issues/3004
# would be type: () -> Optional[Span], 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
Expand All @@ -734,7 +732,7 @@ def transaction(self, value):
# anything set in the scope.
# XXX: note that with the introduction of the Scope.transaction getter,
# there is a semantic and type mismatch between getter and setter. The
# getter returns a Transaction, the setter sets a transaction name.
# getter returns a Span, the setter sets a transaction name.
# Without breaking version compatibility, we could make the setter set a
# transaction name or transaction (self._span) depending on the type of
# the value argument.
Expand Down Expand Up @@ -785,13 +783,13 @@ def set_user(self, value):

@property
def span(self):
# type: () -> Optional[POTelSpan]
# type: () -> Optional[Span]
"""Get current tracing span."""
return self._span

@span.setter
def span(self, span):
# type: (Optional[POTelSpan]) -> None
# type: (Optional[Span]) -> None
"""Set current tracing span."""
self._span = span

Expand Down Expand Up @@ -952,7 +950,7 @@ def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
self._breadcrumbs.popleft()

def start_transaction(self, transaction=None, **kwargs):
# type: (Optional[Transaction], Optional[SamplingContext], Unpack[TransactionKwargs]) -> Union[Transaction, NoOpSpan]
# type: (Optional[Span], Optional[SamplingContext], Unpack[TransactionKwargs]) -> Union[Span, NoOpSpan]
"""
Start and return a transaction.

Expand Down Expand Up @@ -981,14 +979,15 @@ def start_transaction(self, transaction=None, **kwargs):
constructor. See :py:class:`sentry_sdk.tracing.Transaction` for
available arguments.
"""
# TODO-neel-potel fix signature and no op
kwargs.setdefault("scope", self)

client = self.get_client()

try_autostart_continuous_profiler()

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

# use traces_sample_rate, traces_sampler, and/or inheritance to make a
# sampling decision
Expand Down Expand Up @@ -1024,6 +1023,7 @@ def start_span(self, **kwargs):

For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Span`.
"""
# TODO-neel-potel fix signature and no op
if kwargs.get("description") is not None:
warnings.warn(
"The `description` parameter is deprecated. Please use `name` instead.",
Expand Down Expand Up @@ -1054,13 +1054,14 @@ def start_span(self, **kwargs):
def continue_trace(
self, environ_or_headers, op=None, name=None, source=None, origin=None
):
# type: (Dict[str, Any], Optional[str], Optional[str], Optional[str], Optional[str]) -> Transaction
# TODO-neel-potel fix signature and no op
# type: (Dict[str, Any], Optional[str], Optional[str], Optional[str], Optional[str]) -> Span
"""
Sets the propagation context from environment or headers and returns a transaction.
"""
self.generate_propagation_context(environ_or_headers)

transaction = Transaction.continue_from_headers(
transaction = Span.continue_from_headers(
normalize_incoming_data(environ_or_headers),
op=op,
origin=origin,
Expand Down
Loading
Loading