Skip to content

ref: Improve start_transaction docs #2920

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
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
9 changes: 5 additions & 4 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
ExcInfo,
MeasurementUnit,
LogLevelStr,
SamplingContext,
)
from sentry_sdk.scope import StartTransactionKwargs
from sentry_sdk.tracing import Span
from sentry_sdk.tracing import Span, TransactionKwargs

T = TypeVar("T")
F = TypeVar("F", bound=Callable[..., Any])
Expand Down Expand Up @@ -284,11 +284,12 @@ def start_span(
def start_transaction(
transaction=None, # type: Optional[Transaction]
instrumenter=INSTRUMENTER.SENTRY, # type: str
**kwargs, # type: Unpack[StartTransactionKwargs]
custom_sampling_context=None, # type: Optional[SamplingContext]
**kwargs, # type: Unpack[TransactionKwargs]
):
# type: (...) -> Union[Transaction, NoOpSpan]
return Scope.get_current_scope().start_transaction(
transaction, instrumenter, **kwargs
transaction, instrumenter, custom_sampling_context, **kwargs
)


Expand Down
13 changes: 9 additions & 4 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
BreadcrumbHint,
ExcInfo,
LogLevelStr,
SamplingContext,
)
from sentry_sdk.consts import ClientConstructor
from sentry_sdk.scope import StartTransactionKwargs
from sentry_sdk.tracing import TransactionKwargs

T = TypeVar("T")

Expand Down Expand Up @@ -472,9 +473,13 @@ def start_span(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
return scope.start_span(instrumenter=instrumenter, **kwargs)

def start_transaction(
self, transaction=None, instrumenter=INSTRUMENTER.SENTRY, **kwargs
self,
transaction=None,
instrumenter=INSTRUMENTER.SENTRY,
custom_sampling_context=None,
**kwargs
):
# type: (Optional[Transaction], str, Unpack[StartTransactionKwargs]) -> Union[Transaction, NoOpSpan]
# type: (Optional[Transaction], str, Optional[SamplingContext], Unpack[TransactionKwargs]) -> Union[Transaction, NoOpSpan]
"""
.. deprecated:: 2.0.0
This function is deprecated and will be removed in a future release.
Expand Down Expand Up @@ -511,7 +516,7 @@ def start_transaction(
kwargs["hub"] = scope # type: ignore

return scope.start_transaction(
transaction=transaction, instrumenter=instrumenter, **kwargs
transaction, instrumenter, custom_sampling_context, **kwargs
)

def continue_trace(self, environ_or_headers, op=None, name=None, source=None):
Expand Down
22 changes: 14 additions & 8 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@

import sentry_sdk

class StartTransactionKwargs(TransactionKwargs, total=False):
client: Optional["sentry_sdk.Client"]
custom_sampling_context: SamplingContext

P = ParamSpec("P")
R = TypeVar("R")

Expand Down Expand Up @@ -966,9 +962,13 @@ def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
self._breadcrumbs.popleft()

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

Expand All @@ -991,7 +991,13 @@ def start_transaction(
When the transaction is finished, it will be sent to Sentry with all its
finished child spans.

For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Transaction`.
:param transaction: The transaction to start. If omitted, we create and
start a new transaction.
:param instrumenter: This parameter is meant for internal use only.
:param custom_sampling_context: The transaction's custom sampling context.
:param kwargs: Optional keyword arguments to be passed to the Transaction
constructor. See :py:class:`sentry_sdk.tracing.Transaction` for
available arguments.
"""
kwargs.setdefault("scope", self)

Expand All @@ -1002,7 +1008,7 @@ def start_transaction(
if instrumenter != configuration_instrumenter:
return NoOpSpan()

custom_sampling_context = kwargs.pop("custom_sampling_context", {})
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.
Expand Down