Skip to content

Commit 7ee8e77

Browse files
ref(api): Type hinting for start_transaction kwargs (#2796)
This PR adds be type hints for the `**kwargs` that can be passed to `sentry_sdk.start_transaction`, thereby clearly documenting the parameters that can be passed directly in the code. Ref getsentry/sentry-docs#5082 - We intend to add to the docs page at least the most useful arguments defined in the `TransactionKwargs` type that this PR introduces. --------- Co-authored-by: Anton Pirker <[email protected]>
1 parent 79871a8 commit 7ee8e77

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

sentry_sdk/api.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from typing import ContextManager
1818
from typing import Union
1919

20+
from typing_extensions import Unpack
21+
2022
from sentry_sdk.client import BaseClient
2123
from sentry_sdk._types import (
2224
Event,
@@ -26,6 +28,7 @@
2628
ExcInfo,
2729
MeasurementUnit,
2830
)
31+
from sentry_sdk.scope import StartTransactionKwargs
2932
from sentry_sdk.tracing import Span
3033

3134
T = TypeVar("T")
@@ -278,7 +281,7 @@ def start_span(
278281
@scopemethod
279282
def start_transaction(
280283
transaction=None, # type: Optional[Transaction]
281-
**kwargs, # type: Any
284+
**kwargs, # type: Unpack[StartTransactionKwargs]
282285
):
283286
# type: (...) -> Union[Transaction, NoOpSpan]
284287
return Scope.get_current_scope().start_transaction(transaction, **kwargs)

sentry_sdk/hub.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
from typing import TypeVar
3232
from typing import Union
3333

34+
from typing_extensions import Unpack
35+
3436
from sentry_sdk.client import BaseClient
3537
from sentry_sdk.integrations import Integration
3638
from sentry_sdk._types import (
@@ -41,6 +43,7 @@
4143
ExcInfo,
4244
)
4345
from sentry_sdk.consts import ClientConstructor
46+
from sentry_sdk.scope import StartTransactionKwargs
4447

4548
T = TypeVar("T")
4649

@@ -468,7 +471,7 @@ def start_span(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
468471
def start_transaction(
469472
self, transaction=None, instrumenter=INSTRUMENTER.SENTRY, **kwargs
470473
):
471-
# type: (Optional[Transaction], str, Any) -> Union[Transaction, NoOpSpan]
474+
# type: (Optional[Transaction], str, Unpack[StartTransactionKwargs]) -> Union[Transaction, NoOpSpan]
472475
"""
473476
.. deprecated:: 2.0.0
474477
This function is deprecated and will be removed in a future release.
@@ -501,7 +504,8 @@ def start_transaction(
501504

502505
# For backwards compatibility, we allow passing the scope as the hub.
503506
# We need a major release to make this nice. (if someone searches the code: deprecated)
504-
kwargs["hub"] = scope
507+
# Type checking disabled for this line because deprecated keys are not allowed in the type signature.
508+
kwargs["hub"] = scope # type: ignore
505509

506510
return scope.start_transaction(
507511
transaction=transaction, instrumenter=instrumenter, **kwargs

sentry_sdk/scope.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
from typing import TypeVar
5050
from typing import Union
5151

52+
from typing_extensions import Unpack
53+
5254
from sentry_sdk._types import (
5355
Breadcrumb,
5456
BreadcrumbHint,
@@ -57,11 +59,18 @@
5759
EventProcessor,
5860
ExcInfo,
5961
Hint,
62+
SamplingContext,
6063
Type,
6164
)
6265

66+
from sentry_sdk.tracing import TransactionKwargs
67+
6368
import sentry_sdk
6469

70+
class StartTransactionKwargs(TransactionKwargs, total=False):
71+
client: Optional["sentry_sdk.Client"]
72+
custom_sampling_context: SamplingContext
73+
6574
P = ParamSpec("P")
6675
R = TypeVar("R")
6776

@@ -935,7 +944,7 @@ def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
935944
def start_transaction(
936945
self, transaction=None, instrumenter=INSTRUMENTER.SENTRY, **kwargs
937946
):
938-
# type: (Optional[Transaction], str, Any) -> Union[Transaction, NoOpSpan]
947+
# type: (Optional[Transaction], str, Unpack[StartTransactionKwargs]) -> Union[Transaction, NoOpSpan]
939948
"""
940949
Start and return a transaction.
941950
@@ -971,9 +980,13 @@ def start_transaction(
971980

972981
custom_sampling_context = kwargs.pop("custom_sampling_context", {})
973982

983+
# kwargs at this point has type TransactionKwargs, since we have removed
984+
# the client and custom_sampling_context from it.
985+
transaction_kwargs = kwargs # type: TransactionKwargs
986+
974987
# if we haven't been given a transaction, make one
975988
if transaction is None:
976-
transaction = Transaction(**kwargs)
989+
transaction = Transaction(**transaction_kwargs)
977990

978991
# use traces_sample_rate, traces_sampler, and/or inheritance to make a
979992
# sampling decision

sentry_sdk/tracing.py

+29-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,35 @@
2222
from typing import Union
2323
from typing import TypeVar
2424

25+
from typing_extensions import TypedDict, Unpack
26+
2527
P = ParamSpec("P")
2628
R = TypeVar("R")
2729

2830
import sentry_sdk.profiler
2931
from sentry_sdk._types import Event, MeasurementUnit, SamplingContext
3032

33+
class SpanKwargs(TypedDict, total=False):
34+
trace_id: str
35+
span_id: str
36+
parent_span_id: str
37+
same_process_as_parent: bool
38+
sampled: bool
39+
op: str
40+
description: str
41+
# hub: Optional[sentry_sdk.Hub] is deprecated, and therefore omitted here!
42+
status: str
43+
# transaction: str is deprecated, and therefore omitted here!
44+
containing_transaction: Optional["Transaction"]
45+
start_timestamp: Optional[Union[datetime, float]]
46+
scope: "sentry_sdk.Scope"
47+
48+
class TransactionKwargs(SpanKwargs, total=False):
49+
name: str
50+
source: str
51+
parent_sampled: bool
52+
baggage: "Baggage"
53+
3154

3255
BAGGAGE_HEADER_NAME = "baggage"
3356
SENTRY_TRACE_HEADER_NAME = "sentry-trace"
@@ -252,7 +275,7 @@ def start_child(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
252275
trace_id=self.trace_id,
253276
parent_span_id=self.span_id,
254277
containing_transaction=self.containing_transaction,
255-
**kwargs
278+
**kwargs,
256279
)
257280

258281
span_recorder = (
@@ -267,7 +290,7 @@ def start_child(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
267290
def continue_from_environ(
268291
cls,
269292
environ, # type: Mapping[str, str]
270-
**kwargs # type: Any
293+
**kwargs, # type: Any
271294
):
272295
# type: (...) -> Transaction
273296
"""
@@ -293,7 +316,7 @@ def continue_from_environ(
293316
def continue_from_headers(
294317
cls,
295318
headers, # type: Mapping[str, str]
296-
**kwargs # type: Any
319+
**kwargs, # type: Any
297320
):
298321
# type: (...) -> Transaction
299322
"""
@@ -349,7 +372,7 @@ def iter_headers(self):
349372
def from_traceparent(
350373
cls,
351374
traceparent, # type: Optional[str]
352-
**kwargs # type: Any
375+
**kwargs, # type: Any
353376
):
354377
# type: (...) -> Optional[Transaction]
355378
"""
@@ -559,7 +582,7 @@ def __init__(
559582
parent_sampled=None, # type: Optional[bool]
560583
baggage=None, # type: Optional[Baggage]
561584
source=TRANSACTION_SOURCE_CUSTOM, # type: str
562-
**kwargs # type: Any
585+
**kwargs, # type: Unpack[SpanKwargs]
563586
):
564587
# type: (...) -> None
565588
"""Constructs a new Transaction.
@@ -583,7 +606,7 @@ def __init__(
583606
"Deprecated: use Transaction(name=...) to create transactions "
584607
"instead of Span(transaction=...)."
585608
)
586-
name = kwargs.pop("transaction")
609+
name = kwargs.pop("transaction") # type: ignore
587610

588611
super().__init__(**kwargs)
589612

0 commit comments

Comments
 (0)