Skip to content

feat(core): Add serverRequestSessionIntegration #14556

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

Closed
wants to merge 2 commits into from
Closed
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 packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export { inboundFiltersIntegration } from './integrations/inboundfilters';
export { linkedErrorsIntegration } from './integrations/linkederrors';
export { moduleMetadataIntegration } from './integrations/metadata';
export { requestDataIntegration } from './integrations/requestdata';
export { serverRequestSessionIntegration } from './integrations/serverrequestsession';
export { captureConsoleIntegration } from './integrations/captureconsole';
// eslint-disable-next-line deprecation/deprecation
export { debugIntegration } from './integrations/debug';
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/integrations/serverrequestsession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getIsolationScope } from '../currentScopes';
import { startSession } from '../exports';
import { defineIntegration } from '../integration';

export const serverRequestSessionIntegration = defineIntegration(() => {
return {
name: 'ServerRequestSession',
setup(client) {
client.on('preprocessEvent', event => {
const isException =
event.type === undefined && event.exception && event.exception.values && event.exception.values.length > 0;

// If the event is of type Exception, then a request session should be captured
if (isException) {
const requestSession = getIsolationScope().getRequestSession();

// Ensure that this is happening within the bounds of a request, and make sure not to override
// Session Status if Errored / Crashed
if (requestSession && requestSession.status === 'ok') {
requestSession.status = 'errored';
}
}
});

// TODO(v9): Remove this start session call
startSession();
},
};
});
4 changes: 2 additions & 2 deletions packages/core/src/server-runtime-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ export class ServerRuntimeClient<
return super.close(timeout);
}

/** Method that initialises an instance of SessionFlusher on Client */
/** Method that initializes an instance of SessionFlusher on Client */
public initSessionFlusher(): void {
const { release, environment } = this._options;
if (!release) {
DEBUG_BUILD && logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!');
DEBUG_BUILD && logger.warn('Cannot initialize an instance of SessionFlusher if no release is provided!');
} else {
this._sessionFlusher = new SessionFlusher(this, {
release,
Expand Down
25 changes: 13 additions & 12 deletions packages/node/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
dropUndefinedKeys,
endSession,
functionToStringIntegration,
getClient,
getCurrentScope,
getIntegrationsToSetup,
getIsolationScope,
Expand All @@ -14,8 +13,8 @@ import {
logger,
propagationContextFromHeaders,
requestDataIntegration,
serverRequestSessionIntegration,
stackParserFromStackParserOptions,
startSession,
} from '@sentry/core';
import {
enhanceDscWithOpenTelemetryRootSpanName,
Expand Down Expand Up @@ -76,14 +75,21 @@ export function getDefaultIntegrationsWithoutPerformance(): Integration[] {

/** Get the default integrations for the Node SDK. */
export function getDefaultIntegrations(options: Options): Integration[] {
return [
const integrations = [
...getDefaultIntegrationsWithoutPerformance(),
// We only add performance integrations if tracing is enabled
// Note that this means that without tracing enabled, e.g. `expressIntegration()` will not be added
// This means that generally request isolation will work (because that is done by httpIntegration)
// But `transactionName` will not be set automatically
...(shouldAddPerformanceIntegrations(options) ? getAutoPerformanceIntegrations() : []),
];

// TODO(v9): Make this a default default integration
if (options.autoSessionTracking) {
integrations.push(serverRequestSessionIntegration());
}

return integrations;
}

function shouldAddPerformanceIntegrations(options: Options): boolean {
Expand Down Expand Up @@ -156,8 +162,9 @@ function _init(

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

// TODO(V9): Unconditionally call startSessionTracking
if (options.autoSessionTracking) {
startSessionTracking();
startSessionTracking(client);
}

client.startClientReportTracking();
Expand Down Expand Up @@ -308,17 +315,11 @@ function updateScopeFromEnvVariables(): void {
getCurrentScope().setPropagationContext(propagationContext);
}
}

/**
* Enable automatic Session Tracking for the node process.
*/
function startSessionTracking(): void {
const client = getClient<NodeClient>();
if (client && client.getOptions().autoSessionTracking) {
client.initSessionFlusher();
}

startSession();
function startSessionTracking(client: NodeClient): void {
client.initSessionFlusher();

// 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,
Expand Down
Loading