Skip to content

feat(sessions): Add top-level start- and end session methods #4474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions sentry_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
"trace",
"monitor",
"logger",
"start_session",
"end_session",
]

# Initialize the debug support after everything is loaded
Expand Down
16 changes: 16 additions & 0 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def overload(x):
"start_transaction",
"trace",
"monitor",
"start_session",
"end_session",
]


Expand Down Expand Up @@ -450,3 +452,17 @@ def continue_trace(
return get_isolation_scope().continue_trace(
environ_or_headers, op, name, source, origin
)


@scopemethod
def start_session(
session_mode="application", # type: str
):
# type: (...) -> None
return get_isolation_scope().start_session(session_mode=session_mode)


@scopemethod
def end_session():
# type: () -> None
return get_isolation_scope().end_session()
50 changes: 50 additions & 0 deletions tests/test_api_sessions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import sentry_sdk
Copy link
Contributor

@sentrivana sentrivana Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move these tests to the existing tests/test_sessions.py and delete this file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure



def test_start_session_basic(sentry_init, capture_envelopes):
"""Test that start_session starts a session on the isolation scope."""
sentry_init(release="test-release", environment="test-env")
envelopes = capture_envelopes()

# Start a session using the top-level API
sentry_sdk.start_session()

# End the session
sentry_sdk.end_session()
sentry_sdk.flush()

# Check that we got a session envelope
assert len(envelopes) == 1
sess = envelopes[0]
assert len(sess.items) == 1
sess_event = sess.items[0].payload.json

assert sess_event["attrs"] == {
"release": "test-release",
"environment": "test-env",
}
assert sess_event["status"] == "exited"


def test_start_session_with_mode(sentry_init, capture_envelopes):
"""Test that start_session accepts session_mode parameter."""
sentry_init(release="test-release", environment="test-env")
envelopes = capture_envelopes()

# Start a session with request mode
sentry_sdk.start_session(session_mode="request")
sentry_sdk.end_session()
sentry_sdk.flush()

# Request mode sessions are aggregated
assert len(envelopes) == 1
sess = envelopes[0]
assert len(sess.items) == 1
sess_event = sess.items[0].payload.json

assert sess_event["attrs"] == {
"release": "test-release",
"environment": "test-env",
}
# Request sessions show up as aggregates
assert "aggregates" in sess_event
Loading