Skip to content

Commit 1f500be

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

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

packages/hub/src/hub.ts

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

328-
let domain = null;
329328
try {
330-
domain = process.domain as SentryDomain;
331-
} catch (_Oo) {
332-
// We do not have process
333-
}
329+
const domain = process.domain as SentryDomain;
334330

335-
if (!domain) {
336-
return registry.hub;
337-
}
331+
if (!domain.__SENTRY__) {
332+
return registry.hub;
333+
}
338334

339-
let carrier = domain.__SENTRY__;
340-
if (!carrier) {
341-
domain.__SENTRY__ = carrier = {};
342-
}
335+
if (!domain.__SENTRY__.hub || domain.__SENTRY__.hub.isOlderThan(API_VERSION)) {
336+
domain.__SENTRY__.hub = new Hub(registry.hub.getStackTop().client, Scope.clone(registry.hub.getStackTop().scope));
337+
}
343338

344-
if (!carrier.hub) {
345-
const top = registry.hub.getStackTop();
346-
carrier.hub = top ? new Hub(top.client, Scope.clone(top.scope)) : new Hub();
339+
return domain.__SENTRY__.hub;
340+
} catch (_Oo) {
341+
return registry.hub;
347342
}
348-
349-
return carrier.hub;
350343
}
351344

352345
/**
@@ -363,3 +356,17 @@ export function getHubFromCarrier(carrier: any): Hub {
363356
return carrier.__SENTRY__.hub;
364357
}
365358
}
359+
360+
/**
361+
* This will set passed {@link Hub} on the passed object's __SENTRY__.hub attribute
362+
* @param carrier object
363+
* @param hub Hub
364+
*/
365+
export function setHubOnCarrier(carrier: any, hub: Hub): boolean {
366+
if (!carrier) {
367+
return false;
368+
}
369+
carrier.__SENTRY__ = carrier.__SENTRY__ || {};
370+
carrier.__SENTRY__.hub = hub;
371+
return true;
372+
}

packages/hub/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { Carrier, Layer } from './interfaces';
22
export { addGlobalEventProcessor, Scope } from './scope';
3-
export { getCurrentHub, getHubFromCarrier, Hub } from './hub';
3+
export { getCurrentHub, getHubFromCarrier, getMainCarrier, Hub, setHubOnCarrier } from './hub';

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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getCurrentHub } from '@sentry/core';
2-
import { Carrier, Hub } from '@sentry/hub';
32
import { SentryEvent } from '@sentry/types';
43
import { forget } from '@sentry/utils/async';
54
import { logger } from '@sentry/utils/logger';

packages/node/src/sdk.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
1+
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
2+
import { getMainCarrier, setHubOnCarrier } from '@sentry/hub';
23
import { NodeOptions } from './backend';
34
import { NodeClient } from './client';
45
import { Console, Http, LinkedErrors, OnUncaughtException, OnUnhandledRejection } from './integrations';
@@ -66,5 +67,10 @@ export function init(options: NodeOptions = {}): void {
6667
if (options.defaultIntegrations === undefined) {
6768
options.defaultIntegrations = defaultIntegrations;
6869
}
70+
71+
if (process.domain) {
72+
setHubOnCarrier(getMainCarrier(), getCurrentHub());
73+
}
74+
6975
initAndBind(NodeClient, options);
7076
}

0 commit comments

Comments
 (0)