Skip to content

Commit 7dfa746

Browse files
authored
ref: Refactor remaining makeMain usage (#10713)
Instead, use a combination of `setCurrentClient()` and `client.init()`. I also refactored the domain ACS to avoid using the hub on the carrier (removing this overall will happen in a follow up PR). I also removed the tracing `hub` test as that should be covered in other places and uses a lot of internal hub stuff etc.
1 parent ac39685 commit 7dfa746

File tree

9 files changed

+141
-790
lines changed

9 files changed

+141
-790
lines changed

packages/gatsby/gatsby-browser.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-console */
2-
import { init } from '@sentry/gatsby';
2+
import { getClient, init } from '@sentry/gatsby';
33

44
export function onClientEntry(_, pluginParams) {
55
const isIntialized = isSentryInitialized();
@@ -32,10 +32,7 @@ export function onClientEntry(_, pluginParams) {
3232
}
3333

3434
function isSentryInitialized() {
35-
// Although `window` should exist because we're in the browser (where this script
36-
// is run), and `__SENTRY__.hub` is created when importing the Gatsby SDK, double
37-
// check that in case something weird happens.
38-
return !!(window && window.__SENTRY__ && window.__SENTRY__.hub && window.__SENTRY__.hub.getClient());
35+
return !!getClient();
3936
}
4037

4138
function areSentryOptionsDefined(params) {

packages/gatsby/test/gatsby-browser.test.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
/* eslint-disable @typescript-eslint/no-var-requires */
2-
/* eslint-disable @typescript-eslint/no-explicit-any */
3-
1+
import type { Client } from '@sentry/types';
42
import { onClientEntry } from '../gatsby-browser';
5-
import { browserTracingIntegration } from '../src/index';
3+
import { browserTracingIntegration, getCurrentScope, getIsolationScope, setCurrentClient } from '../src/index';
64

75
(global as any).__SENTRY_RELEASE__ = '683f3a6ab819d47d23abfca9a914c81f0524d35b';
86
(global as any).__SENTRY_DSN__ = 'https://[email protected]/0';
@@ -50,24 +48,21 @@ describe('onClientEntry', () => {
5048
});
5149

5250
describe('inits Sentry once', () => {
51+
beforeEach(() => {
52+
getCurrentScope().clear();
53+
getIsolationScope().clear();
54+
getCurrentScope().setClient(undefined);
55+
});
56+
5357
afterEach(() => {
54-
delete (window as any).__SENTRY__;
5558
(global.console.warn as jest.Mock).mockClear();
5659
(global.console.error as jest.Mock).mockClear();
5760
});
5861

59-
function setMockedSentryInWindow() {
60-
(window as any).__SENTRY__ = {
61-
hub: {
62-
getClient: () => ({
63-
// Empty object mocking the client
64-
}),
65-
},
66-
};
67-
}
68-
6962
it('initialized in injected config, without pluginParams', () => {
70-
setMockedSentryInWindow();
63+
const client = {} as Client;
64+
setCurrentClient(client);
65+
7166
onClientEntry(undefined, { plugins: [] });
7267
// eslint-disable-next-line no-console
7368
expect(console.warn).not.toHaveBeenCalled();
@@ -77,7 +72,9 @@ describe('onClientEntry', () => {
7772
});
7873

7974
it('initialized in injected config, with pluginParams', () => {
80-
setMockedSentryInWindow();
75+
const client = {} as Client;
76+
setCurrentClient(client);
77+
8178
onClientEntry(undefined, { plugins: [], dsn: 'dsn', release: 'release' });
8279
// eslint-disable-next-line no-console
8380
expect((console.warn as jest.Mock).mock.calls[0]).toMatchInlineSnapshot(`

packages/node/src/async/domain.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
import * as domain from 'domain';
2-
import type { Carrier } from '@sentry/core';
32
import { getGlobalHub } from '@sentry/core';
43
import { Hub as HubClass } from '@sentry/core';
5-
import { ensureHubOnCarrier, getHubFromCarrier, setAsyncContextStrategy, setHubOnCarrier } from '@sentry/core';
4+
import { setAsyncContextStrategy } from '@sentry/core';
65
import type { Client, Hub, Scope } from '@sentry/types';
76

7+
type DomainWithHub = domain.Domain & {
8+
hub?: Hub;
9+
};
10+
811
function getActiveDomain<T>(): T | undefined {
912
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
1013
return (domain as any).active as T | undefined;
1114
}
1215

1316
function getCurrentDomainHub(): Hub | undefined {
14-
const activeDomain = getActiveDomain<Carrier>();
17+
const activeDomain = getActiveDomain<DomainWithHub>();
1518

1619
// If there's no active domain, just return undefined and the global hub will be used
1720
if (!activeDomain) {
1821
return undefined;
1922
}
2023

21-
ensureHubOnCarrier(activeDomain);
24+
if (activeDomain.hub) {
25+
return activeDomain.hub;
26+
}
2227

23-
return getHubFromCarrier(activeDomain);
28+
activeDomain.hub = getCurrentHub();
29+
return activeDomain.hub;
2430
}
2531

2632
function getCurrentHub(): Hub {
@@ -33,11 +39,11 @@ function withExecutionContext<T>(
3339
isolationScope: Scope,
3440
callback: () => T,
3541
): T {
36-
const local = domain.create() as domain.Domain & Carrier;
42+
const local = domain.create() as DomainWithHub;
3743

3844
// eslint-disable-next-line deprecation/deprecation
3945
const newHub = new HubClass(client, scope, isolationScope);
40-
setHubOnCarrier(local, newHub);
46+
local.hub = newHub;
4147

4248
return local.bind(() => {
4349
return callback();

packages/node/test/async/domain.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/* eslint-disable deprecation/deprecation */
2-
import { Hub, getCurrentHub, getCurrentScope, makeMain, setAsyncContextStrategy, withScope } from '@sentry/core';
2+
import type { Hub } from '@sentry/core';
3+
import { getCurrentHub, getCurrentScope, setAsyncContextStrategy, withScope } from '@sentry/core';
34
import { getIsolationScope, withIsolationScope } from '@sentry/core';
45
import type { Scope } from '@sentry/types';
56

67
import { setDomainAsyncContextStrategy } from '../../src/async/domain';
78

89
describe('setDomainAsyncContextStrategy()', () => {
910
beforeEach(() => {
10-
const hub = new Hub();
11-
12-
makeMain(hub);
11+
getCurrentScope().clear();
12+
getIsolationScope().clear();
1313
});
1414

1515
afterEach(() => {

packages/node/test/async/hooks.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/* eslint-disable deprecation/deprecation */
2+
import type { Hub } from '@sentry/core';
23
import {
3-
Hub,
44
getCurrentHub,
55
getCurrentScope,
66
getIsolationScope,
7-
makeMain,
87
setAsyncContextStrategy,
98
withIsolationScope,
109
withScope,
@@ -15,9 +14,8 @@ import { setHooksAsyncContextStrategy } from '../../src/async/hooks';
1514

1615
describe('setHooksAsyncContextStrategy()', () => {
1716
beforeEach(() => {
18-
const hub = new Hub();
19-
20-
makeMain(hub);
17+
getCurrentScope().clear();
18+
getIsolationScope().clear();
2119
});
2220

2321
afterEach(() => {

0 commit comments

Comments
 (0)