Skip to content

Commit 74ac9c3

Browse files
author
Luca Forstner
committed
Hmm
1 parent ac0d55a commit 74ac9c3

File tree

3 files changed

+30
-58
lines changed

3 files changed

+30
-58
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export class ServerRuntimeClient<
7676
* @inheritDoc
7777
*/
7878
public captureException(exception: unknown, hint?: EventHint, scope?: Scope): string {
79+
// TODO: Check if mechanism === unhandled
80+
setCurrentRequestSessionCrashed();
7981
return super.captureException(exception, hint, scope);
8082
}
8183

@@ -87,15 +89,7 @@ export class ServerRuntimeClient<
8789
const isException = !event.type && event.exception && event.exception.values && event.exception.values.length > 0;
8890
if (isException) {
8991
// TODO: Check if mechanism === unhandled
90-
const isolationScope = getIsolationScope();
91-
const requestSession = isolationScope.getScopeData().sdkProcessingMetadata.requestSession;
92-
if (requestSession) {
93-
isolationScope.setSDKProcessingMetadata({
94-
requestSession: {
95-
status: 'errored',
96-
},
97-
});
98-
}
92+
setCurrentRequestSessionCrashed();
9993
}
10094

10195
return super.captureEvent(event, hint, scope);
@@ -209,3 +203,18 @@ export class ServerRuntimeClient<
209203
return [dynamicSamplingContext, traceContext];
210204
}
211205
}
206+
207+
function setCurrentRequestSessionCrashed(): void {
208+
const requestSession = getIsolationScope().getScopeData().sdkProcessingMetadata.requestSession as
209+
| { status: 'ok' | 'crashed' }
210+
| undefined;
211+
212+
if (requestSession) {
213+
// We mutate instead of doing `setSdkProcessingMetadata` because the http integration stores away a particular
214+
// isolationScope. If that isolation scope is forked, setting the processing metadata here will not mutate the
215+
// original isolation scope that the http integration stored away.
216+
requestSession.status = 'crashed';
217+
218+
// TODO maybe send 'errored' when handled exception?
219+
}
220+
}

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { AggregationCounts, Client, RequestEventData, SanitizedRequestData,
99
import {
1010
addBreadcrumb,
1111
getBreadcrumbLogLevelFromHttpStatusCode,
12+
getClient,
1213
getIsolationScope,
1314
getSanitizedUrlString,
1415
httpRequestToRequestData,
@@ -23,11 +24,11 @@ import { getRequestInfo } from './vendor/getRequestInfo';
2324

2425
const clientToAggregatesMap = new Map<
2526
Client,
26-
{ [timestampRoundedToSeconds: string]: { exited: number; errored: number } }
27+
{ [timestampRoundedToSeconds: string]: { exited: number; crashed: number } }
2728
>();
2829

2930
interface RequestSession {
30-
status: 'ok' | 'errored';
31+
status: 'ok' | 'crashed';
3132
}
3233

3334
type Http = typeof http;
@@ -178,7 +179,8 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
178179
requestSession: { status: 'ok' },
179180
});
180181
response.once('close', () => {
181-
const client = isolationScope.getClient();
182+
// We need to grab the client off the current scope instead of the isolation scope because the isolation scope doesn't hold any client out of the box.
183+
const client = getClient();
182184
const requestSession = isolationScope.getScopeData().sdkProcessingMetadata.requestSession as
183185
| RequestSession
184186
| undefined;
@@ -191,13 +193,13 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
191193
const existingClientAggregate = clientToAggregatesMap.get(client);
192194
if (existingClientAggregate) {
193195
DEBUG_BUILD && logger.debug(`Recorded request session with status: ${requestSession.status}`);
194-
const bucket = existingClientAggregate[dateBucketKey] || { errored: 0, exited: 0 };
195-
bucket[requestSession.status === 'ok' ? 'exited' : 'errored']++;
196+
const bucket = existingClientAggregate[dateBucketKey] || { crashed: 0, exited: 0 };
197+
bucket[requestSession.status === 'ok' ? 'exited' : 'crashed']++;
196198
existingClientAggregate[dateBucketKey] = bucket;
197199
} else {
198200
DEBUG_BUILD && logger.debug('Opened new request session aggregate.');
199-
const bucket = { errored: 0, exited: 0 };
200-
bucket[requestSession.status === 'ok' ? 'exited' : 'errored']++;
201+
const bucket = { crashed: 0, exited: 0 };
202+
bucket[requestSession.status === 'ok' ? 'exited' : 'crashed']++;
201203
const newClientAggregate = { [dateBucketKey]: bucket };
202204
clientToAggregatesMap.set(client, newClientAggregate);
203205

@@ -210,7 +212,7 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
210212
([timestamp, value]) => ({
211213
started: timestamp,
212214
exited: value.exited,
213-
errored: value.errored,
215+
crashed: value.crashed,
214216
}),
215217
);
216218
client.sendSession({ aggregates: aggregatePayload });
@@ -223,7 +225,8 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
223225
const timeout = setTimeout(() => {
224226
DEBUG_BUILD && logger.debug('Sending request session aggregate due to flushing schedule');
225227
flushPendingClientAggregates();
226-
}, 60_000).unref();
228+
// TODO: Increase to 60s
229+
}, 5_000).unref();
227230
}
228231
}
229232
});

