Skip to content

Commit 4137a8d

Browse files
authored
feat(tracing): Add types for traces_sampler implementation (#864)
- Types for the `traces_sampler` itself (the function and its input) - A new attribute on the `Transaction` class tracking the parent sampling decision separately from the sampling decision of the transaction itself, since part of the `traces_sampler` spec is that there needs to be a difference between an inherited decision and an explicitly set decision.
1 parent 2348f52 commit 4137a8d

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

sentry_sdk/_types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66

77
if MYPY:
8+
from numbers import Real
89
from types import TracebackType
910
from typing import Any
1011
from typing import Callable
1112
from typing import Dict
1213
from typing import Optional
1314
from typing import Tuple
1415
from typing import Type
16+
from typing import Union
1517
from typing_extensions import Literal
1618

1719
ExcInfo = Tuple[
@@ -24,10 +26,14 @@
2426
Breadcrumb = Dict[str, Any]
2527
BreadcrumbHint = Dict[str, Any]
2628

29+
SamplingContext = Dict[str, Any]
30+
2731
EventProcessor = Callable[[Event, Hint], Optional[Event]]
2832
ErrorProcessor = Callable[[Event, ExcInfo], Optional[Event]]
2933
BreadcrumbProcessor = Callable[[Breadcrumb, BreadcrumbHint], Optional[Breadcrumb]]
3034

35+
TracesSampler = Callable[[SamplingContext], Union[Real, bool]]
36+
3137
# https://github.com/python/mypy/issues/5710
3238
NotImplementedType = Any
3339

sentry_sdk/consts.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
from sentry_sdk.transport import Transport
1515
from sentry_sdk.integrations import Integration
1616

17-
from sentry_sdk._types import Event, EventProcessor, BreadcrumbProcessor
17+
from sentry_sdk._types import (
18+
BreadcrumbProcessor,
19+
Event,
20+
EventProcessor,
21+
TracesSampler,
22+
)
1823

1924
# Experiments are feature flags to enable and disable certain unstable SDK
2025
# functionality. Changing them from the defaults (`None`) in production
@@ -65,6 +70,7 @@ def __init__(
6570
ca_certs=None, # type: Optional[str]
6671
propagate_traces=True, # type: bool
6772
traces_sample_rate=0.0, # type: float
73+
traces_sampler=None, # type: Optional[TracesSampler]
6874
auto_enabling_integrations=True, # type: bool
6975
_experiments={}, # type: Experiments # noqa: B006
7076
):

sentry_sdk/tracing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,12 @@ def get_trace_context(self):
449449

450450

451451
class Transaction(Span):
452-
__slots__ = ("name",)
452+
__slots__ = ("name", "parent_sampled")
453453

454454
def __init__(
455455
self,
456456
name="", # type: str
457+
parent_sampled=None, # type: Optional[bool]
457458
**kwargs # type: Any
458459
):
459460
# type: (...) -> None
@@ -468,6 +469,7 @@ def __init__(
468469
name = kwargs.pop("transaction")
469470
Span.__init__(self, **kwargs)
470471
self.name = name
472+
self.parent_sampled = parent_sampled
471473

472474
def __repr__(self):
473475
# type: () -> str

0 commit comments

Comments
 (0)