Skip to content

fix(telemetry): use SDK session integration instead of manual management#232

Merged
BYK merged 2 commits intomainfrom
fix/telemetry-session-lifecycle
Feb 11, 2026
Merged

fix(telemetry): use SDK session integration instead of manual management#232
BYK merged 2 commits intomainfrom
fix/telemetry-session-lifecycle

Conversation

@BYK
Copy link
Member

@BYK BYK commented Feb 11, 2026

Summary

  • Move session tracking from manual startSession/endSession/flush in withTelemetry() to the SDK's built-in processSessionIntegration + a beforeExit handler
  • The manual approach missed unhandled rejections and other paths that bypass withTelemetry's try/catch
  • The SDK already starts a session during Sentry.init() and auto-marks crashes for unhandled exceptions via mechanism.handled: false

Changes

src/lib/telemetry.ts

  • Remove Sentry.startSession(), captureSession(), endSession(), and client.flush() from withTelemetry()
  • Add createBeforeExitHandler(client) — ends OK sessions the SDK skips (clean exit → 'exited'), flushes pending events, includes re-entry guard
  • Extract markSessionCrashed() — checks both current scope and isolation scope since processSessionIntegration stores the session on the isolation scope
  • Register the beforeExit handler in initSentry() when telemetry is enabled

test/lib/telemetry-session.test.ts (new)

  • 5 tests for createBeforeExitHandler (OK session, non-OK session, no session, re-entry guard, flush)
  • 4 tests for markSessionCrashed (current scope, isolation scope, no session, scope priority)
  • Separate file because Sentry scope mocking poisons the SDK's global state and breaks other tests in the same worker

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)
@github-actions
Copy link
Contributor

github-actions bot commented Feb 11, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (formatters) Add Seer fixability score to issue list and detail views by betegon in #234

Bug Fixes 🐛

Telemetry

  • Use SDK session integration instead of manual management by BYK in #232
  • Correct runtime context for Bun binary by BYK in #231

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 11, 2026

Codecov Results 📊

✅ Patch coverage is 96.30%. Project has 3861 uncovered lines.
✅ Project coverage is 70.11%. Comparing base (base) to head (head).

Files with missing lines (67)
File Patch % Lines
human.ts 58.29% ⚠️ 395 Missing
resolve-target.ts 20.26% ⚠️ 366 Missing
list.ts 14.39% ⚠️ 345 Missing
api-client.ts 66.37% ⚠️ 230 Missing
list.ts 23.47% ⚠️ 212 Missing
oauth.ts 25.10% ⚠️ 194 Missing
list.ts 21.96% ⚠️ 167 Missing
plan.ts 19.37% ⚠️ 154 Missing
help.ts 19.85% ⚠️ 109 Missing
upgrade.ts 57.71% ⚠️ 107 Missing
view.ts 36.48% ⚠️ 101 Missing
interactive-login.ts 9.17% ⚠️ 99 Missing
view.ts 25.81% ⚠️ 92 Missing
view.ts 39.44% ⚠️ 86 Missing
clipboard.ts 4.49% ⚠️ 85 Missing
status.ts 24.07% ⚠️ 82 Missing
migration.ts 47.44% ⚠️ 82 Missing
list.ts 27.18% ⚠️ 75 Missing
browser.ts 4.11% ⚠️ 70 Missing
login.ts 33.33% ⚠️ 64 Missing
span-tree.ts 5.00% ⚠️ 57 Missing
explain.ts 33.33% ⚠️ 56 Missing
api.ts 89.80% ⚠️ 47 Missing
upgrade.ts 66.91% ⚠️ 46 Missing
telemetry.ts 84.72% ⚠️ 46 Missing
seer.ts 75.54% ⚠️ 45 Missing
schema.ts 89.56% ⚠️ 40 Missing
refresh.ts 40.63% ⚠️ 38 Missing
seer.ts 79.87% ⚠️ 30 Missing
preload.ts 53.23% ⚠️ 29 Missing
view.ts 87.27% ⚠️ 28 Missing
utils.ts 88.94% ⚠️ 25 Missing
view.ts 61.54% ⚠️ 25 Missing
detector.ts 90.10% ⚠️ 20 Missing
binary.ts 88.67% ⚠️ 17 Missing
list.ts 91.16% ⚠️ 16 Missing
code-scanner.ts 95.00% ⚠️ 16 Missing
help.ts 57.14% ⚠️ 15 Missing
arg-parsing.ts 90.00% ⚠️ 12 Missing
dsn-cache.ts 94.62% ⚠️ 12 Missing
logout.ts 56.00% ⚠️ 11 Missing
token.ts 52.17% ⚠️ 11 Missing
fix.ts 83.61% ⚠️ 10 Missing
qrcode.ts 33.33% ⚠️ 10 Missing
fs-utils.ts 57.14% ⚠️ 9 Missing
view.ts 94.70% ⚠️ 7 Missing
project-root.ts 97.73% ⚠️ 7 Missing
version-check.ts 91.76% ⚠️ 7 Missing
feedback.ts 84.21% ⚠️ 6 Missing
auth.ts 95.52% ⚠️ 6 Missing
shell.ts 96.23% ⚠️ 6 Missing
app.ts 93.59% ⚠️ 5 Missing
resolver.ts 94.57% ⚠️ 5 Missing
setup.ts 97.84% ⚠️ 4 Missing
list.ts 97.33% ⚠️ 4 Missing
index.ts 95.96% ⚠️ 4 Missing
project-aliases.ts 97.40% ⚠️ 2 Missing
project-root-cache.ts 96.92% ⚠️ 2 Missing
output.ts 89.47% ⚠️ 2 Missing
alias.ts 99.42% ⚠️ 1 Missing
completions.ts 99.37% ⚠️ 1 Missing
env-file.ts 99.19% ⚠️ 1 Missing
parser.ts 98.63% ⚠️ 1 Missing
colors.ts 98.21% ⚠️ 1 Missing
trace.ts 99.16% ⚠️ 1 Missing
helpers.ts 94.74% ⚠️ 1 Missing
helpers.ts 94.74% ⚠️ 1 Missing
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

@BYK BYK marked this pull request as ready for review February 11, 2026 16:09
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

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.
@BYK BYK merged commit d010122 into main Feb 11, 2026
23 checks passed
@BYK BYK deleted the fix/telemetry-session-lifecycle branch February 11, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant