Skip to content

Commit fdb6bb5

Browse files
committed
revert: set request session on current scope
1 parent 1675734 commit fdb6bb5

File tree

2 files changed

+74
-48
lines changed

2 files changed

+74
-48
lines changed

packages/node/src/handlers.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
normalize,
2929
} from '@sentry/utils';
3030

31+
import { ServerRuntimeClient } from '@sentry/core';
3132
import type { NodeClient } from './client';
3233
import { DEBUG_BUILD } from './debug-build';
3334
// TODO (v8 / XXX) Remove this import
@@ -165,9 +166,9 @@ export function requestHandler(
165166
client.initSessionFlusher();
166167

167168
// If Scope contains a Single mode Session, it is removed in favor of using Session Aggregates mode
168-
const isolationScope = getIsolationScope();
169-
if (isolationScope.getSession()) {
170-
isolationScope.setSession();
169+
const scope = getCurrentScope();
170+
if (scope.getSession()) {
171+
scope.setSession();
171172
}
172173
}
173174

@@ -200,19 +201,17 @@ export function requestHandler(
200201
const client = getClient<NodeClient>();
201202
if (isAutoSessionTrackingEnabled(client)) {
202203
// Set `status` of `RequestSession` to Ok, at the beginning of the request
203-
isolationScope.setRequestSession({ status: 'ok' });
204+
getCurrentScope().setRequestSession({ status: 'ok' });
204205
}
205206

206207
res.once('finish', () => {
207-
const client = getClient<NodeClient>();
208-
if (isAutoSessionTrackingEnabled(client)) {
208+
const client = getClient<ServerRuntimeClient>();
209+
if (client && isAutoSessionTrackingEnabled(client)) {
209210
setImmediate(() => {
210-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
211-
if (client && (client as any)._captureRequestSession) {
211+
if (client['_captureRequestSession']) {
212212
// Calling _captureRequestSession to capture request session at the end of the request by incrementing
213213
// the correct SessionAggregates bucket i.e. crashed, errored or exited
214-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
215-
(client as any)._captureRequestSession();
214+
client['_captureRequestSession']();
216215
}
217216
});
218217
}
@@ -273,7 +272,7 @@ export function errorHandler(options?: {
273272
// The request should already have been stored in `scope.sdkProcessingMetadata` by `sentryRequestMiddleware`,
274273
// but on the off chance someone is using `sentryErrorMiddleware` without `sentryRequestMiddleware`, it doesn't
275274
// hurt to be sure
276-
_scope.setSDKProcessingMetadata({ request: _req });
275+
getIsolationScope().setSDKProcessingMetadata({ request: _req });
277276

278277
// For some reason we need to set the transaction on the scope again
279278
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access

packages/node/test/handlers.test.ts

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
mergeScopeData,
1313
spanToJSON,
1414
} from '@sentry/core';
15-
import type { Event, PropagationContext } from '@sentry/types';
15+
import type { Event, PropagationContext, Scope } from '@sentry/types';
1616
import { SentryError } from '@sentry/utils';
1717

1818
import { NodeClient } from '../src/client';
@@ -61,18 +61,24 @@ describe('requestHandler', () => {
6161
jest.restoreAllMocks();
6262
});
6363

64-
it('autoSessionTracking is enabled, sets requestSession status to ok, when handling a request', () => {
64+
it('autoSessionTracking is enabled, sets requestSession status to ok, when handling a request', done => {
6565
const options = getDefaultNodeClientOptions({ autoSessionTracking: true, release: '1.2' });
6666
client = new NodeClient(options);
6767
// eslint-disable-next-line deprecation/deprecation
6868
const hub = new Hub(client);
6969
// eslint-disable-next-line deprecation/deprecation
7070
makeMain(hub);
7171

72-
sentryRequestMiddleware(req, res, next);
72+
let scope: Scope;
73+
sentryRequestMiddleware(req, res, () => {
74+
scope = getCurrentScope();
75+
return next();
76+
});
7377

74-
const isolationScope = getIsolationScope();
75-
expect(isolationScope.getRequestSession()).toEqual({ status: 'ok' });
78+
setImmediate(() => {
79+
expect(scope.getRequestSession()).toEqual({ status: 'ok' });
80+
done();
81+
});
7682
});
7783

7884
it('autoSessionTracking is disabled, does not set requestSession, when handling a request', () => {
@@ -83,13 +89,18 @@ describe('requestHandler', () => {
8389
// eslint-disable-next-line deprecation/deprecation
8490
makeMain(hub);
8591

86-
sentryRequestMiddleware(req, res, next);
92+
let scope: Scope;
93+
sentryRequestMiddleware(req, res, () => {
94+
scope = getCurrentScope();
95+
return next();
96+
});
8797

88-
const scope = getCurrentScope();
89-
expect(scope?.getRequestSession()).toBeUndefined();
98+
setImmediate(() => {
99+
expect(scope.getRequestSession()).toBeUndefined();
100+
});
90101
});
91102

92-
it('autoSessionTracking is enabled, calls _captureRequestSession, on response finish', done => {
103+
it('autoSessionTracking is enabled, calls _captureRequestSession, on response finish xxx', done => {
93104
const options = getDefaultNodeClientOptions({ autoSessionTracking: true, release: '1.2' });
94105
client = new NodeClient(options);
95106
// eslint-disable-next-line deprecation/deprecation
@@ -99,13 +110,16 @@ describe('requestHandler', () => {
99110

100111
const captureRequestSession = jest.spyOn<any, any>(client, '_captureRequestSession');
101112

102-
sentryRequestMiddleware(req, res, next);
113+
let scope: Scope;
114+
sentryRequestMiddleware(req, res, () => {
115+
scope = getCurrentScope();
116+
return next();
117+
});
103118

104-
const isolationScope = getIsolationScope();
105119
res.emit('finish');
106120

107121
setImmediate(() => {
108-
expect(isolationScope.getRequestSession()).toEqual({ status: 'ok' });
122+
expect(scope.getRequestSession()).toEqual({ status: 'ok' });
109123
expect(captureRequestSession).toHaveBeenCalled();
110124
done();
111125
});
@@ -122,11 +136,10 @@ describe('requestHandler', () => {
122136
const captureRequestSession = jest.spyOn<any, any>(client, '_captureRequestSession');
123137

124138
sentryRequestMiddleware(req, res, next);
125-
const scope = getCurrentScope();
126139
res.emit('finish');
127140

128141
setImmediate(() => {
129-
expect(scope?.getRequestSession()).toBeUndefined();
142+
expect(getCurrentScope().getRequestSession()).toBeUndefined();
130143
expect(captureRequestSession).not.toHaveBeenCalled();
131144
done();
132145
});
@@ -479,10 +492,8 @@ describe('tracingHandler', () => {
479492
const options = getDefaultNodeClientOptions({ tracesSampleRate: 1.0 });
480493
// eslint-disable-next-line deprecation/deprecation
481494
const hub = new Hub(new NodeClient(options));
482-
483-
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
484495
// eslint-disable-next-line deprecation/deprecation
485-
jest.spyOn(sentryCore, 'getCurrentScope').mockImplementation(() => hub.getScope());
496+
makeMain(hub);
486497

487498
sentryTracingMiddleware(req, res, next);
488499

@@ -538,31 +549,40 @@ describe('errorHandler()', () => {
538549
const scope = getCurrentScope();
539550
// eslint-disable-next-line deprecation/deprecation
540551
const hub = new Hub(client);
552+
// eslint-disable-next-line deprecation/deprecation
553+
makeMain(hub);
541554

542555
jest.spyOn<any, any>(client, '_captureRequestSession');
543-
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
544556

545-
scope?.setRequestSession({ status: 'ok' });
557+
scope.setRequestSession({ status: 'ok' });
546558
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, next);
547-
const requestSession = scope?.getRequestSession();
559+
const requestSession = scope.getRequestSession();
548560
expect(requestSession).toEqual({ status: 'ok' });
549561
});
550562

551-
it('autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash', () => {
563+
it('autoSessionTracking is enabled + requestHandler is not used -> does not set requestSession status on Crash', done => {
552564
const options = getDefaultNodeClientOptions({ autoSessionTracking: false, release: '3.3' });
553565
client = new NodeClient(options);
554566

555-
const scope = getCurrentScope();
556567
// eslint-disable-next-line deprecation/deprecation
557568
const hub = new Hub(client);
569+
// eslint-disable-next-line deprecation/deprecation
570+
makeMain(hub);
558571

559572
jest.spyOn<any, any>(client, '_captureRequestSession');
560-
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
561573

562-
scope?.setRequestSession({ status: 'ok' });
563-
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, next);
564-
const requestSession = scope?.getRequestSession();
565-
expect(requestSession).toEqual({ status: 'ok' });
574+
getCurrentScope().setRequestSession({ status: 'ok' });
575+
576+
let scope: Scope;
577+
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, () => {
578+
scope = getCurrentScope();
579+
return next();
580+
});
581+
582+
setImmediate(() => {
583+
expect(scope.getRequestSession()).toEqual({ status: 'ok' });
584+
done();
585+
});
566586
});
567587

568588
it('when autoSessionTracking is enabled, should set requestSession status to Crashed when an unhandled error occurs within the bounds of a request', () => {
@@ -580,16 +600,16 @@ describe('errorHandler()', () => {
580600
jest.spyOn<any, any>(client, '_captureRequestSession');
581601

582602
hub.run(() => {
583-
scope?.setRequestSession({ status: 'ok' });
603+
scope.setRequestSession({ status: 'ok' });
584604
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, () => {
585605
const scope = getCurrentScope();
586-
const requestSession = scope?.getRequestSession();
606+
const requestSession = scope.getRequestSession();
587607
expect(requestSession).toEqual({ status: 'crashed' });
588608
});
589609
});
590610
});
591611

592-
it('when autoSessionTracking is enabled, should not set requestSession status on Crash when it occurs outside the bounds of a request', () => {
612+
it('when autoSessionTracking is enabled, should not set requestSession status on Crash when it occurs outside the bounds of a request', done => {
593613
const options = getDefaultNodeClientOptions({ autoSessionTracking: true, release: '2.2' });
594614
client = new NodeClient(options);
595615
// It is required to initialise SessionFlusher to capture Session Aggregates (it is usually initialised
@@ -598,13 +618,21 @@ describe('errorHandler()', () => {
598618
const scope = new ScopeClass();
599619
// eslint-disable-next-line deprecation/deprecation
600620
const hub = new Hub(client, scope);
621+
// eslint-disable-next-line deprecation/deprecation
622+
makeMain(hub);
601623

602624
jest.spyOn<any, any>(client, '_captureRequestSession');
603-
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
604625

605-
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, next);
606-
const requestSession = scope?.getRequestSession();
607-
expect(requestSession).toEqual(undefined);
626+
let currentScope: Scope;
627+
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, () => {
628+
currentScope = getCurrentScope();
629+
return next();
630+
});
631+
632+
setImmediate(() => {
633+
expect(currentScope.getRequestSession()).toEqual(undefined);
634+
done();
635+
});
608636
});
609637

610638
it('stores request in `sdkProcessingMetadata`', () => {
@@ -620,9 +648,8 @@ describe('errorHandler()', () => {
620648
// `captureException` in order to examine the scope as it exists inside the `withScope` callback
621649
// eslint-disable-next-line deprecation/deprecation
622650
hub.captureException = function (this: Hub, _exception: any) {
623-
// eslint-disable-next-line deprecation/deprecation
624-
const scope = this.getScope();
625-
expect((scope as any)._sdkProcessingMetadata.request).toEqual(req);
651+
const scope = getIsolationScope();
652+
expect(scope.getScopeData().sdkProcessingMetadata.request).toEqual(req);
626653
} as any;
627654

628655
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, next);

0 commit comments

Comments
 (0)