packages/node/src/sdk/index.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,16 @@ import type { Integration, Options } from '@sentry/core';
22
import {
33
consoleSandbox,
44
dropUndefinedKeys,
5-
endSession,
65
functionToStringIntegration,
7-
getClient,
86
getCurrentScope,
97
getIntegrationsToSetup,
10-
getIsolationScope,
118
hasTracingEnabled,
129
inboundFiltersIntegration,
1310
linkedErrorsIntegration,
1411
logger,
1512
propagationContextFromHeaders,
1613
requestDataIntegration,
1714
stackParserFromStackParserOptions,
18-
startSession,
1915
} from '@sentry/core';
2016
import {
2117
enhanceDscWithOpenTelemetryRootSpanName,
@@ -155,13 +151,6 @@ function _init(
155151
client.init();
156152

157153
logger.log(`Running in ${isCjs() ? 'CommonJS' : 'ESM'} mode.`);
158-
159-
// TODO(V9): Remove this code since all of the logic should be in an integration
160-
// eslint-disable-next-line deprecation/deprecation
161-
if (options.autoSessionTracking) {
162-
startSessionTracking();
163-
}
164-
165154
client.startClientReportTracking();
166155

167156
updateScopeFromEnvVariables();
@@ -312,32 +301,3 @@ function updateScopeFromEnvVariables(): void {
312301
getCurrentScope().setPropagationContext(propagationContext);
313302
}
314303
}
315-
316-
/**
317-
* Enable automatic Session Tracking for the node process.
318-
*/
319-
function startSessionTracking(): void {
320-
const client = getClient<NodeClient>();
321-
// eslint-disable-next-line deprecation/deprecation
322-
if (client && client.getOptions().autoSessionTracking) {
323-
client.initSessionFlusher();
324-
}
325-
326-
startSession();
327-
328-
// Emitted in the case of healthy sessions, error of `mechanism.handled: true` and unhandledrejections because
329-
// The 'beforeExit' event is not emitted for conditions causing explicit termination,
330-
// such as calling process.exit() or uncaught exceptions.
331-
// Ref: https://nodejs.org/api/process.html#process_event_beforeexit
332-
process.on('beforeExit', () => {
333-
const session = getIsolationScope().getSession();
334-
335-
// Only call endSession, if the Session exists on Scope and SessionStatus is not a
336-
// Terminal Status i.e. Exited or Crashed because
337-
// "When a session is moved away from ok it must not be updated anymore."
338-
// Ref: https://develop.sentry.dev/sdk/sessions/
339-
if (session && session.status !== 'ok') {
340-
endSession();
341-
}
342-
});
343-
}

0 commit comments

Comments
 (0)