Skip to content

Commit 216a056

Browse files
committed
fix: Update requestHandler to correctly pass scope
1 parent 92c37f5 commit 216a056

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

packages/node/src/handlers.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { logger } from '@sentry/core';
22
import { getHubFromCarrier, Scope } from '@sentry/hub';
33
import { SentryEvent, Severity } from '@sentry/types';
44
import { forget } from '@sentry/utils/async';
5-
import { serialize } from '@sentry/utils/object';
5+
import { clone, serialize } from '@sentry/utils/object';
66
import * as cookie from 'cookie';
77
import * as domain from 'domain';
88
import * as http from 'http';
@@ -201,6 +201,40 @@ function parseRequest(
201201
return event;
202202
}
203203

204+
/** Inherit globally configured data from Scope, but make sure to not use references */
205+
function inheritGlobalScope(to: Scope, from: Scope): void {
206+
const user = from.getUser();
207+
if (user) {
208+
to.setUser(clone(user));
209+
}
210+
211+
const tags = from.getTags();
212+
if (tags) {
213+
const clonedTags = clone(tags);
214+
Object.keys(clonedTags).forEach(key => {
215+
to.setTag(key, clonedTags[key]);
216+
});
217+
}
218+
219+
const extra = from.getExtra();
220+
if (extra) {
221+
const clonedExtra = clone(extra);
222+
Object.keys(clonedExtra).forEach(key => {
223+
to.setExtra(key, clonedExtra[key]);
224+
});
225+
}
226+
227+
const fingerprint = from.getFingerprint();
228+
if (fingerprint) {
229+
to.setFingerprint(clone(fingerprint));
230+
}
231+
232+
const level = from.getLevel();
233+
if (level) {
234+
to.setLevel(level);
235+
}
236+
}
237+
204238
/** JSDoc */
205239
export function requestHandler(options?: {
206240
request?: boolean;
@@ -215,9 +249,14 @@ export function requestHandler(options?: {
215249
next: () => void,
216250
): void {
217251
const local = domain.create();
252+
const currentHub = getCurrentHub();
253+
const currentScope = currentHub.getScope();
218254
const hub = getHubFromCarrier(req);
219-
hub.bindClient(getCurrentHub().getClient());
255+
hub.bindClient(currentHub.getClient());
220256
hub.configureScope((scope: Scope) => {
257+
if (currentScope) {
258+
inheritGlobalScope(scope, currentScope);
259+
}
221260
scope.addEventProcessor(async (event: SentryEvent) => parseRequest(event, req, options));
222261
});
223262
local.on('error', next);

packages/utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"scripts": {
2929
"build": "run-s clean; tsc -p tsconfig.build.json",
30-
"build:watch": "run-s clean; tsc -p tsconfig.build.json -w --preserveWatchOutput",
30+
"build:watch": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
3131
"clean": "rimraf dist coverage *.js *.js.map *.d.ts",
3232
"lint": "run-s lint:prettier lint:tslint",
3333
"lint:prettier": "prettier-check '{src,test}/**/*.ts'",

0 commit comments

Comments
 (0)