Skip to content

Commit 8d475f9

Browse files
authored
feat(sessions): Improved control over sessions (getsentry#646)
1 parent 427ddb0 commit 8d475f9

File tree

5 files changed

+54
-25
lines changed

5 files changed

+54
-25
lines changed

sentry_sdk/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def capture_event(
320320

321321
# whenever we capture an event we also check if the session needs
322322
# to be updated based on that information.
323-
session = scope.session if scope else None
323+
session = scope._session if scope else None
324324
if session:
325325
self._update_session_from_event(session, event)
326326

sentry_sdk/hub.py

+29-9
Original file line numberDiff line numberDiff line change
@@ -567,27 +567,47 @@ def inner():
567567

568568
return inner()
569569

570+
def start_session(self):
571+
# type: (...) -> None
572+
"""Starts a new session."""
573+
self.end_session()
574+
client, scope = self._stack[-1]
575+
scope._session = Session(
576+
release=client.options["release"] if client else None,
577+
environment=client.options["environment"] if client else None,
578+
user=scope._user,
579+
)
580+
570581
def end_session(self):
571582
# type: (...) -> None
572583
"""Ends the current session if there is one."""
573584
client, scope = self._stack[-1]
574-
session = scope.session
585+
session = scope._session
575586
if session is not None:
576587
session.close()
577588
if client is not None:
578589
client.capture_session(session)
579-
self._stack[-1][1].session = None
590+
self._stack[-1][1]._session = None
580591

581-
def start_session(self):
592+
def stop_auto_session_tracking(self):
582593
# type: (...) -> None
583-
"""Starts a new session."""
594+
"""Stops automatic session tracking.
595+
596+
This temporarily session tracking for the current scope when called.
597+
To resume session tracking call `resume_auto_session_tracking`.
598+
"""
584599
self.end_session()
585600
client, scope = self._stack[-1]
586-
scope.session = Session(
587-
release=client.options["release"] if client else None,
588-
environment=client.options["environment"] if client else None,
589-
user=scope._user,
590-
)
601+
scope._force_auto_session_tracking = False
602+
603+
def resume_auto_session_tracking(self):
604+
# type: (...) -> None
605+
"""Resumes automatic session tracking for the current scope if
606+
disabled earlier. This requires that generally automatic session
607+
tracking is enabled.
608+
"""
609+
client, scope = self._stack[-1]
610+
scope._force_auto_session_tracking = None
591611

592612
def flush(
593613
self,

sentry_sdk/integrations/atexit.py

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def _shutdown():
5454
if integration is not None:
5555
logger.debug("atexit: shutting down client")
5656

57+
# If there is a session on the hub, close it now.
58+
hub.end_session()
59+
5760
# If an integration is there, a client has to be there.
5861
client = hub.client # type: Any
5962
client.close(callback=integration.callback)

sentry_sdk/scope.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Scope(object):
8787
"_should_capture",
8888
"_span",
8989
"_session",
90+
"_force_auto_session_tracking",
9091
)
9192

9293
def __init__(self):
@@ -114,6 +115,7 @@ def clear(self):
114115

115116
self._span = None # type: Optional[Span]
116117
self._session = None # type: Optional[Session]
118+
self._force_auto_session_tracking = None # type: Optional[bool]
117119

118120
@_attr_setter
119121
def level(self, value):
@@ -169,17 +171,6 @@ def span(self, span):
169171
if span_transaction:
170172
self._transaction = span_transaction
171173

172-
@property
173-
def session(self):
174-
# type: () -> Optional[Session]
175-
"""Get/set current tracing session."""
176-
return self._session
177-
178-
@session.setter
179-
def session(self, session):
180-
# type: (Optional[Session]) -> None
181-
self._session = session
182-
183174
def set_tag(
184175
self,
185176
key, # type: str
@@ -404,6 +395,7 @@ def __copy__(self):
404395
rv._should_capture = self._should_capture
405396
rv._span = self._span
406397
rv._session = self._session
398+
rv._force_auto_session_tracking = self._force_auto_session_tracking
407399

408400
return rv
409401

sentry_sdk/sessions.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,25 @@
2020
from sentry_sdk._types import SessionStatus
2121

2222

23+
def is_auto_session_tracking_enabled(hub=None):
24+
# type: (Optional[sentry_sdk.Hub]) -> bool
25+
"""Utility function to find out if session tracking is enabled."""
26+
if hub is None:
27+
hub = sentry_sdk.Hub.current
28+
should_track = hub.scope._force_auto_session_tracking
29+
if should_track is None:
30+
exp = hub.client.options["_experiments"] if hub.client else {}
31+
should_track = exp.get("auto_session_tracking")
32+
return should_track
33+
34+
2335
@contextmanager
24-
def auto_session_tracking(hub):
25-
# type: (sentry_sdk.Hub) -> Generator[None, None, None]
26-
exp = hub.client.options["_experiments"] if hub.client else {}
27-
should_track = exp.get("auto_session_tracking")
36+
def auto_session_tracking(hub=None):
37+
# type: (Optional[sentry_sdk.Hub]) -> Generator[None, None, None]
38+
"""Starts and stops a session automatically around a block."""
39+
if hub is None:
40+
hub = sentry_sdk.Hub.current
41+
should_track = is_auto_session_tracking_enabled(hub)
2842
if should_track:
2943
hub.start_session()
3044
try:

0 commit comments

Comments
 (0)