Skip to content

Commit 8fb2d0b

Browse files
committed
ref(types): add partial type hints for SamplingContext
1 parent 8f80fa7 commit 8fb2d0b

2 files changed

Lines changed: 63 additions & 14 deletions

File tree

sentry_sdk/_types.py

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import TYPE_CHECKING, TypeVar, Union
22

3-
43
# Re-exported for compat, since code out there in the wild might use this variable.
54
MYPY = TYPE_CHECKING
65

@@ -93,20 +92,14 @@ def substituted_because_contains_sensitive_data(cls) -> "AnnotatedValue":
9392

9493
if TYPE_CHECKING:
9594
from collections.abc import Container, MutableMapping, Sequence
96-
9795
from datetime import datetime
98-
9996
from types import TracebackType
100-
from typing import Any
101-
from typing import Callable
102-
from typing import Dict
103-
from typing import Mapping
104-
from typing import NotRequired
105-
from typing import Optional
106-
from typing import Tuple
107-
from typing import Type
97+
from typing import Any, Callable, Dict, Mapping, NotRequired, Optional, Type
98+
10899
from typing_extensions import Literal, TypedDict
109100

101+
from sentry_sdk.tracing import TransactionSource
102+
110103
class SDKInfo(TypedDict):
111104
name: str
112105
version: str
@@ -285,8 +278,64 @@ class SDKInfo(TypedDict):
285278
# TODO: Make a proper type definition for this (PRs welcome!)
286279
BreadcrumbHint = Dict[str, Any]
287280

288-
# TODO: Make a proper type definition for this (PRs welcome!)
289-
SamplingContext = Dict[str, Any]
281+
_ASGIInfo = TypedDict("_ASGIInfo", {"version": str, "spec_version": str})
282+
_ASGIScope = TypedDict(
283+
"_ASGIScope",
284+
{
285+
"type": str,
286+
"asgi": _ASGIInfo,
287+
"http_version": str,
288+
"server": tuple[str, int],
289+
"client": tuple[str, int],
290+
"scheme": str,
291+
"method": str,
292+
"root_path": str,
293+
"path": str,
294+
"raw_path": bytes,
295+
"query_string": bytes,
296+
"headers": list[tuple[bytes, bytes]],
297+
"state": dict[str, Any],
298+
},
299+
)
300+
_TransactionContext = TypedDict(
301+
"_TransactionContext",
302+
{
303+
"trace_id": str,
304+
"span_id": str,
305+
"parent_span_id": Optional[str],
306+
"same_process_as_parent": bool,
307+
"op": Optional[str],
308+
"description": Optional[str],
309+
"start_timestamp": datetime | int,
310+
"timestamp": Optional[datetime],
311+
"origin": str,
312+
"tags": NotRequired[dict[str, str]],
313+
"data": dict[str, Any],
314+
"name": str,
315+
"source": TransactionSource,
316+
"sampled": Optional[bool],
317+
},
318+
)
319+
_SamplingContextTyped = TypedDict(
320+
"_SamplingContextTyped",
321+
{
322+
"transaction_context": _TransactionContext,
323+
"parent_sampled": Optional[bool],
324+
"asgi_scope": NotRequired[_ASGIScope],
325+
# `wsgi_environ` is only present for WSGI server (like Django), and it contains mainly env vars, and some wsgi specifics.
326+
"wsgi_environ": NotRequired[dict[str, Any]],
327+
# `aiohttp_request` is only present for AIOHTTP server, and contains <class 'aiohttp.web_request.Request'>
328+
"aiohttp_request": NotRequired[Any],
329+
# NOT tested these below, but documented for completeness sake to pass mypy.
330+
"tornado_request": NotRequired[Any],
331+
"celery_job": NotRequired[dict[str, Any]],
332+
"rq_job": NotRequired[Any],
333+
"aws_event": NotRequired[Any],
334+
"aws_context": NotRequired[Any],
335+
"gcp_env": NotRequired[dict[str, Any]],
336+
},
337+
)
338+
SamplingContext = Union[_SamplingContextTyped, dict[str, Any]]
290339

291340
EventProcessor = Callable[[Event, Hint], Optional[Event]]
292341
ErrorProcessor = Callable[[Event, ExcInfo], Optional[Event]]

sentry_sdk/scope.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ def start_transaction(
10761076

10771077
# use traces_sample_rate, traces_sampler, and/or inheritance to make a
10781078
# sampling decision
1079-
sampling_context = {
1079+
sampling_context: "Dict[str, Any]" = {
10801080
"transaction_context": transaction.to_json(),
10811081
"parent_sampled": transaction.parent_sampled,
10821082
}

0 commit comments

Comments
 (0)