Skip to content

Commit 7362857

Browse files
authored
feat: Remove hub from global, hub.run & hub utilities (#10718)
This removes `hub` on the global, as well as `getHubFromCarrier`, `hub.run()`, and some hub utilities: `getHubFromCarrier`, `ensureHubOnCarrier` and `setHubOnCarrier`. note that `makeMain` remains as a noop for now to not break vite-plugin v0.6.0 😿 We can remove this eventually.
1 parent 041c484 commit 7362857

File tree

20 files changed

+20
-176
lines changed

20 files changed

+20
-176
lines changed

packages/astro/src/index.server.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export {
1616
captureCheckIn,
1717
withMonitor,
1818
createTransport,
19-
getHubFromCarrier,
2019
// eslint-disable-next-line deprecation/deprecation
2120
getCurrentHub,
2221
getClient,

packages/browser/src/exports.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export {
3131
close,
3232
createTransport,
3333
flush,
34-
getHubFromCarrier,
3534
// eslint-disable-next-line deprecation/deprecation
3635
getCurrentHub,
3736
getClient,

packages/browser/test/unit/index.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,21 @@ describe('SentryBrowser', () => {
5858
describe('getContext() / setContext()', () => {
5959
it('should store/load extra', () => {
6060
getCurrentScope().setExtra('abc', { def: [1] });
61-
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({
61+
expect(getCurrentScope().getScopeData().extra).toEqual({
6262
abc: { def: [1] },
6363
});
6464
});
6565

6666
it('should store/load tags', () => {
6767
getCurrentScope().setTag('abc', 'def');
68-
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({
68+
expect(getCurrentScope().getScopeData().tags).toEqual({
6969
abc: 'def',
7070
});
7171
});
7272

7373
it('should store/load user', () => {
7474
getCurrentScope().setUser({ id: 'def' });
75-
expect(global.__SENTRY__.hub._stack[0].scope._user).toEqual({
75+
expect(getCurrentScope().getScopeData().user).toEqual({
7676
id: 'def',
7777
});
7878
});
@@ -294,20 +294,20 @@ describe('SentryBrowser initialization', () => {
294294
it('should use window.SENTRY_RELEASE to set release on initialization if available', () => {
295295
global.SENTRY_RELEASE = { id: 'foobar' };
296296
init({ dsn });
297-
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBe('foobar');
297+
expect(getClient()?.getOptions().release).toBe('foobar');
298298
delete global.SENTRY_RELEASE;
299299
});
300300

301301
it('should use initialScope', () => {
302302
init({ dsn, initialScope: { tags: { a: 'b' } } });
303-
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' });
303+
expect(getCurrentScope().getScopeData().tags).toEqual({ a: 'b' });
304304
});
305305

306306
it('should use initialScope Scope', () => {
307307
const scope = new Scope();
308308
scope.setTags({ a: 'b' });
309309
init({ dsn, initialScope: scope });
310-
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' });
310+
expect(getCurrentScope().getScopeData().tags).toEqual({ a: 'b' });
311311
});
312312

313313
it('should use initialScope callback', () => {
@@ -318,13 +318,13 @@ describe('SentryBrowser initialization', () => {
318318
return scope;
319319
},
320320
});
321-
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({ a: 'b' });
321+
expect(getCurrentScope().getScopeData().tags).toEqual({ a: 'b' });
322322
});
323323

324324
it('should have initialization proceed as normal if window.SENTRY_RELEASE is not set', () => {
325325
// This is mostly a happy-path test to ensure that the initialization doesn't throw an error.
326326
init({ dsn });
327-
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBeUndefined();
327+
expect(getClient()?.getOptions().release).toBeUndefined();
328328
});
329329

330330
describe('SDK metadata', () => {

packages/bun/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export {
3535
flush,
3636
// eslint-disable-next-line deprecation/deprecation
3737
getActiveTransaction,
38-
getHubFromCarrier,
3938
// eslint-disable-next-line deprecation/deprecation
4039
getCurrentHub,
4140
getClient,

packages/core/src/asyncContext.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export interface Carrier {
5353
}
5454

5555
interface SentryCarrier {
56-
hub?: Hub;
5756
acs?: AsyncContextStrategy;
5857
/**
5958
* Extra Hub properties injected by various SDKs
@@ -96,7 +95,6 @@ export function getSentryCarrier(carrier: Carrier): SentryCarrier {
9695
if (!carrier.__SENTRY__) {
9796
carrier.__SENTRY__ = {
9897
extensions: {},
99-
hub: undefined,
10098
};
10199
}
102100
return carrier.__SENTRY__;

packages/core/src/hub.ts

Lines changed: 7 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -421,20 +421,6 @@ export class Hub implements HubInterface {
421421
this.getIsolationScope().setContext(name, context);
422422
}
423423

424-
/**
425-
* @inheritDoc
426-
*/
427-
public run(callback: (hub: Hub) => void): void {
428-
// eslint-disable-next-line deprecation/deprecation
429-
const oldHub = makeMain(this);
430-
try {
431-
callback(this);
432-
} finally {
433-
// eslint-disable-next-line deprecation/deprecation
434-
makeMain(oldHub);
435-
}
436-
}
437-
438424
/**
439425
* @inheritDoc
440426
* @deprecated Use `Sentry.getClient().getIntegrationByName()` instead.
@@ -618,23 +604,8 @@ Sentry.init({...});
618604
* @deprecated Use `setCurrentClient()` instead.
619605
*/
620606
export function makeMain(hub: HubInterface): HubInterface {
621-
const registry = getMainCarrier();
622-
const oldHub = getHubFromCarrier(registry);
623-
setHubOnCarrier(registry, hub);
624-
return oldHub;
625-
}
626-
627-
/**
628-
* This will set passed {@link Hub} on the passed object's __SENTRY__.hub attribute
629-
* @param carrier object
630-
* @param hub Hub
631-
* @returns A boolean indicating success or failure
632-
*/
633-
export function setHubOnCarrier(carrier: Carrier, hub: HubInterface): boolean {
634-
if (!carrier) return false;
635-
const sentry = getSentryCarrier(carrier);
636-
sentry.hub = hub;
637-
return true;
607+
// noop!
608+
return hub;
638609
}
639610

640611
/**
@@ -681,69 +652,18 @@ export function getDefaultIsolationScope(): Scope {
681652
*/
682653
export function getGlobalHub(): HubInterface {
683654
const registry = getMainCarrier();
655+
const sentry = getSentryCarrier(registry) as { hub?: HubInterface };
684656

685657
// If there's no hub, or its an old API, assign a new one
686-
687-
if (
688-
!hasHubOnCarrier(registry) ||
689-
// eslint-disable-next-line deprecation/deprecation
690-
getHubFromCarrier(registry).isOlderThan(API_VERSION)
691-
) {
692-
// eslint-disable-next-line deprecation/deprecation
693-
setHubOnCarrier(registry, new Hub(undefined, getDefaultCurrentScope(), getDefaultIsolationScope()));
694-
}
695-
696-
// Return hub that lives on a global object
697-
return getHubFromCarrier(registry);
698-
}
699-
700-
/**
701-
* This will tell whether a carrier has a hub on it or not
702-
* @param carrier object
703-
*/
704-
function hasHubOnCarrier(carrier: Carrier): boolean {
705-
return !!getSentryCarrier(carrier).hub;
706-
}
707-
708-
/**
709-
* This will create a new {@link Hub} and add to the passed object on
710-
* __SENTRY__.hub.
711-
* @param carrier object
712-
* @hidden
713-
*/
714-
export function getHubFromCarrier(carrier: Carrier): HubInterface {
715-
const sentry = getSentryCarrier(carrier);
716-
if (!sentry.hub) {
717-
// eslint-disable-next-line deprecation/deprecation
718-
sentry.hub = new Hub();
658+
if (sentry.hub) {
659+
return sentry.hub;
719660
}
720661

662+
// eslint-disable-next-line deprecation/deprecation
663+
sentry.hub = new Hub(undefined, getDefaultCurrentScope(), getDefaultIsolationScope());
721664
return sentry.hub;
722665
}
723666

724-
/**
725-
* @private Private API with no semver guarantees!
726-
*
727-
* If the carrier does not contain a hub, a new hub is created with the global hub client and scope.
728-
*/
729-
export function ensureHubOnCarrier(carrier: Carrier, parent: HubInterface = getGlobalHub()): void {
730-
// If there's no hub on current domain, or it's an old API, assign a new one
731-
if (
732-
!hasHubOnCarrier(carrier) ||
733-
// eslint-disable-next-line deprecation/deprecation
734-
getHubFromCarrier(carrier).isOlderThan(API_VERSION)
735-
) {
736-
// eslint-disable-next-line deprecation/deprecation
737-
const client = parent.getClient();
738-
// eslint-disable-next-line deprecation/deprecation
739-
const scope = parent.getScope();
740-
// eslint-disable-next-line deprecation/deprecation
741-
const isolationScope = parent.getIsolationScope();
742-
// eslint-disable-next-line deprecation/deprecation
743-
setHubOnCarrier(carrier, new Hub(client, scope.clone() as Scope, isolationScope.clone() as Scope));
744-
}
745-
}
746-
747667
/**
748668
* Get the current async context strategy.
749669
* If none has been setup, the default will be used.

packages/core/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@ export {
3535
export {
3636
// eslint-disable-next-line deprecation/deprecation
3737
getCurrentHub,
38-
getHubFromCarrier,
3938
Hub,
4039
// eslint-disable-next-line deprecation/deprecation
4140
makeMain,
42-
setHubOnCarrier,
43-
ensureHubOnCarrier,
4441
getGlobalHub,
4542
getDefaultCurrentScope,
4643
getDefaultIsolationScope,

packages/core/test/lib/global.test.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

packages/deno/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export {
3434
flush,
3535
// eslint-disable-next-line deprecation/deprecation
3636
getActiveTransaction,
37-
getHubFromCarrier,
3837
// eslint-disable-next-line deprecation/deprecation
3938
getCurrentHub,
4039
getClient,

packages/node-experimental/src/sdk/hub.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ export function getCurrentHub(): Hub {
7272
setExtras,
7373
setContext,
7474

75-
run(callback: (hub: Hub) => void): void {
76-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
77-
return withScope(() => callback(this as any));
78-
},
79-
8075
getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null {
8176
// eslint-disable-next-line deprecation/deprecation
8277
return getClient().getIntegration(integration);

packages/node/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export {
3434
flush,
3535
// eslint-disable-next-line deprecation/deprecation
3636
getActiveTransaction,
37-
getHubFromCarrier,
3837
// eslint-disable-next-line deprecation/deprecation
3938
getCurrentHub,
4039
getClient,

packages/node/test/handlers.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ describe('requestHandler', () => {
2828
const sentry = getMainCarrier().__SENTRY__;
2929
if (sentry) {
3030
sentry.acs = undefined;
31-
sentry.hub = undefined;
3231
}
3332
});
3433

@@ -205,7 +204,6 @@ describe('tracingHandler', () => {
205204
const sentry = getMainCarrier().__SENTRY__;
206205
if (sentry) {
207206
sentry.acs = undefined;
208-
sentry.hub = undefined;
209207
}
210208
});
211209

@@ -524,7 +522,6 @@ describe('errorHandler()', () => {
524522
const sentry = getMainCarrier().__SENTRY__;
525523
if (sentry) {
526524
sentry.acs = undefined;
527-
sentry.hub = undefined;
528525
}
529526
});
530527

packages/node/test/index.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,22 @@ describe('SentryNode', () => {
5656
describe('getContext() / setContext()', () => {
5757
test('store/load extra', async () => {
5858
getCurrentScope().setExtra('abc', { def: [1] });
59-
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({
59+
60+
expect(getCurrentScope().getScopeData().extra).toEqual({
6061
abc: { def: [1] },
6162
});
6263
});
6364

6465
test('store/load tags', async () => {
6566
getCurrentScope().setTag('abc', 'def');
66-
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({
67+
expect(getCurrentScope().getScopeData().tags).toEqual({
6768
abc: 'def',
6869
});
6970
});
7071

7172
test('store/load user', async () => {
7273
getCurrentScope().setUser({ id: 'def' });
73-
expect(global.__SENTRY__.hub._stack[0].scope._user).toEqual({
74+
expect(getCurrentScope().getScopeData().user).toEqual({
7475
id: 'def',
7576
});
7677
});
@@ -384,7 +385,7 @@ describe('SentryNode initialization', () => {
384385
test('global.SENTRY_RELEASE is used to set release on initialization if available', () => {
385386
global.SENTRY_RELEASE = { id: 'foobar' };
386387
init({ dsn });
387-
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toEqual('foobar');
388+
expect(getClient()?.getOptions().release).toEqual('foobar');
388389
// Unsure if this is needed under jest.
389390
global.SENTRY_RELEASE = undefined;
390391
});

packages/node/test/integrations/undici.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ beforeEach(() => {
4444
const sentry = getMainCarrier().__SENTRY__;
4545
if (sentry) {
4646
sentry.acs = undefined;
47-
sentry.hub = undefined;
4847
}
4948

5049
getCurrentScope().clear();

packages/opentelemetry/src/custom/getCurrentHub.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ export function getCurrentHub(): Hub {
7171
setExtras,
7272
setContext,
7373

74-
run(callback: (hub: Hub) => void): void {
75-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
76-
return withScope(() => callback(this as any));
77-
},
78-
7974
getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null {
8075
// eslint-disable-next-line deprecation/deprecation
8176
return getClient()?.getIntegration(integration) || null;

packages/remix/src/index.server.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export {
2323
createTransport,
2424
// eslint-disable-next-line deprecation/deprecation
2525
getActiveTransaction,
26-
getHubFromCarrier,
2726
// eslint-disable-next-line deprecation/deprecation
2827
getCurrentHub,
2928
getClient,

0 commit comments

Comments
 (0)