Skip to content

feat: Remove hub from global, hub.run & hub utilities #10718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export {
captureCheckIn,
withMonitor,
createTransport,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
Expand Down
1 change: 0 additions & 1 deletion packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export {
close,
createTransport,
flush,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
Expand Down
16 changes: 8 additions & 8 deletions packages/browser/test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ describe('SentryBrowser', () => {
describe('getContext() / setContext()', () => {
it('should store/load extra', () => {
getCurrentScope().setExtra('abc', { def: [1] });
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({
expect(getCurrentScope().getScopeData().extra).toEqual({
abc: { def: [1] },
});
});

it('should store/load tags', () => {
getCurrentScope().setTag('abc', 'def');
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({
expect(getCurrentScope().getScopeData().tags).toEqual({
abc: 'def',
});
});

it('should store/load user', () => {
getCurrentScope().setUser({ id: 'def' });
expect(global.__SENTRY__.hub._stack[0].scope._user).toEqual({
expect(getCurrentScope().getScopeData().user).toEqual({
id: 'def',
});
});
Expand Down Expand Up @@ -294,20 +294,20 @@ describe('SentryBrowser initialization', () => {
it('should use window.SENTRY_RELEASE to set release on initialization if available', () => {
global.SENTRY_RELEASE = { id: 'foobar' };
init({ dsn });
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBe('foobar');
expect(getClient()?.getOptions().release).toBe('foobar');
delete global.SENTRY_RELEASE;
});

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

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

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

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

describe('SDK metadata', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export {
flush,
// eslint-disable-next-line deprecation/deprecation
getActiveTransaction,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/asyncContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export interface Carrier {
}

interface SentryCarrier {
hub?: Hub;
acs?: AsyncContextStrategy;
/**
* Extra Hub properties injected by various SDKs
Expand Down Expand Up @@ -96,7 +95,6 @@ export function getSentryCarrier(carrier: Carrier): SentryCarrier {
if (!carrier.__SENTRY__) {
carrier.__SENTRY__ = {
extensions: {},
hub: undefined,
};
}
return carrier.__SENTRY__;
Expand Down
94 changes: 7 additions & 87 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,20 +421,6 @@ export class Hub implements HubInterface {
this.getIsolationScope().setContext(name, context);
}

/**
* @inheritDoc
*/
public run(callback: (hub: Hub) => void): void {
// eslint-disable-next-line deprecation/deprecation
const oldHub = makeMain(this);
try {
callback(this);
} finally {
// eslint-disable-next-line deprecation/deprecation
makeMain(oldHub);
}
}

/**
* @inheritDoc
* @deprecated Use `Sentry.getClient().getIntegrationByName()` instead.
Expand Down Expand Up @@ -618,23 +604,8 @@ Sentry.init({...});
* @deprecated Use `setCurrentClient()` instead.
*/
export function makeMain(hub: HubInterface): HubInterface {
const registry = getMainCarrier();
const oldHub = getHubFromCarrier(registry);
setHubOnCarrier(registry, hub);
return oldHub;
}

/**
* This will set passed {@link Hub} on the passed object's __SENTRY__.hub attribute
* @param carrier object
* @param hub Hub
* @returns A boolean indicating success or failure
*/
export function setHubOnCarrier(carrier: Carrier, hub: HubInterface): boolean {
if (!carrier) return false;
const sentry = getSentryCarrier(carrier);
sentry.hub = hub;
return true;
// noop!
return hub;
}

/**
Expand Down Expand Up @@ -681,69 +652,18 @@ export function getDefaultIsolationScope(): Scope {
*/
export function getGlobalHub(): HubInterface {
const registry = getMainCarrier();
const sentry = getSentryCarrier(registry) as { hub?: HubInterface };

// If there's no hub, or its an old API, assign a new one

if (
!hasHubOnCarrier(registry) ||
// eslint-disable-next-line deprecation/deprecation
getHubFromCarrier(registry).isOlderThan(API_VERSION)
) {
// eslint-disable-next-line deprecation/deprecation
setHubOnCarrier(registry, new Hub(undefined, getDefaultCurrentScope(), getDefaultIsolationScope()));
}

// Return hub that lives on a global object
return getHubFromCarrier(registry);
}

/**
* This will tell whether a carrier has a hub on it or not
* @param carrier object
*/
function hasHubOnCarrier(carrier: Carrier): boolean {
return !!getSentryCarrier(carrier).hub;
}

/**
* This will create a new {@link Hub} and add to the passed object on
* __SENTRY__.hub.
* @param carrier object
* @hidden
*/
export function getHubFromCarrier(carrier: Carrier): HubInterface {
const sentry = getSentryCarrier(carrier);
if (!sentry.hub) {
// eslint-disable-next-line deprecation/deprecation
sentry.hub = new Hub();
if (sentry.hub) {
return sentry.hub;
}

// eslint-disable-next-line deprecation/deprecation
sentry.hub = new Hub(undefined, getDefaultCurrentScope(), getDefaultIsolationScope());
return sentry.hub;
}

/**
* @private Private API with no semver guarantees!
*
* If the carrier does not contain a hub, a new hub is created with the global hub client and scope.
*/
export function ensureHubOnCarrier(carrier: Carrier, parent: HubInterface = getGlobalHub()): void {
// If there's no hub on current domain, or it's an old API, assign a new one
if (
!hasHubOnCarrier(carrier) ||
// eslint-disable-next-line deprecation/deprecation
getHubFromCarrier(carrier).isOlderThan(API_VERSION)
) {
// eslint-disable-next-line deprecation/deprecation
const client = parent.getClient();
// eslint-disable-next-line deprecation/deprecation
const scope = parent.getScope();
// eslint-disable-next-line deprecation/deprecation
const isolationScope = parent.getIsolationScope();
// eslint-disable-next-line deprecation/deprecation
setHubOnCarrier(carrier, new Hub(client, scope.clone() as Scope, isolationScope.clone() as Scope));
}
}

/**
* Get the current async context strategy.
* If none has been setup, the default will be used.
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ export {
export {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getHubFromCarrier,
Hub,
// eslint-disable-next-line deprecation/deprecation
makeMain,
setHubOnCarrier,
ensureHubOnCarrier,
getGlobalHub,
getDefaultCurrentScope,
getDefaultIsolationScope,
Expand Down
40 changes: 0 additions & 40 deletions packages/core/test/lib/global.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export {
flush,
// eslint-disable-next-line deprecation/deprecation
getActiveTransaction,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
Expand Down
5 changes: 0 additions & 5 deletions packages/node-experimental/src/sdk/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ export function getCurrentHub(): Hub {
setExtras,
setContext,

run(callback: (hub: Hub) => void): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return withScope(() => callback(this as any));
},

getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null {
// eslint-disable-next-line deprecation/deprecation
return getClient().getIntegration(integration);
Expand Down
1 change: 0 additions & 1 deletion packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export {
flush,
// eslint-disable-next-line deprecation/deprecation
getActiveTransaction,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
Expand Down
3 changes: 0 additions & 3 deletions packages/node/test/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('requestHandler', () => {
const sentry = getMainCarrier().__SENTRY__;
if (sentry) {
sentry.acs = undefined;
sentry.hub = undefined;
}
});

Expand Down Expand Up @@ -205,7 +204,6 @@ describe('tracingHandler', () => {
const sentry = getMainCarrier().__SENTRY__;
if (sentry) {
sentry.acs = undefined;
sentry.hub = undefined;
}
});

Expand Down Expand Up @@ -524,7 +522,6 @@ describe('errorHandler()', () => {
const sentry = getMainCarrier().__SENTRY__;
if (sentry) {
sentry.acs = undefined;
sentry.hub = undefined;
}
});

Expand Down
9 changes: 5 additions & 4 deletions packages/node/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,22 @@ describe('SentryNode', () => {
describe('getContext() / setContext()', () => {
test('store/load extra', async () => {
getCurrentScope().setExtra('abc', { def: [1] });
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({

expect(getCurrentScope().getScopeData().extra).toEqual({
abc: { def: [1] },
});
});

test('store/load tags', async () => {
getCurrentScope().setTag('abc', 'def');
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({
expect(getCurrentScope().getScopeData().tags).toEqual({
abc: 'def',
});
});

test('store/load user', async () => {
getCurrentScope().setUser({ id: 'def' });
expect(global.__SENTRY__.hub._stack[0].scope._user).toEqual({
expect(getCurrentScope().getScopeData().user).toEqual({
id: 'def',
});
});
Expand Down Expand Up @@ -384,7 +385,7 @@ describe('SentryNode initialization', () => {
test('global.SENTRY_RELEASE is used to set release on initialization if available', () => {
global.SENTRY_RELEASE = { id: 'foobar' };
init({ dsn });
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toEqual('foobar');
expect(getClient()?.getOptions().release).toEqual('foobar');
// Unsure if this is needed under jest.
global.SENTRY_RELEASE = undefined;
});
Expand Down
1 change: 0 additions & 1 deletion packages/node/test/integrations/undici.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ beforeEach(() => {
const sentry = getMainCarrier().__SENTRY__;
if (sentry) {
sentry.acs = undefined;
sentry.hub = undefined;
}

getCurrentScope().clear();
Expand Down
5 changes: 0 additions & 5 deletions packages/opentelemetry/src/custom/getCurrentHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ export function getCurrentHub(): Hub {
setExtras,
setContext,

run(callback: (hub: Hub) => void): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return withScope(() => callback(this as any));
},

getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null {
// eslint-disable-next-line deprecation/deprecation
return getClient()?.getIntegration(integration) || null;
Expand Down
1 change: 0 additions & 1 deletion packages/remix/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export {
createTransport,
// eslint-disable-next-line deprecation/deprecation
getActiveTransaction,
getHubFromCarrier,
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
Expand Down
Loading