1
1
import os
2
2
import time
3
+ import warnings
3
4
from threading import Thread , Lock
4
5
from contextlib import contextmanager
5
6
19
20
from typing import Union
20
21
21
22
22
- def is_auto_session_tracking_enabled (hub = None ):
23
- # type: (Optional[sentry_sdk.Hub]) -> Union[Any, bool, None]
24
- """Utility function to find out if session tracking is enabled."""
25
- # TODO: add deprecation warning
26
-
27
- if hub is None :
28
- hub = sentry_sdk .Hub .current
29
-
30
- should_track = hub .scope ._force_auto_session_tracking
23
+ def _is_auto_session_tracking_enabled (scope ):
24
+ # type: (sentry_sdk.Scope) -> bool
25
+ """
26
+ Utility function to find out if session tracking is enabled.
27
+ """
31
28
29
+ should_track = scope ._force_auto_session_tracking
32
30
if should_track is None :
33
- client_options = hub . client .options if hub . client else {}
31
+ client_options = sentry_sdk . get_client () .options
34
32
should_track = client_options .get ("auto_session_tracking" , False )
35
33
36
34
return should_track
37
35
38
36
37
+ @contextmanager
38
+ def track_session (scope , session_mode = "application" ):
39
+ # type: (sentry_sdk.Scope, str) -> None
40
+ """
41
+ Start a new session in the provided scope, assuming session tracking is enabled.
42
+ This is a no-op context manager if session tracking is not enabled.
43
+ """
44
+
45
+ if _is_auto_session_tracking_enabled (scope ):
46
+ scope .start_session (session_mode = session_mode )
47
+ try :
48
+ yield
49
+ finally :
50
+ scope .end_session ()
51
+ else :
52
+ yield
53
+
54
+
39
55
@contextmanager
40
56
def auto_session_tracking (hub = None , session_mode = "application" ):
41
57
# type: (Optional[sentry_sdk.Hub], str) -> Generator[None, None, None]
42
- """Starts and stops a session automatically around a block."""
43
- # TODO: add deprecation warning
58
+ """DEPRECATED: Use track_session instead
59
+ Starts and stops a session automatically around a block.
60
+ """
61
+ warnings .warn (
62
+ "This function is deprecated and will be removed in the next major release. "
63
+ "Use track_session instead." ,
64
+ DeprecationWarning ,
65
+ stacklevel = 2 ,
66
+ )
44
67
45
68
if hub is None :
46
69
hub = sentry_sdk .Hub .current
47
- should_track = is_auto_session_tracking_enabled (hub )
70
+ with warnings .catch_warnings ():
71
+ warnings .simplefilter ("ignore" , DeprecationWarning )
72
+ should_track = is_auto_session_tracking_enabled (hub )
48
73
if should_track :
49
74
hub .start_session (session_mode = session_mode )
50
75
try :
@@ -54,41 +79,62 @@ def auto_session_tracking(hub=None, session_mode="application"):
54
79
hub .end_session ()
55
80
56
81
57
- def is_auto_session_tracking_enabled_scope (scope ):
58
- # type: (sentry_sdk.Scope) -> bool
59
- """
60
- Utility function to find out if session tracking is enabled.
82
+ def is_auto_session_tracking_enabled (hub = None ):
83
+ # type: (Optional[sentry_sdk.Hub]) -> Union[Any, bool, None]
84
+ """DEPRECATED: Utility function to find out if session tracking is enabled."""
85
+
86
+ # Internal callers should use private _is_auto_session_tracking_enabled, instead.
87
+ warnings .warn (
88
+ "This function is deprecated and will be removed in the next major release. "
89
+ "There is no public API replacement." ,
90
+ DeprecationWarning ,
91
+ stacklevel = 2 ,
92
+ )
93
+
94
+ if hub is None :
95
+ hub = sentry_sdk .Hub .current
96
+
97
+ should_track = hub .scope ._force_auto_session_tracking
61
98
62
- TODO: This uses the new scopes. When the Hub is removed, the function
63
- is_auto_session_tracking_enabled should be removed and this function
64
- should be renamed to is_auto_session_tracking_enabled.
65
- """
66
- should_track = scope ._force_auto_session_tracking
67
99
if should_track is None :
68
- client_options = sentry_sdk . get_client () .options
100
+ client_options = hub . client .options if hub . client else {}
69
101
should_track = client_options .get ("auto_session_tracking" , False )
70
102
71
103
return should_track
72
104
73
105
106
+ def is_auto_session_tracking_enabled_scope (scope ):
107
+ # type: (sentry_sdk.Scope) -> bool
108
+ """
109
+ DEPRECATED: Utility function to find out if session tracking is enabled.
110
+ """
111
+
112
+ warnings .warn (
113
+ "This function is deprecated and will be removed in the next major release. "
114
+ "There is no public API replacement." ,
115
+ DeprecationWarning ,
116
+ stacklevel = 2 ,
117
+ )
118
+
119
+ # Internal callers should use private _is_auto_session_tracking_enabled, instead.
120
+ return _is_auto_session_tracking_enabled (scope )
121
+
122
+
74
123
@contextmanager
75
124
def auto_session_tracking_scope (scope , session_mode = "application" ):
76
125
# type: (sentry_sdk.Scope, str) -> Generator[None, None, None]
77
- """
126
+ """DEPRECATED: This function is a deprecated alias for track_session.
78
127
Starts and stops a session automatically around a block.
79
-
80
- TODO: This uses the new scopes. When the Hub is removed, the function
81
- auto_session_tracking should be removed and this function
82
- should be renamed to auto_session_tracking.
83
128
"""
84
- should_track = is_auto_session_tracking_enabled_scope (scope )
85
- if should_track :
86
- scope .start_session (session_mode = session_mode )
87
- try :
129
+
130
+ warnings .warn (
131
+ "This function is a deprecated alias for track_session and will be removed in the next major release." ,
132
+ DeprecationWarning ,
133
+ stacklevel = 2 ,
134
+ )
135
+
136
+ with track_session (scope , session_mode = session_mode ):
88
137
yield
89
- finally :
90
- if should_track :
91
- scope .end_session ()
92
138
93
139
94
140
TERMINAL_SESSION_STATES = ("exited" , "abnormal" , "crashed" )
0 commit comments