Skip to content

Commit 8433d45

Browse files
authored
feat(node): Add processSessionIntegration (getsentry#15081)
1 parent 7404fdc commit 8433d45

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

docs/migration/v8-to-v9.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ The `enableTracing` option was removed. In v9, to emulate `enableTracing: true`,
345345
To enable session tracking, it is recommended to unset `autoSessionTracking` and ensure that either, in browser environments the `browserSessionIntegration` is added, or in server environments the `httpIntegration` is added.
346346

347347
To disable session tracking, it is recommended unset `autoSessionTracking` and to remove the `browserSessionIntegration` in browser environments, or in server environments configure the `httpIntegration` with the `trackIncomingRequestsAsSessions` option set to `false`.
348+
Additionally, in Node.js environments, a session was automatically created for every node process when `autoSessionTracking` was set to `true`. This behavior has been replaced by the `processSessionIntegration` which is configured by default.
348349

349350
- **The metrics API has been removed from the SDK.**
350351

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { defineIntegration, endSession, getIsolationScope, startSession } from '@sentry/core';
2+
3+
const INTEGRATION_NAME = 'ProcessSession';
4+
5+
/**
6+
* Records a Session for the current process to track release health.
7+
*/
8+
export const processSessionIntegration = defineIntegration(() => {
9+
return {
10+
name: INTEGRATION_NAME,
11+
setupOnce() {
12+
startSession();
13+
14+
// Emitted in the case of healthy sessions, error of `mechanism.handled: true` and unhandledrejections because
15+
// The 'beforeExit' event is not emitted for conditions causing explicit termination,
16+
// such as calling process.exit() or uncaught exceptions.
17+
// Ref: https://nodejs.org/api/process.html#process_event_beforeexit
18+
process.on('beforeExit', () => {
19+
const session = getIsolationScope().getSession();
20+
21+
// Only call endSession, if the Session exists on Scope and SessionStatus is not a
22+
// Terminal Status i.e. Exited or Crashed because
23+
// "When a session is moved away from ok it must not be updated anymore."
24+
// Ref: https://develop.sentry.dev/sdk/sessions/
25+
if (session?.status !== 'ok') {
26+
endSession();
27+
}
28+
});
29+
},
30+
};
31+
});

packages/node/src/sdk/index.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@ import type { Integration, Options } from '@sentry/core';
22
import {
33
consoleSandbox,
44
dropUndefinedKeys,
5-
endSession,
65
functionToStringIntegration,
76
getCurrentScope,
87
getIntegrationsToSetup,
9-
getIsolationScope,
108
hasTracingEnabled,
119
inboundFiltersIntegration,
1210
linkedErrorsIntegration,
1311
logger,
1412
propagationContextFromHeaders,
1513
requestDataIntegration,
1614
stackParserFromStackParserOptions,
17-
startSession,
1815
} from '@sentry/core';
1916
import {
2017
enhanceDscWithOpenTelemetryRootSpanName,
@@ -33,6 +30,7 @@ import { modulesIntegration } from '../integrations/modules';
3330
import { nativeNodeFetchIntegration } from '../integrations/node-fetch';
3431
import { onUncaughtExceptionIntegration } from '../integrations/onuncaughtexception';
3532
import { onUnhandledRejectionIntegration } from '../integrations/onunhandledrejection';
33+
import { processSessionIntegration } from '../integrations/processSession';
3634
import { INTEGRATION_NAME as SPOTLIGHT_INTEGRATION_NAME, spotlightIntegration } from '../integrations/spotlight';
3735
import { getAutoPerformanceIntegrations } from '../integrations/tracing';
3836
import { makeNodeTransport } from '../transports';
@@ -69,6 +67,7 @@ export function getDefaultIntegrationsWithoutPerformance(): Integration[] {
6967
localVariablesIntegration(),
7068
nodeContextIntegration(),
7169
childProcessIntegration(),
70+
processSessionIntegration(),
7271
...getCjsOnlyIntegrations(),
7372
];
7473
}
@@ -145,8 +144,6 @@ function _init(
145144

146145
logger.log(`Running in ${isCjs() ? 'CommonJS' : 'ESM'} mode.`);
147146

148-
trackSessionForProcess();
149-
150147
client.startClientReportTracking();
151148

152149
updateScopeFromEnvVariables();
@@ -289,28 +286,3 @@ function updateScopeFromEnvVariables(): void {
289286
getCurrentScope().setPropagationContext(propagationContext);
290287
}
291288
}
292-
293-
/**
294-
* Start a session for this process.
295-
*/
296-
// TODO(v9): This is still extremely funky because it's a session on the scope and therefore weirdly mutable by the user.
297-
// Strawman proposal for v9: Either create a processSessionIntegration() or add functionality to the onunhandledexception/rejection integrations.
298-
function trackSessionForProcess(): void {
299-
startSession();
300-
301-
// Emitted in the case of healthy sessions, error of `mechanism.handled: true` and unhandledrejections because
302-
// The 'beforeExit' event is not emitted for conditions causing explicit termination,
303-
// such as calling process.exit() or uncaught exceptions.
304-
// Ref: https://nodejs.org/api/process.html#process_event_beforeexit
305-
process.on('beforeExit', () => {
306-
const session = getIsolationScope().getSession();
307-
308-
// Only call endSession, if the Session exists on Scope and SessionStatus is not a
309-
// Terminal Status i.e. Exited or Crashed because
310-
// "When a session is moved away from ok it must not be updated anymore."
311-
// Ref: https://develop.sentry.dev/sdk/sessions/
312-
if (session?.status !== 'ok') {
313-
endSession();
314-
}
315-
});
316-
}

0 commit comments

Comments
 (0)