Skip to content

Commit

Permalink
ref(tracing): Simplify backwards-compat code
Browse files Browse the repository at this point in the history
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__`
  - Completely remove `start_inactive_span`, since inactive spans can
    be created by setting `active=False` when constructing a
    `POTelSpan`.
  - Handle backwards-compatibility in top-level API by stripping any
    invalid `kwargs` before calling the `POTelSpan` constructor.
  • Loading branch information
szokeasaurusrex committed Jul 30, 2024
1 parent 144d976 commit 971642b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
20 changes: 12 additions & 8 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ def overload(x):
"set_tags",
"set_user",
"start_span",
"start_inactive_span",
"start_transaction",
"trace",
"monitor",
]


_potel_span_init_kwargs = None


def scopemethod(f):
# type: (F) -> F
f.__doc__ = "%s\n\n%s" % (
Expand Down Expand Up @@ -339,14 +341,16 @@ def start_span(
**kwargs, # type: Any
):
# type: (...) -> POTelSpan
return tracing.start_span(**kwargs)
global _potel_span_init_kwargs

if _potel_span_init_kwargs is None:
_potel_span_init_kwargs = inspect.signature(tracing.POTelSpan).parameters.keys()

def start_inactive_span(
**kwargs, # type: Any
):
# type: (...) -> POTelSpan
return tracing.start_inactive_span(**kwargs)
filtered_kwargs = {
kwarg: kwargs[kwarg] for kwarg in _potel_span_init_kwargs if kwarg in kwargs
}

return tracing.POTelSpan(**filtered_kwargs)


def start_transaction(
Expand Down Expand Up @@ -388,7 +392,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=""):
Expand Down
23 changes: 1 addition & 22 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,18 +1249,10 @@ 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: (...) -> None
Expand Down Expand Up @@ -1443,19 +1435,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 (
Expand Down

0 comments on commit 971642b

Please sign in to comment.