Skip to content

Commit a2ca9b0

Browse files
author
Luca Forstner
committed
crashed and errored
1 parent 5c7d64e commit a2ca9b0

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

packages/core/src/server-runtime-client.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ export class ServerRuntimeClient<
7979
* @inheritDoc
8080
*/
8181
public captureException(exception: unknown, hint?: EventHint, scope?: Scope): string {
82-
// TODO: Check if mechanism === unhandled
83-
setCurrentRequestSessionCrashed();
82+
setCurrentRequestSessionErroredOrCrashed(hint);
8483
return super.captureException(exception, hint, scope);
8584
}
8685

@@ -91,8 +90,7 @@ export class ServerRuntimeClient<
9190
// If the event is of type Exception, then a request session should be captured
9291
const isException = !event.type && event.exception && event.exception.values && event.exception.values.length > 0;
9392
if (isException) {
94-
// TODO: Check if mechanism === unhandled
95-
setCurrentRequestSessionCrashed();
93+
setCurrentRequestSessionErroredOrCrashed(hint);
9694
}
9795

9896
return super.captureEvent(event, hint, scope);
@@ -207,17 +205,25 @@ export class ServerRuntimeClient<
207205
}
208206
}
209207

210-
function setCurrentRequestSessionCrashed(): void {
208+
function setCurrentRequestSessionErroredOrCrashed(eventHint?: EventHint): void {
209+
const isHandledException = eventHint?.mechanism?.handled ?? true;
210+
211211
const requestSession = getIsolationScope().getScopeData().sdkProcessingMetadata.requestSession as
212-
| { status: 'ok' | 'crashed' }
212+
| { status: 'ok' | 'errored' | 'crashed' }
213213
| undefined;
214214

215215
if (requestSession) {
216216
// We mutate instead of doing `setSdkProcessingMetadata` because the http integration stores away a particular
217217
// isolationScope. If that isolation scope is forked, setting the processing metadata here will not mutate the
218218
// original isolation scope that the http integration stored away.
219-
requestSession.status = 'crashed';
220-
221-
// TODO maybe send 'errored' when handled exception?
219+
if (isHandledException) {
220+
// A request session can go from "errored" -> "crashed" but not "crashed" -> "errored".
221+
// Crashed (unhandled exception) is worse than errored (handled exception).
222+
if (requestSession.status !== 'crashed') {
223+
requestSession.status = 'errored';
224+
}
225+
} else {
226+
requestSession.status = 'crashed';
227+
}
222228
}
223229
}

packages/node/src/integrations/http/SentryHttpInstrumentation.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import { getRequestInfo } from './vendor/getRequestInfo';
2424

2525
const clientToAggregatesMap = new Map<
2626
Client,
27-
{ [timestampRoundedToSeconds: string]: { exited: number; crashed: number } }
27+
{ [timestampRoundedToSeconds: string]: { exited: number; crashed: number; errored: number } }
2828
>();
2929

3030
interface RequestSession {
31-
status: 'ok' | 'crashed';
31+
status: 'ok' | 'errored' | 'crashed';
3232
}
3333

3434
type Http = typeof http;
@@ -186,20 +186,20 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
186186
| undefined;
187187

188188
if (client && requestSession) {
189+
DEBUG_BUILD && logger.debug(`Recorded request session with status: ${requestSession.status}`);
190+
189191
const roundedDate = new Date();
190192
roundedDate.setSeconds(0, 0);
191193
const dateBucketKey = roundedDate.toISOString();
192194

193195
const existingClientAggregate = clientToAggregatesMap.get(client);
196+
const bucket = existingClientAggregate?.[dateBucketKey] || { exited: 0, crashed: 0, errored: 0 };
197+
bucket[({ ok: 'exited', crashed: 'crashed', errored: 'errored' } as const)[requestSession.status]]++;
198+
194199
if (existingClientAggregate) {
195-
DEBUG_BUILD && logger.debug(`Recorded request session with status: ${requestSession.status}`);
196-
const bucket = existingClientAggregate[dateBucketKey] || { crashed: 0, exited: 0 };
197-
bucket[requestSession.status === 'ok' ? 'exited' : 'crashed']++;
198200
existingClientAggregate[dateBucketKey] = bucket;
199201
} else {
200202
DEBUG_BUILD && logger.debug('Opened new request session aggregate.');
201-
const bucket = { crashed: 0, exited: 0 };
202-
bucket[requestSession.status === 'ok' ? 'exited' : 'crashed']++;
203203
const newClientAggregate = { [dateBucketKey]: bucket };
204204
clientToAggregatesMap.set(client, newClientAggregate);
205205

@@ -225,8 +225,7 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
225225
const timeout = setTimeout(() => {
226226
DEBUG_BUILD && logger.debug('Sending request session aggregate due to flushing schedule');
227227
flushPendingClientAggregates();
228-
// TODO: Increase to 60s
229-
}, 5_000).unref();
228+
}, 60_000).unref();
230229
}
231230
}
232231
});

0 commit comments

Comments
 (0)