Skip to content

Commit

Permalink
Create spans on scope
Browse files Browse the repository at this point in the history
  • Loading branch information
sentrivana committed Aug 12, 2024
1 parent 76ccff5 commit 58e75d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
15 changes: 11 additions & 4 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import inspect

from sentry_sdk import tracing, tracing_utils, Client
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.crons import monitor
Expand Down Expand Up @@ -233,14 +233,17 @@ def flush(


def start_span(
*,
root_span=None,
custom_sampling_context=None,
**kwargs, # type: Any
):
# type: (...) -> POTelSpan
"""
Alias for tracing.POTelSpan constructor. The method signature is the same.
Start and return a span.
"""
# TODO: Consider adding type hints to the method signature.
return tracing.POTelSpan(**kwargs)
return get_current_scope().start_span(root_span, custom_sampling_context, **kwargs)


def start_transaction(
Expand Down Expand Up @@ -282,7 +285,11 @@ def start_transaction(
constructor. See :py:class:`sentry_sdk.tracing.Transaction` for
available arguments.
"""
return start_span(**kwargs)
return get_current_scope().start_span(
root_span=transaction,
custom_sampling_context=custom_sampling_context,
**kwargs,
)


def set_measurement(name, value, unit=""):
Expand Down
29 changes: 10 additions & 19 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
NoOpSpan,
Span,
Transaction,
POTelSpan,
)
from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.utils import (
Expand Down Expand Up @@ -963,6 +964,10 @@ 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 @@ -999,13 +1004,9 @@ def start_transaction(

custom_sampling_context = custom_sampling_context or {}

# kwargs at this point has type TransactionKwargs, since we have removed
# the client and custom_sampling_context from it.
transaction_kwargs = kwargs # type: TransactionKwargs

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

# use traces_sample_rate, traces_sampler, and/or inheritance to make a
# sampling decision
Expand All @@ -1031,26 +1032,16 @@ def start_transaction(

return transaction

def start_span(self, **kwargs):
# type: (Any) -> Span
def start_span(self, root_span=None, custom_sampling_context=None, **kwargs):
# type: (Optional[Span], Optional[SamplingContext], Any) -> Span
"""
Start a span whose parent is the currently active span or transaction, if any.
Start a span whose parent is the currently active span, if any.
The return value is a :py:class:`sentry_sdk.tracing.Span` instance,
typically used as a context manager to start and stop timing in a `with`
block.
Only spans contained in a transaction are sent to Sentry. Most
integrations start a transaction at the appropriate time, for example
for every incoming HTTP request. Use
:py:meth:`sentry_sdk.start_transaction` to start a new transaction when
one is not already in progress.
For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Span`.
The instrumenter parameter is deprecated for user code, and it will
be removed in the next major version. Going forward, it should only
be used by the SDK itself.
"""
with new_scope():
kwargs.setdefault("scope", self)
Expand All @@ -1065,7 +1056,7 @@ def start_span(self, **kwargs):
if propagation_context is not None:
kwargs["trace_id"] = propagation_context.trace_id

span = Span(**kwargs)
span = POTelSpan(**kwargs)
else:
# Children take `trace_id`` from the parent span.
span = span.start_child(**kwargs)
Expand Down

0 comments on commit 58e75d6

Please sign in to comment.