Skip to content

Commit

Permalink
feat(sessions): Remove deprecated functions
Browse files Browse the repository at this point in the history
Remove the deprecated functions from `sessions.py` (this includes all Hub-based logic) and all tests for these functions.

BREAKING CHANGE: Remove `sentry_sdk.sessions.auto_session_tracking`.

BREAKING CHANGE: Remove `sentry_sdk.sessions.is_auto_session_tracking_enabled`.

BREAKING CHANGE: Remove `sentry_sdk.sessions.is_auto_session_tracking_enabled_scope`.

BREAKING CHANGE: Remove `sentry_sdk.sessions.is_auto_session_tracking_enabled_scope`.

Closes: #3416
  • Loading branch information
szokeasaurusrex committed Aug 9, 2024
1 parent c76e1d7 commit ef1bc38
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 189 deletions.
87 changes: 0 additions & 87 deletions sentry_sdk/sessions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import time
import warnings
from threading import Thread, Lock
from contextlib import contextmanager

Expand All @@ -17,75 +16,6 @@
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]
"""DEPRECATED: Utility function to find out if session tracking is enabled."""

# Internal callers should use private _is_auto_session_tracking_enabled, instead.
warnings.warn(
"This function is deprecated and will be removed in the next major release. "
"There is no public API replacement.",
DeprecationWarning,
stacklevel=2,
)

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]
"""DEPRECATED: Use track_session instead
Starts and stops a session automatically around a block.
"""
warnings.warn(
"This function is deprecated and will be removed in the next major release. "
"Use track_session instead.",
DeprecationWarning,
stacklevel=2,
)

if hub is None:
hub = sentry_sdk.Hub.current
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
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):
# type: (sentry_sdk.Scope) -> bool
"""
DEPRECATED: Utility function to find out if session tracking is enabled.
"""

warnings.warn(
"This function is deprecated and will be removed in the next major release. "
"There is no public API replacement.",
DeprecationWarning,
stacklevel=2,
)

# Internal callers should use private _is_auto_session_tracking_enabled, instead.
return _is_auto_session_tracking_enabled(scope)


def _is_auto_session_tracking_enabled(scope):
Expand All @@ -102,23 +32,6 @@ def _is_auto_session_tracking_enabled(scope):
return should_track


@contextmanager
def auto_session_tracking_scope(scope, session_mode="application"):
# type: (sentry_sdk.Scope, str) -> Generator[None, None, None]
"""DEPRECATED: This function is a deprecated alias for track_session.
Starts and stops a session automatically around a block.
"""

warnings.warn(
"This function is a deprecated alias for track_session and will be removed in the next major release.",
DeprecationWarning,
stacklevel=2,
)

with track_session(scope, session_mode=session_mode):
yield


@contextmanager
def track_session(scope, session_mode="application"):
# type: (sentry_sdk.Scope, str) -> Generator[None, None, None]
Expand Down
103 changes: 1 addition & 102 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import mock

import sentry_sdk
from sentry_sdk.sessions import auto_session_tracking, track_session
from sentry_sdk.sessions import track_session


def sorted_aggregates(item):
Expand Down Expand Up @@ -83,47 +83,6 @@ def test_aggregates(sentry_init, capture_envelopes):
assert aggregates[0]["errored"] == 1


def test_aggregates_deprecated(
sentry_init, capture_envelopes, suppress_deprecation_warnings
):
sentry_init(
release="fun-release",
environment="not-fun-env",
)
envelopes = capture_envelopes()

with auto_session_tracking(session_mode="request"):
with sentry_sdk.new_scope() as scope:
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

sentry_sdk.get_isolation_scope().start_session(session_mode="request")
sentry_sdk.get_isolation_scope().end_session()
sentry_sdk.flush()

assert len(envelopes) == 2
assert envelopes[0].get_event() is not None

sess = envelopes[1]
assert len(sess.items) == 1
sess_event = sess.items[0].payload.json
assert sess_event["attrs"] == {
"release": "fun-release",
"environment": "not-fun-env",
}

aggregates = sorted_aggregates(sess_event)
assert len(aggregates) == 1
assert aggregates[0]["exited"] == 2
assert aggregates[0]["errored"] == 1


def test_aggregates_explicitly_disabled_session_tracking_request_mode(
sentry_init, capture_envelopes
):
Expand Down Expand Up @@ -157,38 +116,6 @@ def test_aggregates_explicitly_disabled_session_tracking_request_mode(
assert "errored" not in aggregates[0]


def test_aggregates_explicitly_disabled_session_tracking_request_mode_deprecated(
sentry_init, capture_envelopes, suppress_deprecation_warnings
):
sentry_init(
release="fun-release", environment="not-fun-env", auto_session_tracking=False
)
envelopes = capture_envelopes()

with auto_session_tracking(session_mode="request"):
with sentry_sdk.new_scope():
try:
raise Exception("all is wrong")
except Exception:
sentry_sdk.capture_exception()

with auto_session_tracking(session_mode="request"):
pass

sentry_sdk.get_isolation_scope().start_session(session_mode="request")
sentry_sdk.get_isolation_scope().end_session()
sentry_sdk.flush()

sess = envelopes[1]
assert len(sess.items) == 1
sess_event = sess.items[0].payload.json

aggregates = sorted_aggregates(sess_event)
assert len(aggregates) == 1
assert aggregates[0]["exited"] == 1
assert "errored" not in aggregates[0]


def test_no_thread_on_shutdown_no_errors(sentry_init):
sentry_init(
release="fun-release",
Expand All @@ -214,31 +141,3 @@ def test_no_thread_on_shutdown_no_errors(sentry_init):
sentry_sdk.get_isolation_scope().start_session(session_mode="request")
sentry_sdk.get_isolation_scope().end_session()
sentry_sdk.flush()


def test_no_thread_on_shutdown_no_errors_deprecated(
sentry_init, suppress_deprecation_warnings
):
sentry_init(
release="fun-release",
environment="not-fun-env",
)

# make it seem like the interpreter is shutting down
with mock.patch(
"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():
try:
raise Exception("all is wrong")
except Exception:
sentry_sdk.capture_exception()

with auto_session_tracking(session_mode="request"):
pass

sentry_sdk.get_isolation_scope().start_session(session_mode="request")
sentry_sdk.get_isolation_scope().end_session()
sentry_sdk.flush()

0 comments on commit ef1bc38

Please sign in to comment.