Skip to content

Commit a5f5a8d

Browse files
ref: Make set_custom_sampling_context() a classmethod (#6238)
Allow users to call `set_custom_sampling_context()` directly on the class since the method only operates on the current scope and does not depend on the instance.
1 parent 640c48d commit a5f5a8d

5 files changed

Lines changed: 12 additions & 16 deletions

File tree

sentry_sdk/integrations/aiohttp.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
request_body_within_bounds,
1717
)
1818
from sentry_sdk.integrations.logging import ignore_logger
19-
from sentry_sdk.scope import should_send_default_pii
19+
from sentry_sdk.scope import should_send_default_pii, Scope
2020
from sentry_sdk.sessions import track_session
2121
from sentry_sdk.traces import (
2222
SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE,
@@ -139,9 +139,7 @@ async def sentry_app_handle(
139139
span_ctx: "ContextManager[Union[Span, StreamedSpan]]"
140140
if is_span_streaming_enabled:
141141
sentry_sdk.traces.continue_trace(headers)
142-
sentry_sdk.get_current_scope().set_custom_sampling_context(
143-
{"aiohttp_request": request}
144-
)
142+
Scope.set_custom_sampling_context({"aiohttp_request": request})
145143

146144
header_attributes: "dict[str, Any]" = {}
147145
for header, header_value in _filter_headers(

sentry_sdk/integrations/asgi.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
capture_internal_exceptions,
4747
qualname_from_function,
4848
)
49+
from sentry_sdk.scope import Scope
4950

5051
from typing import TYPE_CHECKING
5152

@@ -257,9 +258,7 @@ async def _run_app(
257258
):
258259
sentry_sdk.traces.continue_trace(_get_headers(scope))
259260

260-
sentry_scope.set_custom_sampling_context(
261-
{"asgi_scope": scope}
262-
)
261+
Scope.set_custom_sampling_context({"asgi_scope": scope})
263262

264263
attributes["sentry.op"] = f"{ty}.server"
265264
segment = sentry_sdk.traces.start_span(
@@ -270,9 +269,7 @@ async def _run_app(
270269
else:
271270
sentry_sdk.traces.new_trace()
272271

273-
sentry_scope.set_custom_sampling_context(
274-
{"asgi_scope": scope}
275-
)
272+
Scope.set_custom_sampling_context({"asgi_scope": scope})
276273

277274
attributes["sentry.op"] = OP.HTTP_SERVER
278275
segment = sentry_sdk.traces.start_span(

sentry_sdk/integrations/celery/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
)
1515
from sentry_sdk.integrations.celery.utils import _now_seconds_since_epoch
1616
from sentry_sdk.integrations.logging import ignore_logger
17-
from sentry_sdk.scope import should_send_default_pii
17+
from sentry_sdk.scope import should_send_default_pii, Scope
1818
from sentry_sdk.traces import StreamedSpan, _get_current_streamed_span
1919
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, Span, TransactionSource
2020
from sentry_sdk.tracing_utils import Baggage, has_span_streaming_enabled
@@ -361,7 +361,7 @@ def _inner(*args: "Any", **kwargs: "Any") -> "Any":
361361
headers = args[3].get("headers") or {}
362362
if span_streaming:
363363
sentry_sdk.traces.continue_trace(headers)
364-
scope.set_custom_sampling_context(custom_sampling_context)
364+
Scope.set_custom_sampling_context(custom_sampling_context)
365365
span = sentry_sdk.traces.start_span(
366366
name=task_name,
367367
parent_span=None, # make this a segment

sentry_sdk/integrations/wsgi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
_filter_headers,
1212
nullcontext,
1313
)
14-
from sentry_sdk.scope import should_send_default_pii, use_isolation_scope
14+
from sentry_sdk.scope import should_send_default_pii, use_isolation_scope, Scope
1515
from sentry_sdk.sessions import track_session
1616
from sentry_sdk.traces import StreamedSpan, SegmentSource
1717
from sentry_sdk.tracing import Span, TransactionSource
@@ -132,7 +132,7 @@ def __call__(
132132
sentry_sdk.traces.continue_trace(
133133
dict(_get_headers(environ))
134134
)
135-
scope.set_custom_sampling_context({"wsgi_environ": environ})
135+
Scope.set_custom_sampling_context({"wsgi_environ": environ})
136136

137137
span_ctx = sentry_sdk.traces.start_span(
138138
name=_DEFAULT_TRANSACTION_NAME,

sentry_sdk/scope.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,11 @@ def get_active_propagation_context(self) -> "PropagationContext":
719719
isolation_scope._propagation_context = PropagationContext()
720720
return isolation_scope._propagation_context
721721

722+
@classmethod
722723
def set_custom_sampling_context(
723-
self, custom_sampling_context: "dict[str, Any]"
724+
cls, custom_sampling_context: "dict[str, Any]"
724725
) -> None:
725-
self.get_current_scope().get_active_propagation_context()._set_custom_sampling_context(
726+
cls.get_current_scope().get_active_propagation_context()._set_custom_sampling_context(
726727
custom_sampling_context
727728
)
728729

0 commit comments

Comments
 (0)