From 4668c3e9e27bbb24b9b4e8c778926add76cbe072 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Tue, 30 Jul 2024 16:06:08 +0200 Subject: [PATCH] ref(tracing): Simplify backwards-compat code With this change, we aim to simplify the backwards-compatibility code for POTel tracing. We do this as follows: - Remove `start_*` functions from `tracing` - Remove unused parameters from `tracing.POTelSpan.__init__`. - Make all parameters to `tracing.POTelSpan.__init__` kwarg-only. - Allow `tracing.POTelSpan.__init__` to accept arbitrary kwargs, which are all ignored, for compatibility with old `Span` interface. - Completely remove `start_inactive_span`, since inactive spans can be created by setting `active=False` when constructing a `POTelSpan`. --- sentry_sdk/api.py | 16 ++++++---------- sentry_sdk/tracing.py | 29 +++++++---------------------- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/sentry_sdk/api.py b/sentry_sdk/api.py index 4c3104f35f..94ee10421e 100644 --- a/sentry_sdk/api.py +++ b/sentry_sdk/api.py @@ -77,7 +77,6 @@ def overload(x): "set_tags", "set_user", "start_span", - "start_inactive_span", "start_transaction", "trace", "monitor", @@ -338,14 +337,11 @@ def start_span( **kwargs, # type: Any ): # type: (...) -> POTelSpan - return tracing.start_span(**kwargs) - - -def start_inactive_span( - **kwargs, # type: Any -): - # type: (...) -> POTelSpan - return tracing.start_inactive_span(**kwargs) + """ + Alias for tracing.POTelSpan constructor. The method signature is the same. + """ + # TODO: Consider adding type hints to the method signature. + return tracing.POTelSpan(**kwargs) def start_transaction( @@ -387,7 +383,7 @@ def start_transaction( constructor. See :py:class:`sentry_sdk.tracing.Transaction` for available arguments. """ - return tracing.start_transaction(transaction, custom_sampling_context, **kwargs) + return start_span(**kwargs) def set_measurement(name, value, unit=""): diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index 145104c6f6..1db105fa62 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -1249,21 +1249,19 @@ class POTelSpan: def __init__( self, + *, active=True, # type: bool - trace_id=None, # type: Optional[str] - span_id=None, # type: Optional[str] - parent_span_id=None, # type: Optional[str] - same_process_as_parent=True, # type: bool - sampled=None, # type: Optional[bool] op=None, # type: Optional[str] description=None, # type: Optional[str] - status=None, # type: Optional[str] - containing_transaction=None, # type: Optional[Transaction] - start_timestamp=None, # type: Optional[Union[datetime, float]] - scope=None, # type: Optional[sentry_sdk.Scope] origin="manual", # type: str + **_, # type: dict[str, object] ): # type: (...) -> None + """ + For backwards compatibility with old the old Span interface, this class + accepts arbitrary keyword arguments, in addition to the ones explicitly + listed in the signature. These additional arguments are ignored. + """ from sentry_sdk.integrations.opentelemetry.consts import SentrySpanAttribute self._otel_span = tracer.start_span(description or op or "") # XXX @@ -1443,19 +1441,6 @@ async def my_async_function(): return start_child_span_decorator -def start_span(*args, **kwargs): - return POTelSpan(*args, active=True, **kwargs) - - -def start_inactive_span(*args, **kwargs): - return POTelSpan(*args, active=False, **kwargs) - - -def start_transaction(*args, **kwargs): - # XXX force_transaction? - return POTelSpan(*args, active=True, **kwargs) - - # Circular imports from sentry_sdk.tracing_utils import (