Skip to content

Commit 0a03da5

Browse files
authored
fix: add Sentry session tracking for crash-free rate metrics (#460)
Sentry crash-free % was always 0% because the CLI never started a session. Without explicit session tracking, Sentry has zero sessions to calculate against. This adds startSession/captureSession on init (when telemetry is enabled) and endSession on clean process exit. Also ensures integration tests always disable telemetry by default to avoid sending Sentry sessions/events from test runs.
1 parent 1bec912 commit 0a03da5

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/instrument.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,16 @@ Sentry.init({
2323
sendDefaultPii: true,
2424
enabled: !isTelemetryDisabled,
2525
});
26+
27+
// Start a Sentry session for this CLI invocation so crash-free rate metrics work.
28+
// Without explicit session tracking, Sentry has zero sessions and crash-free % is always 0%.
29+
if (!isTelemetryDisabled) {
30+
Sentry.startSession();
31+
Sentry.captureSession();
32+
33+
// End the session cleanly when the process exits normally.
34+
// If the process crashes, Sentry automatically marks the session as crashed.
35+
process.on('beforeExit', () => {
36+
Sentry.endSession();
37+
});
38+
}

tests/helpers.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ export async function createMcpStdioClient(
115115
APIFY_TOKEN: process.env.APIFY_TOKEN as string,
116116
};
117117

118+
// Default telemetry to disabled for tests to avoid sending Sentry sessions and events
119+
const telemetryEnabled = telemetry?.enabled ?? false;
120+
118121
// Set environment variables instead of command line arguments when useEnv is true
119122
if (useEnv) {
120123
if (actors !== undefined) {
@@ -126,9 +129,7 @@ export async function createMcpStdioClient(
126129
if (tools !== undefined) {
127130
env.TOOLS = tools.join(',');
128131
}
129-
if (telemetry?.enabled !== undefined) {
130-
env.TELEMETRY_ENABLED = telemetry.enabled.toString();
131-
}
132+
env.TELEMETRY_ENABLED = telemetryEnabled.toString();
132133
if (telemetry?.env !== undefined) {
133134
env.TELEMETRY_ENV = telemetry.env;
134135
}
@@ -149,10 +150,8 @@ export async function createMcpStdioClient(
149150
if (tools !== undefined) {
150151
args.push('--tools', tools.join(','));
151152
}
152-
if (telemetry?.enabled === false) {
153-
args.push('--telemetry-enabled', 'false');
154-
}
155-
if (telemetry?.env !== undefined && telemetry?.enabled !== false) {
153+
args.push('--telemetry-enabled', telemetryEnabled.toString());
154+
if (telemetry?.env !== undefined && telemetryEnabled) {
156155
args.push('--telemetry-env', telemetry.env);
157156
}
158157
if (uiMode !== undefined) {

0 commit comments

Comments
 (0)