Add new session_duration_seconds_total stats#13414
Conversation
✅ PR preview is ready!
|
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
📉 Frontend coverage change detectedThe frontend unit test (vitest) coverage has decreased by 0.0000%
✅ Coverage change is within normal range. |
56eff5e to
988da86
Compare
There was a problem hiding this comment.
Pull request overview
This PR adds a new session_duration_seconds_total metric to track the total time users spend in active sessions. The implementation tracks session connect/reconnect times and accumulates the duration when sessions are disconnected or closed.
Key Changes:
- New
SESSION_DURATION_FAMILYconstant added to track session duration statistics - Session duration tracking implemented in
WebsocketSessionManagerusingtime.monotonic()for accurate elapsed time measurement - Comprehensive test coverage added for various session lifecycle scenarios including reconnections and proper handling of disconnect/close events
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| lib/streamlit/runtime/stats.py | Adds new SESSION_DURATION_FAMILY constant for the duration metric |
| lib/streamlit/runtime/websocket_session_manager.py | Implements session duration tracking by recording connect times and accumulating duration on disconnect/close events |
| lib/tests/streamlit/runtime/websocket_session_manager_test.py | Adds comprehensive test coverage for session duration tracking including reconnection scenarios and edge cases |
SummaryThis PR adds a new metric Changes:
Code QualityThe code quality is excellent and follows Streamlit's established patterns: Strengths:
Implementation Details:
def _accumulate_session_duration(self, session_id: str) -> None:
"""Accumulate the session duration for a closed session.
This method must be called while holding self._stats_lock.
"""
connect_time = self._session_connect_times.pop(session_id, None)
if connect_time is not None:
duration = time.monotonic() - connect_time
self._total_session_duration_seconds += durationThe implementation correctly handles edge cases:
Test CoverageTest coverage is comprehensive and follows the Python unit test guidelines: New Tests Added (5 tests):
Quality of Tests:
Updated Tests:
if label_type is None:
# For GaugeStat or CounterStat without labels
if isinstance(stat, (GaugeStat, CounterStat)) and not stat.labels:
return stat.value
Backwards CompatibilityNo breaking changes. This PR is purely additive:
Security & RiskNo security concerns identified:
Risks:
RecommendationsThe PR is well-implemented with no critical issues. Minor observations:
VerdictAPPROVED: This is a clean, well-tested addition of a new metrics feature. The code follows Streamlit's patterns, has comprehensive test coverage, is backwards compatible, and poses no security risks. This is an automated AI review. Please verify the feedback and use your judgment. |
|
@cursor review |
Co-authored-by: Copilot <[email protected]>
5553e16 to
effcaff
Compare
While this isn't exactly the best way of doing this, it'd be useful for us to have a new metric to track the total length of time users spend in active sessions (see streamlit#13324 (comment)). This PR adds this new metric by keeping track of session connect/reconnect times and incrementing a simple accumulator for aggregate session length each time a session is either disconnected or closed. Note that one caveat of doing this is that long-lived active sessions won't be counted until disconnected or closed, but we're not too worried our data being slow to update due to this.
Describe your changes
While this isn't exactly the best way of doing this, it'd be useful for us to have a new metric to track
the total length of time users spend in active sessions (see #13324 (comment)).
This PR adds this new metric by keeping track of session connect/reconnect times and incrementing
a simple accumulator for aggregate session length each time a session is either disconnected or
closed.
Note that one caveat of doing this is that long-lived active sessions won't be counted until disconnected
or closed, but we're not too worried our data being slow to update due to this.
Testing Plan
localhost:8501/_stcore/metricsafter opening/closing browser tabs