From f2bfa66c55758ccca9f0a6cf7b454ca4843b4c1e Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 20 Jan 2025 15:10:49 +0000 Subject: [PATCH] feat(node): Add `processSessionIntegration` --- docs/migration/v8-to-v9.md | 1 + .../node/src/integrations/processSession.ts | 31 ++++++++++++++++++ packages/node/src/sdk/index.ts | 32 ++----------------- 3 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 packages/node/src/integrations/processSession.ts diff --git a/docs/migration/v8-to-v9.md b/docs/migration/v8-to-v9.md index 7eaf78ed81d4..fb0b51a703e6 100644 --- a/docs/migration/v8-to-v9.md +++ b/docs/migration/v8-to-v9.md @@ -345,6 +345,7 @@ The `enableTracing` option was removed. In v9, to emulate `enableTracing: true`, 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. 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`. +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. - **The metrics API has been removed from the SDK.** diff --git a/packages/node/src/integrations/processSession.ts b/packages/node/src/integrations/processSession.ts new file mode 100644 index 000000000000..16cb9509b6e2 --- /dev/null +++ b/packages/node/src/integrations/processSession.ts @@ -0,0 +1,31 @@ +import { defineIntegration, endSession, getIsolationScope, startSession } from '@sentry/core'; + +const INTEGRATION_NAME = 'ProcessSession'; + +/** + * Records a Session for the current process to track release health. + */ +export const processSessionIntegration = defineIntegration(() => { + return { + name: INTEGRATION_NAME, + setupOnce() { + startSession(); + + // Emitted in the case of healthy sessions, error of `mechanism.handled: true` and unhandledrejections because + // The 'beforeExit' event is not emitted for conditions causing explicit termination, + // such as calling process.exit() or uncaught exceptions. + // Ref: https://nodejs.org/api/process.html#process_event_beforeexit + process.on('beforeExit', () => { + const session = getIsolationScope().getSession(); + + // Only call endSession, if the Session exists on Scope and SessionStatus is not a + // Terminal Status i.e. Exited or Crashed because + // "When a session is moved away from ok it must not be updated anymore." + // Ref: https://develop.sentry.dev/sdk/sessions/ + if (session?.status !== 'ok') { + endSession(); + } + }); + }, + }; +}); diff --git a/packages/node/src/sdk/index.ts b/packages/node/src/sdk/index.ts index a741b4b43f80..4a4900cf5b60 100644 --- a/packages/node/src/sdk/index.ts +++ b/packages/node/src/sdk/index.ts @@ -2,11 +2,9 @@ import type { Integration, Options } from '@sentry/core'; import { consoleSandbox, dropUndefinedKeys, - endSession, functionToStringIntegration, getCurrentScope, getIntegrationsToSetup, - getIsolationScope, hasTracingEnabled, inboundFiltersIntegration, linkedErrorsIntegration, @@ -14,7 +12,6 @@ import { propagationContextFromHeaders, requestDataIntegration, stackParserFromStackParserOptions, - startSession, } from '@sentry/core'; import { enhanceDscWithOpenTelemetryRootSpanName, @@ -33,6 +30,7 @@ import { modulesIntegration } from '../integrations/modules'; import { nativeNodeFetchIntegration } from '../integrations/node-fetch'; import { onUncaughtExceptionIntegration } from '../integrations/onuncaughtexception'; import { onUnhandledRejectionIntegration } from '../integrations/onunhandledrejection'; +import { processSessionIntegration } from '../integrations/processSession'; import { INTEGRATION_NAME as SPOTLIGHT_INTEGRATION_NAME, spotlightIntegration } from '../integrations/spotlight'; import { getAutoPerformanceIntegrations } from '../integrations/tracing'; import { makeNodeTransport } from '../transports'; @@ -69,6 +67,7 @@ export function getDefaultIntegrationsWithoutPerformance(): Integration[] { localVariablesIntegration(), nodeContextIntegration(), childProcessIntegration(), + processSessionIntegration(), ...getCjsOnlyIntegrations(), ]; } @@ -145,8 +144,6 @@ function _init( logger.log(`Running in ${isCjs() ? 'CommonJS' : 'ESM'} mode.`); - trackSessionForProcess(); - client.startClientReportTracking(); updateScopeFromEnvVariables(); @@ -289,28 +286,3 @@ function updateScopeFromEnvVariables(): void { getCurrentScope().setPropagationContext(propagationContext); } } - -/** - * Start a session for this process. - */ -// TODO(v9): This is still extremely funky because it's a session on the scope and therefore weirdly mutable by the user. -// Strawman proposal for v9: Either create a processSessionIntegration() or add functionality to the onunhandledexception/rejection integrations. -function trackSessionForProcess(): void { - startSession(); - - // Emitted in the case of healthy sessions, error of `mechanism.handled: true` and unhandledrejections because - // The 'beforeExit' event is not emitted for conditions causing explicit termination, - // such as calling process.exit() or uncaught exceptions. - // Ref: https://nodejs.org/api/process.html#process_event_beforeexit - process.on('beforeExit', () => { - const session = getIsolationScope().getSession(); - - // Only call endSession, if the Session exists on Scope and SessionStatus is not a - // Terminal Status i.e. Exited or Crashed because - // "When a session is moved away from ok it must not be updated anymore." - // Ref: https://develop.sentry.dev/sdk/sessions/ - if (session?.status !== 'ok') { - endSession(); - } - }); -}