Skip to content

Commit 6b6a2c0

Browse files
committed
Simplify getCurrentHub and inherit hub from previous domain
1 parent b00bf3f commit 6b6a2c0

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

packages/hub/src/hub.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -325,28 +325,16 @@ export function getCurrentHub(): Hub {
325325
registry.hub = new Hub();
326326
}
327327

328-
let domain = null;
329328
try {
330-
domain = process.domain as SentryDomain;
329+
const domain = process.domain as SentryDomain;
330+
domain.__SENTRY__ = domain.__SENTRY__ || {};
331+
if (!domain.__SENTRY__.hub || domain.__SENTRY__.hub.isOlderThan(API_VERSION)) {
332+
domain.__SENTRY__.hub = new Hub(registry.hub.getStackTop().client, Scope.clone(registry.hub.getStackTop().scope));
333+
}
334+
return domain.__SENTRY__.hub;
331335
} catch (_Oo) {
332-
// We do not have process
333-
}
334-
335-
if (!domain) {
336336
return registry.hub;
337337
}
338-
339-
let carrier = domain.__SENTRY__;
340-
if (!carrier) {
341-
domain.__SENTRY__ = carrier = {};
342-
}
343-
344-
if (!carrier.hub) {
345-
const top = registry.hub.getStackTop();
346-
carrier.hub = top ? new Hub(top.client, Scope.clone(top.scope)) : new Hub();
347-
}
348-
349-
return carrier.hub;
350338
}
351339

352340
/**

packages/node/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"dependencies": {
1818
"@sentry/core": "4.1.1",
19+
"@sentry/hub": "4.1.1",
1920
"@sentry/types": "4.1.0",
2021
"@sentry/utils": "4.1.1",
2122
"cookie": "0.3.1",

packages/node/src/handlers.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ function parseRequest(
205205
return event;
206206
}
207207

208+
/** Domain interface with attached Hub */
209+
interface SentryDomain extends NodeJS.Domain {
210+
__SENTRY__?: Carrier;
211+
}
212+
208213
/** JSDoc */
209214
export function requestHandler(options?: {
210215
request?: boolean;
@@ -218,11 +223,25 @@ export function requestHandler(options?: {
218223
res: http.ServerResponse,
219224
next: (error?: any) => void,
220225
): void {
221-
const local = domain.create();
226+
const alreadyRunningDomain = process.domain as SentryDomain;
227+
let alreadyRunningDomainHub: Hub | undefined;
228+
229+
if (alreadyRunningDomain) {
230+
if (alreadyRunningDomain.__SENTRY__) {
231+
alreadyRunningDomainHub = alreadyRunningDomain.__SENTRY__.hub;
232+
}
233+
}
234+
235+
const local = domain.create() as SentryDomain;
222236
local.add(req);
223237
local.add(res);
224238
local.on('error', next);
225239
local.run(() => {
240+
if (alreadyRunningDomainHub) {
241+
local.__SENTRY__ = {
242+
hub: alreadyRunningDomainHub,
243+
};
244+
}
226245
getCurrentHub().configureScope(scope =>
227246
scope.addEventProcessor(async (event: SentryEvent) => parseRequest(event, req, options)),
228247
);

0 commit comments

Comments
 (0)