Skip to content

Commit d58b3d2

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

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [node] feat: Make node transactions a pluggable integration with tests
2929
- [node] feat: Transactions handling for RequestHandler in Express/Hapi
3030
- [node] fix: Dont wrap native modules more than once to prevent leaks
31+
- [node] fix: Update requestHandler to correctly pass scope
3132
- [utils] ref: implemented includes, assign and isNaN polyfills
3233

3334
## 4.0.6

packages/hub/src/scope.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Breadcrumb, SentryEvent, SentryEventHint, Severity, User } from '@sentry/types';
2-
import { assign } from '@sentry/utils/object';
2+
import { assign, clone } from '@sentry/utils/object';
33

44
/**
55
* Holds additional event information. {@link Scope.applyToEvent} will be
@@ -185,6 +185,40 @@ export class Scope {
185185
this.notifyScopeListeners();
186186
}
187187

188+
/** Inherit user data from provided scope, but make sure to not use references */
189+
public inheritUserData(from: Scope): void {
190+
const user = from.getUser();
191+
if (user) {
192+
this.setUser(clone(user));
193+
}
194+
195+
const tags = from.getTags();
196+
if (tags) {
197+
const clonedTags = clone(tags);
198+
Object.keys(clonedTags).forEach(key => {
199+
this.setTag(key, clonedTags[key]);
200+
});
201+
}
202+
203+
const extra = from.getExtra();
204+
if (extra) {
205+
const clonedExtra = clone(extra);
206+
Object.keys(clonedExtra).forEach(key => {
207+
this.setExtra(key, clonedExtra[key]);
208+
});
209+
}
210+
211+
const fingerprint = from.getFingerprint();
212+
if (fingerprint) {
213+
this.setFingerprint(clone(fingerprint));
214+
}
215+
216+
const level = from.getLevel();
217+
if (level) {
218+
this.setLevel(level);
219+
}
220+
}
221+
188222
/**
189223
* Sets the breadcrumbs in the scope
190224
* @param breadcrumbs Breadcrumb

packages/node/src/handlers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,14 @@ export function requestHandler(options?: {
215215
next: () => void,
216216
): void {
217217
const local = domain.create();
218+
const currentHub = getCurrentHub();
219+
const currentScope = currentHub.getScope();
218220
const hub = getHubFromCarrier(req);
219-
hub.bindClient(getCurrentHub().getClient());
221+
hub.bindClient(currentHub.getClient());
220222
hub.configureScope((scope: Scope) => {
223+
if (currentScope) {
224+
scope.inheritUserData(currentScope);
225+
}
221226
scope.addEventProcessor(async (event: SentryEvent) => parseRequest(event, req, options));
222227
});
223228
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)