Skip to content

Commit 7bc76ed

Browse files
cursoragentszokeasaurusrex
authored andcommitted
feat(sessions): Add top-level start- and end session methods
Closes #4474
1 parent 4a0e5ed commit 7bc76ed

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

sentry_sdk/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
"trace",
4848
"monitor",
4949
"logger",
50+
"start_session",
51+
"end_session",
5052
]
5153

5254
# Initialize the debug support after everything is loaded

sentry_sdk/api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ def overload(x):
8282
"start_transaction",
8383
"trace",
8484
"monitor",
85+
"start_session",
86+
"end_session",
8587
]
8688

8789

@@ -450,3 +452,17 @@ def continue_trace(
450452
return get_isolation_scope().continue_trace(
451453
environ_or_headers, op, name, source, origin
452454
)
455+
456+
457+
@scopemethod
458+
def start_session(
459+
session_mode="application", # type: str
460+
):
461+
# type: (...) -> None
462+
return get_isolation_scope().start_session(session_mode=session_mode)
463+
464+
465+
@scopemethod
466+
def end_session():
467+
# type: () -> None
468+
return get_isolation_scope().end_session()

tests/test_api_sessions.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import sentry_sdk
2+
3+
4+
def test_start_session_basic(sentry_init, capture_envelopes):
5+
"""Test that start_session starts a session on the isolation scope."""
6+
sentry_init(release="test-release", environment="test-env")
7+
envelopes = capture_envelopes()
8+
9+
# Start a session using the top-level API
10+
sentry_sdk.start_session()
11+
12+
# End the session
13+
sentry_sdk.end_session()
14+
sentry_sdk.flush()
15+
16+
# Check that we got a session envelope
17+
assert len(envelopes) == 1
18+
sess = envelopes[0]
19+
assert len(sess.items) == 1
20+
sess_event = sess.items[0].payload.json
21+
22+
assert sess_event["attrs"] == {
23+
"release": "test-release",
24+
"environment": "test-env",
25+
}
26+
assert sess_event["status"] == "exited"
27+
28+
29+
def test_start_session_with_mode(sentry_init, capture_envelopes):
30+
"""Test that start_session accepts session_mode parameter."""
31+
sentry_init(release="test-release", environment="test-env")
32+
envelopes = capture_envelopes()
33+
34+
# Start a session with request mode
35+
sentry_sdk.start_session(session_mode="request")
36+
sentry_sdk.end_session()
37+
sentry_sdk.flush()
38+
39+
# Request mode sessions are aggregated
40+
assert len(envelopes) == 1
41+
sess = envelopes[0]
42+
assert len(sess.items) == 1
43+
sess_event = sess.items[0].payload.json
44+
45+
assert sess_event["attrs"] == {
46+
"release": "test-release",
47+
"environment": "test-env",
48+
}
49+
# Request sessions show up as aggregates
50+
assert "aggregates" in sess_event

0 commit comments

Comments
 (0)