diff --git a/sentry_sdk/integrations/aiohttp.py b/sentry_sdk/integrations/aiohttp.py index 6da340f31c..f10b5079a7 100644 --- a/sentry_sdk/integrations/aiohttp.py +++ b/sentry_sdk/integrations/aiohttp.py @@ -6,7 +6,7 @@ from sentry_sdk.consts import OP, SPANSTATUS, SPANDATA from sentry_sdk.integrations import Integration, DidNotEnable from sentry_sdk.integrations.logging import ignore_logger -from sentry_sdk.sessions import auto_session_tracking_scope +from sentry_sdk.sessions import track_session from sentry_sdk.integrations._wsgi_common import ( _filter_headers, request_body_within_bounds, @@ -105,7 +105,7 @@ async def sentry_app_handle(self, request, *args, **kwargs): weak_request = weakref.ref(request) with sentry_sdk.isolation_scope() as scope: - with auto_session_tracking_scope(scope, session_mode="request"): + with track_session(scope, session_mode="request"): # Scope data will not leak between requests because aiohttp # create a task to wrap each request. scope.generate_propagation_context() diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index c0553cb474..b952da021d 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -19,7 +19,7 @@ _get_request_data, _get_url, ) -from sentry_sdk.sessions import auto_session_tracking_scope +from sentry_sdk.sessions import track_session from sentry_sdk.tracing import ( SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE, @@ -169,7 +169,7 @@ async def _run_app(self, scope, receive, send, asgi_version): _asgi_middleware_applied.set(True) try: with sentry_sdk.isolation_scope() as sentry_scope: - with auto_session_tracking_scope(sentry_scope, session_mode="request"): + with track_session(sentry_scope, session_mode="request"): sentry_scope.clear_breadcrumbs() sentry_scope._name = "asgi" processor = partial(self.event_processor, asgi_scope=scope) diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index 1b5c9c7c43..7a95611d78 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -8,9 +8,7 @@ from sentry_sdk.consts import OP from sentry_sdk.scope import should_send_default_pii from sentry_sdk.integrations._wsgi_common import _filter_headers -from sentry_sdk.sessions import ( - auto_session_tracking_scope as auto_session_tracking, -) # When the Hub is removed, this should be renamed (see comment in sentry_sdk/sessions.py) +from sentry_sdk.sessions import track_session from sentry_sdk.scope import use_isolation_scope from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_ROUTE from sentry_sdk.utils import ( @@ -83,7 +81,7 @@ def __call__(self, environ, start_response): _wsgi_middleware_applied.set(True) try: with sentry_sdk.isolation_scope() as scope: - with auto_session_tracking(scope, session_mode="request"): + with track_session(scope, session_mode="request"): with capture_internal_exceptions(): scope.clear_breadcrumbs() scope._name = "wsgi" diff --git a/sentry_sdk/sessions.py b/sentry_sdk/sessions.py index b14bc43187..77e9051550 100644 --- a/sentry_sdk/sessions.py +++ b/sentry_sdk/sessions.py @@ -16,53 +16,14 @@ from typing import Generator from typing import List from typing import Optional - from typing import Union -def is_auto_session_tracking_enabled(hub=None): - # type: (Optional[sentry_sdk.Hub]) -> Union[Any, bool, None] - """Utility function to find out if session tracking is enabled.""" - # TODO: add deprecation warning - - if hub is None: - hub = sentry_sdk.Hub.current - - should_track = hub.scope._force_auto_session_tracking - - if should_track is None: - client_options = hub.client.options if hub.client else {} - should_track = client_options.get("auto_session_tracking", False) - - return should_track - - -@contextmanager -def auto_session_tracking(hub=None, session_mode="application"): - # type: (Optional[sentry_sdk.Hub], str) -> Generator[None, None, None] - """Starts and stops a session automatically around a block.""" - # TODO: add deprecation warning - - if hub is None: - hub = sentry_sdk.Hub.current - should_track = is_auto_session_tracking_enabled(hub) - if should_track: - hub.start_session(session_mode=session_mode) - try: - yield - finally: - if should_track: - hub.end_session() - - -def is_auto_session_tracking_enabled_scope(scope): +def _is_auto_session_tracking_enabled(scope): # type: (sentry_sdk.Scope) -> bool """ Utility function to find out if session tracking is enabled. - - TODO: This uses the new scopes. When the Hub is removed, the function - is_auto_session_tracking_enabled should be removed and this function - should be renamed to is_auto_session_tracking_enabled. """ + should_track = scope._force_auto_session_tracking if should_track is None: client_options = sentry_sdk.get_client().options @@ -72,16 +33,14 @@ def is_auto_session_tracking_enabled_scope(scope): @contextmanager -def auto_session_tracking_scope(scope, session_mode="application"): +def track_session(scope, session_mode="application"): # type: (sentry_sdk.Scope, str) -> Generator[None, None, None] """ - Starts and stops a session automatically around a block. - - TODO: This uses the new scopes. When the Hub is removed, the function - auto_session_tracking should be removed and this function - should be renamed to auto_session_tracking. + Start a new session in the provided scope, assuming session tracking is enabled. + This is a no-op context manager if session tracking is not enabled. """ - should_track = is_auto_session_tracking_enabled_scope(scope) + + should_track = _is_auto_session_tracking_enabled(scope) if should_track: scope.start_session(session_mode=session_mode) try: diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 7a75070274..c6292c818c 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -1,7 +1,7 @@ from unittest import mock import sentry_sdk -from sentry_sdk.sessions import auto_session_tracking +from sentry_sdk.sessions import track_session def sorted_aggregates(item): @@ -50,16 +50,17 @@ def test_aggregates(sentry_init, capture_envelopes): ) envelopes = capture_envelopes() - with auto_session_tracking(session_mode="request"): - with sentry_sdk.new_scope() as scope: + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): try: scope.set_user({"id": "42"}) raise Exception("all is wrong") except Exception: sentry_sdk.capture_exception() - with auto_session_tracking(session_mode="request"): - pass + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): + pass sentry_sdk.get_isolation_scope().start_session(session_mode="request") sentry_sdk.get_isolation_scope().end_session() @@ -90,15 +91,16 @@ def test_aggregates_explicitly_disabled_session_tracking_request_mode( ) envelopes = capture_envelopes() - with auto_session_tracking(session_mode="request"): - with sentry_sdk.new_scope(): + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): try: raise Exception("all is wrong") except Exception: sentry_sdk.capture_exception() - with auto_session_tracking(session_mode="request"): - pass + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): + pass sentry_sdk.get_isolation_scope().start_session(session_mode="request") sentry_sdk.get_isolation_scope().end_session() @@ -125,15 +127,16 @@ def test_no_thread_on_shutdown_no_errors(sentry_init): "threading.Thread.start", side_effect=RuntimeError("can't create new thread at interpreter shutdown"), ): - with auto_session_tracking(session_mode="request"): - with sentry_sdk.new_scope(): + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): try: raise Exception("all is wrong") except Exception: sentry_sdk.capture_exception() - with auto_session_tracking(session_mode="request"): - pass + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): + pass sentry_sdk.get_isolation_scope().start_session(session_mode="request") sentry_sdk.get_isolation_scope().end_session()