Skip to content

Commit d3d8f1f

Browse files
docs: Document Transaction and Span kwargs typed dicts (#2923)
Repeating the doc comments also on the kwargs typed dicts enables better hinting in VSCode ref: getsentry/sentry-docs#5082
1 parent 66f530a commit d3d8f1f

File tree

4 files changed

+75
-17
lines changed

4 files changed

+75
-17
lines changed

sentry_sdk/api.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
ExcInfo,
3030
MeasurementUnit,
3131
LogLevelStr,
32+
SamplingContext,
3233
)
33-
from sentry_sdk.scope import StartTransactionKwargs
34-
from sentry_sdk.tracing import Span
34+
from sentry_sdk.tracing import Span, TransactionKwargs
3535

3636
T = TypeVar("T")
3737
F = TypeVar("F", bound=Callable[..., Any])
@@ -284,11 +284,12 @@ def start_span(
284284
def start_transaction(
285285
transaction=None, # type: Optional[Transaction]
286286
instrumenter=INSTRUMENTER.SENTRY, # type: str
287-
**kwargs, # type: Unpack[StartTransactionKwargs]
287+
custom_sampling_context=None, # type: Optional[SamplingContext]
288+
**kwargs, # type: Unpack[TransactionKwargs]
288289
):
289290
# type: (...) -> Union[Transaction, NoOpSpan]
290291
return Scope.get_current_scope().start_transaction(
291-
transaction, instrumenter, **kwargs
292+
transaction, instrumenter, custom_sampling_context, **kwargs
292293
)
293294

294295

sentry_sdk/hub.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@
4242
BreadcrumbHint,
4343
ExcInfo,
4444
LogLevelStr,
45+
SamplingContext,
4546
)
4647
from sentry_sdk.consts import ClientConstructor
47-
from sentry_sdk.scope import StartTransactionKwargs
48+
from sentry_sdk.tracing import TransactionKwargs
4849

4950
T = TypeVar("T")
5051

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

474475
def start_transaction(
475-
self, transaction=None, instrumenter=INSTRUMENTER.SENTRY, **kwargs
476+
self,
477+
transaction=None,
478+
instrumenter=INSTRUMENTER.SENTRY,
479+
custom_sampling_context=None,
480+
**kwargs
476481
):
477-
# type: (Optional[Transaction], str, Unpack[StartTransactionKwargs]) -> Union[Transaction, NoOpSpan]
482+
# type: (Optional[Transaction], str, Optional[SamplingContext], Unpack[TransactionKwargs]) -> Union[Transaction, NoOpSpan]
478483
"""
479484
.. deprecated:: 2.0.0
480485
This function is deprecated and will be removed in a future release.
@@ -511,7 +516,7 @@ def start_transaction(
511516
kwargs["hub"] = scope # type: ignore
512517

513518
return scope.start_transaction(
514-
transaction=transaction, instrumenter=instrumenter, **kwargs
519+
transaction, instrumenter, custom_sampling_context, **kwargs
515520
)
516521

517522
def continue_trace(self, environ_or_headers, op=None, name=None, source=None):

sentry_sdk/scope.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@
7070

7171
import sentry_sdk
7272

73-
class StartTransactionKwargs(TransactionKwargs, total=False):
74-
client: Optional["sentry_sdk.Client"]
75-
custom_sampling_context: SamplingContext
76-
7773
P = ParamSpec("P")
7874
R = TypeVar("R")
7975

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

968964
def start_transaction(
969-
self, transaction=None, instrumenter=INSTRUMENTER.SENTRY, **kwargs
965+
self,
966+
transaction=None,
967+
instrumenter=INSTRUMENTER.SENTRY,
968+
custom_sampling_context=None,
969+
**kwargs
970970
):
971-
# type: (Optional[Transaction], str, Unpack[StartTransactionKwargs]) -> Union[Transaction, NoOpSpan]
971+
# type: (Optional[Transaction], str, Optional[SamplingContext], Unpack[TransactionKwargs]) -> Union[Transaction, NoOpSpan]
972972
"""
973973
Start and return a transaction.
974974
@@ -991,7 +991,13 @@ def start_transaction(
991991
When the transaction is finished, it will be sent to Sentry with all its
992992
finished child spans.
993993
994-
For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Transaction`.
994+
:param transaction: The transaction to start. If omitted, we create and
995+
start a new transaction.
996+
:param instrumenter: This parameter is meant for internal use only.
997+
:param custom_sampling_context: The transaction's custom sampling context.
998+
:param kwargs: Optional keyword arguments to be passed to the Transaction
999+
constructor. See :py:class:`sentry_sdk.tracing.Transaction` for
1000+
available arguments.
9951001
"""
9961002
kwargs.setdefault("scope", self)
9971003

@@ -1002,7 +1008,7 @@ def start_transaction(
10021008
if instrumenter != configuration_instrumenter:
10031009
return NoOpSpan()
10041010

1005-
custom_sampling_context = kwargs.pop("custom_sampling_context", {})
1011+
custom_sampling_context = custom_sampling_context or {}
10061012

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

sentry_sdk/tracing.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,69 @@
3636

3737
class SpanKwargs(TypedDict, total=False):
3838
trace_id: str
39+
"""
40+
The trace ID of the root span. If this new span is to be the root span,
41+
omit this parameter, and a new trace ID will be generated.
42+
"""
43+
3944
span_id: str
45+
"""The span ID of this span. If omitted, a new span ID will be generated."""
46+
4047
parent_span_id: str
48+
"""The span ID of the parent span, if applicable."""
49+
4150
same_process_as_parent: bool
51+
"""Whether this span is in the same process as the parent span."""
52+
4253
sampled: bool
54+
"""
55+
Whether the span should be sampled. Overrides the default sampling decision
56+
for this span when provided.
57+
"""
58+
4359
op: str
60+
"""
61+
The span's operation. A list of recommended values is available here:
62+
https://develop.sentry.dev/sdk/performance/span-operations/
63+
"""
64+
4465
description: str
45-
# hub: Optional[sentry_sdk.Hub] is deprecated, and therefore omitted here!
66+
"""A description of what operation is being performed within the span."""
67+
68+
hub: Optional["sentry_sdk.Hub"]
69+
"""The hub to use for this span. This argument is DEPRECATED. Please use the `scope` parameter, instead."""
70+
4671
status: str
72+
"""The span's status. Possible values are listed at https://develop.sentry.dev/sdk/event-payloads/span/"""
73+
4774
containing_transaction: Optional["Transaction"]
75+
"""The transaction that this span belongs to."""
76+
4877
start_timestamp: Optional[Union[datetime, float]]
78+
"""
79+
The timestamp when the span started. If omitted, the current time
80+
will be used.
81+
"""
82+
4983
scope: "sentry_sdk.Scope"
84+
"""The scope to use for this span. If not provided, we use the current scope."""
5085

5186
class TransactionKwargs(SpanKwargs, total=False):
5287
name: str
88+
"""Identifier of the transaction. Will show up in the Sentry UI."""
89+
5390
source: str
91+
"""
92+
A string describing the source of the transaction name. This will be used to determine the transaction's type.
93+
See https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations for more information.
94+
Default "custom".
95+
"""
96+
5497
parent_sampled: bool
98+
"""Whether the parent transaction was sampled. If True this transaction will be kept, if False it will be discarded."""
99+
55100
baggage: "Baggage"
101+
"""The W3C baggage header value. (see https://www.w3.org/TR/baggage/)"""
56102

57103

58104
BAGGAGE_HEADER_NAME = "baggage"

0 commit comments

Comments
 (0)