Skip to content

Commit

Permalink
feat(tracing): Add types for traces_sampler implementation (#864)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
lobsterkatie authored Oct 21, 2020
1 parent 2348f52 commit 4137a8d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@


if MYPY:
from numbers import Real
from types import TracebackType
from typing import Any
from typing import Callable
from typing import Dict
from typing import Optional
from typing import Tuple
from typing import Type
from typing import Union
from typing_extensions import Literal

ExcInfo = Tuple[
Expand All @@ -24,10 +26,14 @@
Breadcrumb = Dict[str, Any]
BreadcrumbHint = Dict[str, Any]

SamplingContext = Dict[str, Any]

EventProcessor = Callable[[Event, Hint], Optional[Event]]
ErrorProcessor = Callable[[Event, ExcInfo], Optional[Event]]
BreadcrumbProcessor = Callable[[Breadcrumb, BreadcrumbHint], Optional[Breadcrumb]]

TracesSampler = Callable[[SamplingContext], Union[Real, bool]]

# https://github.com/python/mypy/issues/5710
NotImplementedType = Any

Expand Down
8 changes: 7 additions & 1 deletion sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
from sentry_sdk.transport import Transport
from sentry_sdk.integrations import Integration

from sentry_sdk._types import Event, EventProcessor, BreadcrumbProcessor
from sentry_sdk._types import (
BreadcrumbProcessor,
Event,
EventProcessor,
TracesSampler,
)

# Experiments are feature flags to enable and disable certain unstable SDK
# functionality. Changing them from the defaults (`None`) in production
Expand Down Expand Up @@ -65,6 +70,7 @@ def __init__(
ca_certs=None, # type: Optional[str]
propagate_traces=True, # type: bool
traces_sample_rate=0.0, # type: float
traces_sampler=None, # type: Optional[TracesSampler]
auto_enabling_integrations=True, # type: bool
_experiments={}, # type: Experiments # noqa: B006
):
Expand Down
4 changes: 3 additions & 1 deletion sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,12 @@ def get_trace_context(self):


class Transaction(Span):
__slots__ = ("name",)
__slots__ = ("name", "parent_sampled")

def __init__(
self,
name="", # type: str
parent_sampled=None, # type: Optional[bool]
**kwargs # type: Any
):
# type: (...) -> None
Expand All @@ -468,6 +469,7 @@ def __init__(
name = kwargs.pop("transaction")
Span.__init__(self, **kwargs)
self.name = name
self.parent_sampled = parent_sampled

def __repr__(self):
# type: () -> str
Expand Down

0 comments on commit 4137a8d

Please sign in to comment.