fix(telemetry): use SDK session integration instead of manual management#232
Merged
fix(telemetry): use SDK session integration instead of manual management#232
Conversation
Move session tracking from manual start/end in withTelemetry() to the SDK's built-in processSessionIntegration plus a beforeExit handler. The SDK starts a session during Sentry.init() and marks it as crashed for unhandled exceptions automatically. The manual approach missed unhandled rejections and other paths that bypass withTelemetry's try/catch. Changes: - Remove startSession/captureSession/endSession/flush from withTelemetry - Add createBeforeExitHandler() to end healthy sessions and flush events when the event loop drains (complements SDK's non-OK session handler) - Extract markSessionCrashed() to check both current and isolation scope - Register beforeExit handler in initSentry() when telemetry is enabled - Add telemetry-session.test.ts for session tests (separate file to avoid Sentry scope poisoning in main test suite)
Contributor
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Bug Fixes 🐛Telemetry
🤖 This preview updates automatically when you update the PR. |
Contributor
Codecov Results 📊✅ Patch coverage is 96.30%. Project has 3861 uncovered lines. Files with missing lines (67)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 69.87% 70.11% +0.24%
==========================================
Files 105 105 —
Lines 12848 12918 +70
Branches 0 0 —
==========================================
+ Hits 8977 9057 +80
- Misses 3871 3861 -10
- Partials 0 0 —Generated by Codecov Action |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
…ates In the auto-login retry flow, withTelemetry → initSentry is called twice. Each call was registering a new beforeExit handler with its own isFlushing closure and client reference. Track the current handler at module scope and removeListener before re-registering so only the latest handler (with the current client) is active.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
startSession/endSession/flushinwithTelemetry()to the SDK's built-inprocessSessionIntegration+ abeforeExithandlerwithTelemetry's try/catchSentry.init()and auto-marks crashes for unhandled exceptions viamechanism.handled: falseChanges
src/lib/telemetry.tsSentry.startSession(),captureSession(),endSession(), andclient.flush()fromwithTelemetry()createBeforeExitHandler(client)— ends OK sessions the SDK skips (clean exit →'exited'), flushes pending events, includes re-entry guardmarkSessionCrashed()— checks both current scope and isolation scope sinceprocessSessionIntegrationstores the session on the isolation scopebeforeExithandler ininitSentry()when telemetry is enabledtest/lib/telemetry-session.test.ts(new)createBeforeExitHandler(OK session, non-OK session, no session, re-entry guard, flush)markSessionCrashed(current scope, isolation scope, no session, scope priority)