Skip to content

feat(node): Add processSessionIntegration #15081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/migration/v8-to-v9.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.**

Expand Down
31 changes: 31 additions & 0 deletions packages/node/src/integrations/processSession.ts
Original file line number Diff line number Diff line change
@@ -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();
}
});
},
};
});
32 changes: 2 additions & 30 deletions packages/node/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ import type { Integration, Options } from '@sentry/core';
import {
consoleSandbox,
dropUndefinedKeys,
endSession,
functionToStringIntegration,
getCurrentScope,
getIntegrationsToSetup,
getIsolationScope,
hasTracingEnabled,
inboundFiltersIntegration,
linkedErrorsIntegration,
logger,
propagationContextFromHeaders,
requestDataIntegration,
stackParserFromStackParserOptions,
startSession,
} from '@sentry/core';
import {
enhanceDscWithOpenTelemetryRootSpanName,
Expand All @@ -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';
Expand Down Expand Up @@ -69,6 +67,7 @@ export function getDefaultIntegrationsWithoutPerformance(): Integration[] {
localVariablesIntegration(),
nodeContextIntegration(),
childProcessIntegration(),
processSessionIntegration(),
...getCjsOnlyIntegrations(),
];
}
Expand Down Expand Up @@ -145,8 +144,6 @@ function _init(

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

trackSessionForProcess();

client.startClientReportTracking();

updateScopeFromEnvVariables();
Expand Down Expand Up @@ -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();
}
});
}
Loading