Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ def _capture_envelope(envelope):
try:
_client_init_debug.set(self.options["debug"])
self.transport = make_transport(self.options)
session_mode = self.options["_experiments"].get(
"session_mode", "application"
)
session_mode = self.options.get("session_mode", "application")
self.session_flusher = SessionFlusher(
capture_func=_capture_envelope, session_mode=session_mode
)
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def __init__(
traces_sample_rate=None, # type: Optional[float]
traces_sampler=None, # type: Optional[TracesSampler]
auto_enabling_integrations=True, # type: bool
auto_session_tracking=True, # type: bool
session_mode="application", # type: str
_experiments={}, # type: Experiments # noqa: B006
):
# type: (...) -> None
Expand Down
7 changes: 4 additions & 3 deletions sentry_sdk/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@
from typing import Dict
from typing import List
from typing import Generator
from typing import Union


def is_auto_session_tracking_enabled(hub=None):
# type: (Optional[sentry_sdk.Hub]) -> bool
# type: (Optional[sentry_sdk.Hub]) -> Union[bool, None]
"""Utility function to find out if session tracking is enabled."""
if hub is None:
hub = sentry_sdk.Hub.current
should_track = hub.scope._force_auto_session_tracking
if should_track is None:
exp = hub.client.options["_experiments"] if hub.client else {}
should_track = exp.get("auto_session_tracking")
client_options = hub.client.options if hub.client else {}
should_track = client_options.get("auto_session_tracking", True)
return should_track


Expand Down
40 changes: 39 additions & 1 deletion tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_aggregates(sentry_init, capture_envelopes):
sentry_init(
release="fun-release",
environment="not-fun-env",
_experiments={"auto_session_tracking": True, "session_mode": "request"},
session_mode="request",
)
envelopes = capture_envelopes()

Expand Down Expand Up @@ -85,3 +85,41 @@ def test_aggregates(sentry_init, capture_envelopes):
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
):
sentry_init(
release="fun-release",
environment="not-fun-env",
auto_session_tracking=False,
session_mode="request",
)
envelopes = capture_envelopes()

hub = Hub.current

with auto_session_tracking():
with sentry_sdk.push_scope():
try:
raise Exception("all is wrong")
except Exception:
sentry_sdk.capture_exception()

with auto_session_tracking():
pass

hub.start_session()
hub.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]