From cd065e031c6caf3f5c89b36c540b5e362bcaf2e0 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Fri, 8 May 2026 12:37:26 +0200 Subject: [PATCH] ref: Make set_custom_sampling_context a classmethod --- sentry_sdk/integrations/aiohttp.py | 6 ++---- sentry_sdk/integrations/asgi.py | 9 +++------ sentry_sdk/integrations/celery/__init__.py | 4 ++-- sentry_sdk/integrations/wsgi.py | 4 ++-- sentry_sdk/scope.py | 5 +++-- 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/sentry_sdk/integrations/aiohttp.py b/sentry_sdk/integrations/aiohttp.py index 26f96a672f..af3a4baca4 100644 --- a/sentry_sdk/integrations/aiohttp.py +++ b/sentry_sdk/integrations/aiohttp.py @@ -16,7 +16,7 @@ request_body_within_bounds, ) from sentry_sdk.integrations.logging import ignore_logger -from sentry_sdk.scope import should_send_default_pii +from sentry_sdk.scope import should_send_default_pii, Scope from sentry_sdk.sessions import track_session from sentry_sdk.traces import ( SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE, @@ -139,9 +139,7 @@ async def sentry_app_handle( span_ctx: "ContextManager[Union[Span, StreamedSpan]]" if is_span_streaming_enabled: sentry_sdk.traces.continue_trace(headers) - sentry_sdk.get_current_scope().set_custom_sampling_context( - {"aiohttp_request": request} - ) + Scope.set_custom_sampling_context({"aiohttp_request": request}) header_attributes: "dict[str, Any]" = {} for header, header_value in _filter_headers( diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index 151b5c58ee..ef75bce0bd 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -46,6 +46,7 @@ capture_internal_exceptions, qualname_from_function, ) +from sentry_sdk.scope import Scope from typing import TYPE_CHECKING @@ -257,9 +258,7 @@ async def _run_app( ): sentry_sdk.traces.continue_trace(_get_headers(scope)) - sentry_scope.set_custom_sampling_context( - {"asgi_scope": scope} - ) + Scope.set_custom_sampling_context({"asgi_scope": scope}) attributes["sentry.op"] = f"{ty}.server" segment = sentry_sdk.traces.start_span( @@ -270,9 +269,7 @@ async def _run_app( else: sentry_sdk.traces.new_trace() - sentry_scope.set_custom_sampling_context( - {"asgi_scope": scope} - ) + Scope.set_custom_sampling_context({"asgi_scope": scope}) attributes["sentry.op"] = OP.HTTP_SERVER segment = sentry_sdk.traces.start_span( diff --git a/sentry_sdk/integrations/celery/__init__.py b/sentry_sdk/integrations/celery/__init__.py index 6eb295d81c..8c01e4ec93 100644 --- a/sentry_sdk/integrations/celery/__init__.py +++ b/sentry_sdk/integrations/celery/__init__.py @@ -14,7 +14,7 @@ ) from sentry_sdk.integrations.celery.utils import _now_seconds_since_epoch from sentry_sdk.integrations.logging import ignore_logger -from sentry_sdk.scope import should_send_default_pii +from sentry_sdk.scope import should_send_default_pii, Scope from sentry_sdk.traces import StreamedSpan, _get_current_streamed_span from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, Span, TransactionSource from sentry_sdk.tracing_utils import Baggage, has_span_streaming_enabled @@ -361,7 +361,7 @@ def _inner(*args: "Any", **kwargs: "Any") -> "Any": headers = args[3].get("headers") or {} if span_streaming: sentry_sdk.traces.continue_trace(headers) - scope.set_custom_sampling_context(custom_sampling_context) + Scope.set_custom_sampling_context(custom_sampling_context) span = sentry_sdk.traces.start_span( name=task_name, parent_span=None, # make this a segment diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index 91d26ba8ac..89478f4483 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -11,7 +11,7 @@ _filter_headers, nullcontext, ) -from sentry_sdk.scope import should_send_default_pii, use_isolation_scope +from sentry_sdk.scope import should_send_default_pii, use_isolation_scope, Scope from sentry_sdk.sessions import track_session from sentry_sdk.traces import StreamedSpan, SegmentSource from sentry_sdk.tracing import Span, TransactionSource @@ -132,7 +132,7 @@ def __call__( sentry_sdk.traces.continue_trace( dict(_get_headers(environ)) ) - scope.set_custom_sampling_context({"wsgi_environ": environ}) + Scope.set_custom_sampling_context({"wsgi_environ": environ}) span_ctx = sentry_sdk.traces.start_span( name=_DEFAULT_TRANSACTION_NAME, diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 9c9b0c152f..06aad5857f 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -719,10 +719,11 @@ def get_active_propagation_context(self) -> "PropagationContext": isolation_scope._propagation_context = PropagationContext() return isolation_scope._propagation_context + @classmethod def set_custom_sampling_context( - self, custom_sampling_context: "dict[str, Any]" + cls, custom_sampling_context: "dict[str, Any]" ) -> None: - self.get_current_scope().get_active_propagation_context()._set_custom_sampling_context( + cls.get_current_scope().get_active_propagation_context()._set_custom_sampling_context( custom_sampling_context )