Skip to content

Commit 89b4c0c

Browse files
author
Luca Forstner
committed
feat(core): Add serverRequestSessionIntegration
1 parent 0b349eb commit 89b4c0c

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export { inboundFiltersIntegration } from './integrations/inboundfilters';
9292
export { linkedErrorsIntegration } from './integrations/linkederrors';
9393
export { moduleMetadataIntegration } from './integrations/metadata';
9494
export { requestDataIntegration } from './integrations/requestdata';
95+
export { serverRequestSessionIntegration } from './integrations/serverrequestsession';
9596
export { captureConsoleIntegration } from './integrations/captureconsole';
9697
// eslint-disable-next-line deprecation/deprecation
9798
export { debugIntegration } from './integrations/debug';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { startSession } from '..';
2+
import { getIsolationScope } from '../currentScopes';
3+
import { defineIntegration } from '../integration';
4+
5+
export const serverRequestSessionIntegration = defineIntegration(() => {
6+
return {
7+
name: 'ServerRequestSession',
8+
setup(client) {
9+
client.on('preprocessEvent', event => {
10+
const isException =
11+
event.type === undefined && event.exception && event.exception.values && event.exception.values.length > 0;
12+
13+
// If the event is of type Exception, then a request session should be captured
14+
if (isException) {
15+
const requestSession = getIsolationScope().getRequestSession();
16+
17+
// Ensure that this is happening within the bounds of a request, and make sure not to override
18+
// Session Status if Errored / Crashed
19+
if (requestSession && requestSession.status === 'ok') {
20+
requestSession.status = 'errored';
21+
}
22+
}
23+
});
24+
25+
// TODO(v9): Remove this start session call
26+
startSession();
27+
},
28+
};
29+
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ export class ServerRuntimeClient<
134134
return super.close(timeout);
135135
}
136136

137-
/** Method that initialises an instance of SessionFlusher on Client */
137+
/** Method that initializes an instance of SessionFlusher on Client */
138138
public initSessionFlusher(): void {
139139
const { release, environment } = this._options;
140140
if (!release) {
141-
DEBUG_BUILD && logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!');
141+
DEBUG_BUILD && logger.warn('Cannot initialize an instance of SessionFlusher if no release is provided!');
142142
} else {
143143
this._sessionFlusher = new SessionFlusher(this, {
144144
release,

packages/node/src/sdk/index.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
dropUndefinedKeys,
55
endSession,
66
functionToStringIntegration,
7-
getClient,
87
getCurrentScope,
98
getIntegrationsToSetup,
109
getIsolationScope,
@@ -14,8 +13,8 @@ import {
1413
logger,
1514
propagationContextFromHeaders,
1615
requestDataIntegration,
16+
serverRequestSessionIntegration,
1717
stackParserFromStackParserOptions,
18-
startSession,
1918
} from '@sentry/core';
2019
import {
2120
enhanceDscWithOpenTelemetryRootSpanName,
@@ -76,14 +75,21 @@ export function getDefaultIntegrationsWithoutPerformance(): Integration[] {
7675

7776
/** Get the default integrations for the Node SDK. */
7877
export function getDefaultIntegrations(options: Options): Integration[] {
79-
return [
78+
const integrations = [
8079
...getDefaultIntegrationsWithoutPerformance(),
8180
// We only add performance integrations if tracing is enabled
8281
// Note that this means that without tracing enabled, e.g. `expressIntegration()` will not be added
8382
// This means that generally request isolation will work (because that is done by httpIntegration)
8483
// But `transactionName` will not be set automatically
8584
...(shouldAddPerformanceIntegrations(options) ? getAutoPerformanceIntegrations() : []),
8685
];
86+
87+
// TODO(v9): Make this a default default integration
88+
if (options.autoSessionTracking) {
89+
integrations.push(serverRequestSessionIntegration());
90+
}
91+
92+
return integrations;
8793
}
8894

8995
function shouldAddPerformanceIntegrations(options: Options): boolean {
@@ -156,8 +162,9 @@ function _init(
156162

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

165+
// TODO(V9): Unconditionally call startSessionTracking
159166
if (options.autoSessionTracking) {
160-
startSessionTracking();
167+
startSessionTracking(client);
161168
}
162169

163170
client.startClientReportTracking();
@@ -308,17 +315,11 @@ function updateScopeFromEnvVariables(): void {
308315
getCurrentScope().setPropagationContext(propagationContext);
309316
}
310317
}
311-
312318
/**
313319
* Enable automatic Session Tracking for the node process.
314320
*/
315-
function startSessionTracking(): void {
316-
const client = getClient<NodeClient>();
317-
if (client && client.getOptions().autoSessionTracking) {
318-
client.initSessionFlusher();
319-
}
320-
321-
startSession();
321+
function startSessionTracking(client: NodeClient): void {
322+
client.initSessionFlusher();
322323

323324
// Emitted in the case of healthy sessions, error of `mechanism.handled: true` and unhandledrejections because
324325
// The 'beforeExit' event is not emitted for conditions causing explicit termination,

0 commit comments

Comments
 (0)