Skip to content

Commit da468dc

Browse files
committed
feat(core): Deprecate getCurrentHub()
Instead, users should use the direct replacement of the hub method.
1 parent a1f4a3e commit da468dc

File tree

37 files changed

+82
-35
lines changed

37 files changed

+82
-35
lines changed

MIGRATION.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ npx @sentry/migr8@latest
1010
This will let you select which updates to run, and automatically update your code. Make sure to still review all code
1111
changes!
1212

13+
## Deprecate `getCurrentHub()`
14+
15+
In v8, you will no longer have a Hub, only Scopes as a concept. This also means that `getCurrentHub()` will eventually
16+
be removed.
17+
18+
Instead of `getCurrentHub()`, use the respective replacement API directly - see [Deprecate Hub](#deprecate-hub) for
19+
details.
20+
1321
## Deprecate `hub.bindClient()` and `makeMain()`
1422

1523
Instead, either directly use `initAndBind()`, or the new APIs `setCurrentClient()` and `client.init()`. See
@@ -54,7 +62,7 @@ If you are using the `Hub` right now, see the following table on how to migrate
5462
| ---------------------- | ------------------------------------------------------------------------------------ |
5563
| `new Hub()` | `withScope()`, `withIsolationScope()` or `new Scope()` |
5664
| hub.isOlderThan() | REMOVED - Was used to compare `Hub` instances, which are gonna be removed |
57-
| hub.bindClient() | A combination of `scope.setClient()` and `client.setupIntegrations()` |
65+
| hub.bindClient() | A combination of `scope.setClient()` and `client.init()` |
5866
| hub.pushScope() | `Sentry.withScope()` |
5967
| hub.popScope() | `Sentry.withScope()` |
6068
| hub.withScope() | `Sentry.withScope()` |

packages/astro/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export {
2525
// eslint-disable-next-line deprecation/deprecation
2626
getActiveTransaction,
2727
getHubFromCarrier,
28+
// eslint-disable-next-line deprecation/deprecation
2829
getCurrentHub,
2930
getClient,
3031
getCurrentScope,

packages/browser/src/exports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export {
3737
createTransport,
3838
flush,
3939
getHubFromCarrier,
40+
// eslint-disable-next-line deprecation/deprecation
4041
getCurrentHub,
4142
getClient,
4243
getCurrentScope,

packages/browser/src/sdk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ interface ShowReportDialogFunction {
159159
export const showReportDialog: ShowReportDialogFunction = (
160160
// eslint-disable-next-line deprecation/deprecation
161161
options: ReportDialogOptions = {},
162+
// eslint-disable-next-line deprecation/deprecation
162163
hub: Hub = getCurrentHub(),
163164
) => {
164165
// doesn't work without a document (React Native)

packages/browser/test/unit/profiling/hubextensions.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const patchedEncoder = (!global.window.TextEncoder && (global.window.TextEncoder
44
// @ts-expect-error patch the encoder on the window, else importing JSDOM fails (deleted in afterAll)
55
const patchedDecoder = (!global.window.TextDecoder && (global.window.TextDecoder = TextDecoder)) || true;
66

7-
import { getCurrentHub } from '@sentry/core';
7+
import { setCurrentClient } from '@sentry/core';
88
import type { Transaction } from '@sentry/types';
99
import { JSDOM } from 'jsdom';
1010

@@ -30,7 +30,6 @@ describe('BrowserProfilingIntegration', () => {
3030
// @ts-expect-error need to override global document
3131
global.location = dom.window.location;
3232

33-
const hub = getCurrentHub();
3433
const client: any = {
3534
getDsn() {
3635
return {};
@@ -47,8 +46,7 @@ describe('BrowserProfilingIntegration', () => {
4746
},
4847
};
4948

50-
// eslint-disable-next-line deprecation/deprecation
51-
hub.bindClient(client);
49+
setCurrentClient(client);
5250
});
5351

5452
// Reset back to previous values

packages/bun/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export {
4242
// eslint-disable-next-line deprecation/deprecation
4343
getActiveTransaction,
4444
getHubFromCarrier,
45+
// eslint-disable-next-line deprecation/deprecation
4546
getCurrentHub,
4647
getClient,
4748
getCurrentScope,

packages/core/src/exports.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,17 @@ export function withScope<T>(scope: ScopeInterface | undefined, callback: (scope
189189
export function withScope<T>(
190190
...rest: [callback: (scope: Scope) => T] | [scope: ScopeInterface | undefined, callback: (scope: Scope) => T]
191191
): T {
192+
// eslint-disable-next-line deprecation/deprecation
193+
const hub = getCurrentHub();
194+
192195
// If a scope is defined, we want to make this the active scope instead of the default one
193196
if (rest.length === 2) {
194197
const [scope, callback] = rest;
195198
if (!scope) {
196199
// eslint-disable-next-line deprecation/deprecation
197-
return getCurrentHub().withScope(callback);
200+
return hub.withScope(callback);
198201
}
199202

200-
const hub = getCurrentHub();
201203
// eslint-disable-next-line deprecation/deprecation
202204
return hub.withScope(() => {
203205
// eslint-disable-next-line deprecation/deprecation
@@ -207,7 +209,7 @@ export function withScope<T>(
207209
}
208210

209211
// eslint-disable-next-line deprecation/deprecation
210-
return getCurrentHub().withScope(rest[0]);
212+
return hub.withScope(rest[0]);
211213
}
212214

213215
/**

packages/core/src/hub.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,8 @@ export function makeMain(hub: Hub): Hub {
712712
* If a hub is already registered in the global carrier but this module
713713
* contains a more recent version, it replaces the registered version.
714714
* Otherwise, the currently registered hub will be returned.
715+
*
716+
* @deprecated Use the respective replacement method directly instead.
715717
*/
716718
export function getCurrentHub(): Hub {
717719
// Get main carrier (global for every environment)

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export {
3737
captureSession,
3838
} from './exports';
3939
export {
40+
// eslint-disable-next-line deprecation/deprecation
4041
getCurrentHub,
4142
getIsolationScope,
4243
getHubFromCarrier,

packages/core/src/sdk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Client, ClientOptions } from '@sentry/types';
22
import { consoleSandbox, logger } from '@sentry/utils';
33

44
import { DEBUG_BUILD } from './debug-build';
5+
import { getCurrentScope } from './exports';
56
import { getCurrentHub } from './hub';
67

78
/** A class object that can instantiate Client objects. */
@@ -29,9 +30,7 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
2930
});
3031
}
3132
}
32-
const hub = getCurrentHub();
33-
// eslint-disable-next-line deprecation/deprecation
34-
const scope = hub.getScope();
33+
const scope = getCurrentScope();
3534
scope.update(options.initialScope);
3635

3736
const client = new clientClass(options);
@@ -43,6 +42,7 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
4342
* Make the given client the current client.
4443
*/
4544
export function setCurrentClient(client: Client): void {
45+
// eslint-disable-next-line deprecation/deprecation
4646
const hub = getCurrentHub();
4747
// eslint-disable-next-line deprecation/deprecation
4848
const top = hub.getStackTop();

packages/core/src/tracing/trace.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export function trace<T>(
141141
// eslint-disable-next-line @typescript-eslint/no-empty-function
142142
afterFinish: () => void = () => {},
143143
): T {
144+
// eslint-disable-next-line deprecation/deprecation
144145
const hub = getCurrentHub();
145146
const scope = getCurrentScope();
146147
// eslint-disable-next-line deprecation/deprecation
@@ -182,6 +183,7 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span |
182183
const ctx = normalizeContext(context);
183184

184185
return withScope(context.scope, scope => {
186+
// eslint-disable-next-line deprecation/deprecation
185187
const hub = getCurrentHub();
186188
// eslint-disable-next-line deprecation/deprecation
187189
const parentSpan = scope.getSpan();
@@ -226,6 +228,7 @@ export function startSpanManual<T>(
226228
const ctx = normalizeContext(context);
227229

228230
return withScope(context.scope, scope => {
231+
// eslint-disable-next-line deprecation/deprecation
229232
const hub = getCurrentHub();
230233
// eslint-disable-next-line deprecation/deprecation
231234
const parentSpan = scope.getSpan();
@@ -266,6 +269,7 @@ export function startInactiveSpan(context: StartSpanOptions): Span | undefined {
266269
}
267270

268271
const ctx = normalizeContext(context);
272+
// eslint-disable-next-line deprecation/deprecation
269273
const hub = getCurrentHub();
270274
const parentSpan = context.scope
271275
? // eslint-disable-next-line deprecation/deprecation

packages/core/src/tracing/transaction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
5454
this._measurements = {};
5555
this._contexts = {};
5656

57+
// eslint-disable-next-line deprecation/deprecation
5758
this._hub = hub || getCurrentHub();
5859

5960
this._name = transactionContext.name || '';

packages/core/src/tracing/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getCurrentHub } from '../hub';
1010
* @deprecated You should not rely on the transaction, but just use `startSpan()` APIs instead.
1111
*/
1212
export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T | undefined {
13+
// eslint-disable-next-line deprecation/deprecation
1314
const hub = maybeHub || getCurrentHub();
1415
// eslint-disable-next-line deprecation/deprecation
1516
const scope = hub.getScope();

packages/core/test/lib/async-context.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { getCurrentHub, runWithAsyncContext } from '../../src';
33
describe('runWithAsyncContext()', () => {
44
it('without strategy hubs should be equal', () => {
55
runWithAsyncContext(() => {
6+
// eslint-disable-next-line deprecation/deprecation
67
const hub1 = getCurrentHub();
78
runWithAsyncContext(() => {
9+
// eslint-disable-next-line deprecation/deprecation
810
const hub2 = getCurrentHub();
911
expect(hub1).toBe(hub2);
1012
});

packages/core/test/lib/scope.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ describe('withActiveSpan()', () => {
528528
const client = new TestClient(options);
529529
const scope = new Scope();
530530
const hub = new Hub(client, scope);
531+
// eslint-disable-next-line deprecation/deprecation
531532
makeMain(hub);
532533
});
533534

packages/core/test/lib/sdk.test.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { captureCheckIn, getCurrentHub } from '@sentry/core';
1+
import { Hub, captureCheckIn, makeMain, setCurrentClient } from '@sentry/core';
22
import type { Client, Integration } from '@sentry/types';
33

44
import { installedIntegrations } from '../../src/integration';
@@ -39,21 +39,22 @@ describe('SDK', () => {
3939
});
4040

4141
describe('captureCheckIn', () => {
42+
afterEach(function () {
43+
const hub = new Hub();
44+
// eslint-disable-next-line deprecation/deprecation
45+
makeMain(hub);
46+
});
47+
4248
it('returns an id when client is defined', () => {
43-
const hub = getCurrentHub();
44-
jest.spyOn(hub, 'getClient').mockImplementation(() => {
45-
return {
46-
captureCheckIn: () => 'some-id-wasd-1234',
47-
} as unknown as Client;
48-
});
49+
const client = {
50+
captureCheckIn: () => 'some-id-wasd-1234',
51+
} as unknown as Client;
52+
setCurrentClient(client);
4953

5054
expect(captureCheckIn({ monitorSlug: 'gogogo', status: 'in_progress' })).toStrictEqual('some-id-wasd-1234');
5155
});
5256

5357
it('returns an id when client is undefined', () => {
54-
const hub = getCurrentHub();
55-
jest.spyOn(hub, 'getClient').mockImplementation(() => undefined);
56-
5758
expect(captureCheckIn({ monitorSlug: 'gogogo', status: 'in_progress' })).toStrictEqual(expect.any(String));
5859
});
5960
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export {
4141
// eslint-disable-next-line deprecation/deprecation
4242
getActiveTransaction,
4343
getHubFromCarrier,
44+
// eslint-disable-next-line deprecation/deprecation
4445
getCurrentHub,
4546
getClient,
4647
getCurrentScope,

packages/hub/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class Scope extends ScopeCore {}
4141
/**
4242
* @deprecated This export has moved to @sentry/core. The @sentry/hub package will be removed in v8.
4343
*/
44+
// eslint-disable-next-line deprecation/deprecation
4445
export const getCurrentHub = getCurrentHubCore;
4546

4647
/**

packages/nextjs/test/serverSdk.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ describe('Server init()', () => {
116116
});
117117

118118
it("initializes both global hub and domain hub when there's an active domain", () => {
119+
// eslint-disable-next-line deprecation/deprecation
119120
const globalHub = getCurrentHub();
120121

121122
runWithAsyncContext(() => {
123+
// eslint-disable-next-line deprecation/deprecation
122124
const globalHub2 = getCurrentHub();
123125
// If we call runWithAsyncContext before init, it executes the callback in the same context as there is no
124126
// strategy yet
@@ -131,6 +133,7 @@ describe('Server init()', () => {
131133
init({});
132134

133135
runWithAsyncContext(() => {
136+
// eslint-disable-next-line deprecation/deprecation
134137
const domainHub = getCurrentHub();
135138
// this tag should end up only in the domain hub
136139
// eslint-disable-next-line deprecation/deprecation

packages/node/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export {
4141
// eslint-disable-next-line deprecation/deprecation
4242
getActiveTransaction,
4343
getHubFromCarrier,
44+
// eslint-disable-next-line deprecation/deprecation
4445
getCurrentHub,
4546
getClient,
4647
getCurrentScope,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable deprecation/deprecation */
12
import { Hub, makeMain } from '@sentry/core';
23
import { getIsolationScope, withIsolationScope } from '@sentry/core';
34
import { getCurrentHub, runWithAsyncContext, setAsyncContextStrategy } from '@sentry/core';

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable deprecation/deprecation */
12
import {
23
Hub,
34
getCurrentHub,

packages/node/test/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ describe('SentryNode', () => {
303303
const client = new NodeClient(options);
304304

305305
runWithAsyncContext(() => {
306+
// eslint-disable-next-line deprecation/deprecation
306307
const hub = getCurrentHub();
307308
setCurrentClient(client);
308309
client.init();

packages/opentelemetry-node/src/spanprocessor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,15 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
119119
return;
120120
}
121121

122+
// eslint-disable-next-line deprecation/deprecation
122123
const hub = getCurrentHub();
123124
otelSpan.events.forEach(event => {
124125
maybeCaptureExceptionForTimedEvent(hub, event, otelSpan);
125126
});
126127

127128
if (sentrySpan instanceof Transaction) {
128129
updateTransactionWithOtelData(sentrySpan, otelSpan);
129-
sentrySpan.setHub(getCurrentHub());
130+
sentrySpan.setHub(hub);
130131
} else {
131132
updateSpanWithOtelData(sentrySpan, otelSpan);
132133
}

packages/react/src/profiler.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ function useProfiler(
225225
export { withProfiler, Profiler, useProfiler };
226226

227227
/** Grabs active transaction off scope */
228-
export function getActiveTransaction<T extends Transaction>(hub: Hub = getCurrentHub()): T | undefined {
228+
export function getActiveTransaction<T extends Transaction>(
229+
// eslint-disable-next-line deprecation/deprecation
230+
hub: Hub = getCurrentHub(),
231+
): T | undefined {
229232
if (hub) {
230233
// eslint-disable-next-line deprecation/deprecation
231234
const scope = hub.getScope();

packages/remix/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export {
2828
// eslint-disable-next-line deprecation/deprecation
2929
getActiveTransaction,
3030
getHubFromCarrier,
31+
// eslint-disable-next-line deprecation/deprecation
3132
getCurrentHub,
3233
Hub,
3334
// eslint-disable-next-line deprecation/deprecation

packages/remix/src/utils/instrumentServer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui
452452
}
453453

454454
return runWithAsyncContext(async () => {
455+
// eslint-disable-next-line deprecation/deprecation
455456
const hub = getCurrentHub();
456457
const options = getClient()?.getOptions();
457458
const scope = getCurrentScope();

packages/remix/src/utils/serverAdapters/express.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function wrapExpressRequestHandler(
5050
res.end = wrapEndMethod(res.end);
5151

5252
const request = extractRequestData(req);
53+
// eslint-disable-next-line deprecation/deprecation
5354
const hub = getCurrentHub();
5455
const options = getClient()?.getOptions();
5556
const scope = getCurrentScope();

packages/replay/test/unit/session/createSession.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Sentry from '@sentry/core';
2+
import type { Hub } from '@sentry/core';
23

34
import { WINDOW } from '../../../src/constants';
45
import { createSession } from '../../../src/session/createSession';
@@ -20,10 +21,11 @@ describe('Unit | session | createSession', () => {
2021

2122
beforeAll(() => {
2223
WINDOW.sessionStorage.clear();
23-
jest.spyOn(Sentry, 'getCurrentHub');
24-
(Sentry.getCurrentHub as jest.Mock).mockImplementation(() => ({
25-
captureEvent: captureEventMock,
26-
}));
24+
jest.spyOn(Sentry, 'getCurrentHub').mockImplementation(() => {
25+
return {
26+
captureEvent: captureEventMock,
27+
} as unknown as Hub;
28+
});
2729
});
2830

2931
afterEach(() => {

0 commit comments

Comments
 (0)