Skip to content

Commit ac278e7

Browse files
authored
feat(core): Deprecate methods on Hub (#10124)
Except for `bindClient()` which needs a separate replacement. For everything else we already have a replacement in place! Then in a follow up we can deprecate `getCurrentHub()` itself.
1 parent 281d77c commit ac278e7

File tree

88 files changed

+500
-331
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+500
-331
lines changed

dev-packages/browser-integration-tests/loader-suites/loader/noOnLoad/sdkLoadedInMeanwhile/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ sentryTest('it does not download the SDK if the SDK was loaded in the meanwhile'
5454
expect(sentryEventCount).toBe(1);
5555

5656
// Ensure loader does not overwrite init/config
57-
const options = await page.evaluate(() => (window as any).Sentry.getCurrentHub().getClient()?.getOptions());
57+
const options = await page.evaluate(() => (window as any).Sentry.getClient()?.getOptions());
5858
expect(options?.replaysSessionSampleRate).toBe(0.42);
5959

6060
expect(eventData.exception?.values?.length).toBe(1);

dev-packages/browser-integration-tests/loader-suites/loader/onLoad/customBrowserTracing/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ sentryTest('should handle custom added BrowserTracing integration', async ({ get
2727
expect(eventData.transaction_info?.source).toEqual('url');
2828

2929
const tracePropagationTargets = await page.evaluate(() => {
30-
const browserTracing = (window as any).Sentry.getCurrentHub().getClient().getIntegrationById('BrowserTracing');
30+
const browserTracing = (window as any).Sentry.getClient().getIntegrationById('BrowserTracing');
3131
return browserTracing.options.tracePropagationTargets;
3232
});
3333

dev-packages/browser-integration-tests/loader-suites/loader/onLoad/customInit/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ sentryTest('always calls onLoad init correctly', async ({ getLocalTestUrl, page
3030

3131
expect(await page.evaluate('window.__hadSentry')).toEqual(false);
3232
expect(await page.evaluate('window.__sentryOnLoad')).toEqual(1);
33-
expect(await page.evaluate('Sentry.getCurrentHub().getClient().getOptions().sampleRate')).toEqual(0.5);
33+
expect(await page.evaluate('Sentry.getClient().getOptions().sampleRate')).toEqual(0.5);
3434
});

dev-packages/browser-integration-tests/loader-suites/loader/onLoad/customIntegrations/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ sentryTest('should handle custom added integrations & default integrations', asy
2424
});
2525

2626
const hasCustomIntegration = await page.evaluate(() => {
27-
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('CustomIntegration');
27+
return !!(window as any).Sentry.getClient().getIntegrationById('CustomIntegration');
2828
});
2929

3030
const hasReplay = await page.evaluate(() => {
31-
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('Replay');
31+
return !!(window as any).Sentry.getClient().getIntegrationById('Replay');
3232
});
3333
const hasBrowserTracing = await page.evaluate(() => {
34-
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('BrowserTracing');
34+
return !!(window as any).Sentry.getClient().getIntegrationById('BrowserTracing');
3535
});
3636

3737
expect(hasCustomIntegration).toEqual(true);

dev-packages/browser-integration-tests/loader-suites/loader/onLoad/customIntegrationsFunction/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ sentryTest(
2121
});
2222

2323
const hasCustomIntegration = await page.evaluate(() => {
24-
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('CustomIntegration');
24+
return !!(window as any).Sentry.getClient().getIntegrationById('CustomIntegration');
2525
});
2626

2727
const hasReplay = await page.evaluate(() => {
28-
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('Replay');
28+
return !!(window as any).Sentry.getClient().getIntegrationById('Replay');
2929
});
3030
const hasBrowserTracing = await page.evaluate(() => {
31-
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('BrowserTracing');
31+
return !!(window as any).Sentry.getClient().getIntegrationById('BrowserTracing');
3232
});
3333

3434
expect(hasCustomIntegration).toEqual(true);

dev-packages/browser-integration-tests/loader-suites/loader/onLoad/customReplay/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ sentryTest('should handle custom added Replay integration', async ({ getLocalTes
2727
expect(eventData.segment_id).toBe(0);
2828

2929
const useCompression = await page.evaluate(() => {
30-
const replay = (window as any).Sentry.getCurrentHub().getClient().getIntegrationById('Replay');
30+
const replay = (window as any).Sentry.getClient().getIntegrationById('Replay');
3131
return replay._replay.getOptions().useCompression;
3232
});
3333

dev-packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoad/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ sentryTest('sentryOnLoad callback is called before Sentry.onLoad()', async ({ ge
1111

1212
expect(eventData.message).toBe('Test exception');
1313

14-
expect(await page.evaluate('Sentry.getCurrentHub().getClient().getOptions().tracesSampleRate')).toEqual(0.123);
14+
expect(await page.evaluate('Sentry.getClient().getOptions().tracesSampleRate')).toEqual(0.123);
1515
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Sentry.onLoad(function () {
22
// this should be called _after_ window.sentryOnLoad
3-
Sentry.captureException(`Test exception: ${Sentry.getCurrentHub().getClient().getOptions().tracesSampleRate}`);
3+
Sentry.captureException(`Test exception: ${Sentry.getClient().getOptions().tracesSampleRate}`);
44
});

dev-packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ sentryTest('sentryOnLoad callback is used', async ({ getLocalTestUrl, page }) =>
1111

1212
expect(eventData.message).toBe('Test exception: 0.123');
1313

14-
expect(await page.evaluate('Sentry.getCurrentHub().getClient().getOptions().tracesSampleRate')).toEqual(0.123);
14+
expect(await page.evaluate('Sentry.getClient().getOptions().tracesSampleRate')).toEqual(0.123);
1515
});

dev-packages/e2e-tests/test-applications/create-next-app/pages/api/success.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
66
// eslint-disable-next-line deprecation/deprecation
77
const transaction = Sentry.startTransaction({ name: 'test-transaction', op: 'e2e-test' });
88
// eslint-disable-next-line deprecation/deprecation
9-
Sentry.getCurrentHub().getScope().setSpan(transaction);
9+
Sentry.getCurrentScope().setSpan(transaction);
1010

1111
// eslint-disable-next-line deprecation/deprecation
1212
const span = transaction.startChild();

dev-packages/node-integration-tests/suites/express/sentry-trace/baggage-header-out/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ app.use(cors());
2727

2828
app.get('/test/express', (_req, res) => {
2929
// eslint-disable-next-line deprecation/deprecation
30-
const transaction = Sentry.getCurrentHub().getScope().getTransaction();
30+
const transaction = Sentry.getCurrentScope().getTransaction();
3131
if (transaction) {
3232
// eslint-disable-next-line deprecation/deprecation
3333
transaction.traceId = '86f39e84263a4de99c326acab3bfe3bd';

dev-packages/node-integration-tests/suites/express/sentry-trace/baggage-transaction-name/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ app.use(cors());
3131

3232
app.get('/test/express', (_req, res) => {
3333
// eslint-disable-next-line deprecation/deprecation
34-
const transaction = Sentry.getCurrentHub().getScope().getTransaction();
34+
const transaction = Sentry.getCurrentScope().getTransaction();
3535
if (transaction) {
3636
// eslint-disable-next-line deprecation/deprecation
3737
transaction.traceId = '86f39e84263a4de99c326acab3bfe3bd';

dev-packages/node-integration-tests/suites/sessions/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ app.use(Sentry.Handlers.requestHandler());
1414

1515
// ### Taken from manual tests ###
1616
// Hack that resets the 60s default flush interval, and replaces it with just a one second interval
17-
const flusher = (Sentry.getCurrentHub()?.getClient() as Sentry.NodeClient)['_sessionFlusher'] as SessionFlusher;
17+
const flusher = (Sentry.getClient() as Sentry.NodeClient)['_sessionFlusher'] as SessionFlusher;
1818

1919
let flusherIntervalId = flusher && flusher['_intervalId'];
2020

packages/astro/test/client/sdk.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { BrowserClient } from '@sentry/browser';
2+
import { getCurrentScope } from '@sentry/browser';
23
import * as SentryBrowser from '@sentry/browser';
3-
import { BrowserTracing, SDK_VERSION, WINDOW, getClient, getCurrentHub } from '@sentry/browser';
4+
import { BrowserTracing, SDK_VERSION, WINDOW, getClient } from '@sentry/browser';
45
import { vi } from 'vitest';
56

67
import { init } from '../../../astro/src/client/sdk';
@@ -37,7 +38,7 @@ describe('Sentry client SDK', () => {
3738
});
3839

3940
it('sets the runtime tag on the scope', () => {
40-
const currentScope = getCurrentHub().getScope();
41+
const currentScope = getCurrentScope();
4142

4243
// @ts-expect-error need access to protected _tags attribute
4344
expect(currentScope._tags).toEqual({});

packages/astro/test/server/sdk.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { getCurrentHub } from '@sentry/core';
21
import * as SentryNode from '@sentry/node';
32
import { SDK_VERSION } from '@sentry/node';
43
import { GLOBAL_OBJ } from '@sentry/utils';
@@ -38,7 +37,7 @@ describe('Sentry server SDK', () => {
3837
});
3938

4039
it('sets the runtime tag on the scope', () => {
41-
const currentScope = getCurrentHub().getScope();
40+
const currentScope = SentryNode.getCurrentScope();
4241

4342
// @ts-expect-error need access to protected _tags attribute
4443
expect(currentScope._tags).toEqual({});

packages/browser/src/sdk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export const showReportDialog: ShowReportDialogFunction = (
167167
return;
168168
}
169169

170+
// eslint-disable-next-line deprecation/deprecation
170171
const { client, scope } = hub.getStackTop();
171172
const dsn = options.dsn || (client && client.getDsn());
172173
if (!dsn) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('BrowserProfilingIntegration', () => {
5151
const client = Sentry.getClient<BrowserClient>();
5252

5353
// eslint-disable-next-line deprecation/deprecation
54-
const currentTransaction = Sentry.getCurrentHub().getScope().getTransaction();
54+
const currentTransaction = Sentry.getCurrentScope().getTransaction();
5555
expect(currentTransaction?.op).toBe('pageload');
5656
currentTransaction?.end();
5757
await client?.flush(1000);

packages/core/src/exports.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export function configureScope(callback: (scope: Scope) => void): ReturnType<Hub
9999
* @param breadcrumb The breadcrumb to record.
100100
*/
101101
export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): ReturnType<Hub['addBreadcrumb']> {
102+
// eslint-disable-next-line deprecation/deprecation
102103
getCurrentHub().addBreadcrumb(breadcrumb, hint);
103104
}
104105

@@ -109,6 +110,7 @@ export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): Re
109110
*/
110111
// eslint-disable-next-line @typescript-eslint/no-explicit-any
111112
export function setContext(name: string, context: { [key: string]: any } | null): ReturnType<Hub['setContext']> {
113+
// eslint-disable-next-line deprecation/deprecation
112114
getCurrentHub().setContext(name, context);
113115
}
114116

@@ -117,6 +119,7 @@ export function setContext(name: string, context: { [key: string]: any } | null)
117119
* @param extras Extras object to merge into current context.
118120
*/
119121
export function setExtras(extras: Extras): ReturnType<Hub['setExtras']> {
122+
// eslint-disable-next-line deprecation/deprecation
120123
getCurrentHub().setExtras(extras);
121124
}
122125

@@ -126,6 +129,7 @@ export function setExtras(extras: Extras): ReturnType<Hub['setExtras']> {
126129
* @param extra Any kind of data. This data will be normalized.
127130
*/
128131
export function setExtra(key: string, extra: Extra): ReturnType<Hub['setExtra']> {
132+
// eslint-disable-next-line deprecation/deprecation
129133
getCurrentHub().setExtra(key, extra);
130134
}
131135

@@ -134,6 +138,7 @@ export function setExtra(key: string, extra: Extra): ReturnType<Hub['setExtra']>
134138
* @param tags Tags context object to merge into current context.
135139
*/
136140
export function setTags(tags: { [key: string]: Primitive }): ReturnType<Hub['setTags']> {
141+
// eslint-disable-next-line deprecation/deprecation
137142
getCurrentHub().setTags(tags);
138143
}
139144

@@ -146,6 +151,7 @@ export function setTags(tags: { [key: string]: Primitive }): ReturnType<Hub['set
146151
* @param value Value of tag
147152
*/
148153
export function setTag(key: string, value: Primitive): ReturnType<Hub['setTag']> {
154+
// eslint-disable-next-line deprecation/deprecation
149155
getCurrentHub().setTag(key, value);
150156
}
151157

@@ -155,6 +161,7 @@ export function setTag(key: string, value: Primitive): ReturnType<Hub['setTag']>
155161
* @param user User context object to be set in the current context. Pass `null` to unset the user.
156162
*/
157163
export function setUser(user: User | null): ReturnType<Hub['setUser']> {
164+
// eslint-disable-next-line deprecation/deprecation
158165
getCurrentHub().setUser(user);
159166
}
160167

@@ -184,16 +191,20 @@ export function withScope<T>(
184191
if (rest.length === 2) {
185192
const [scope, callback] = rest;
186193
if (!scope) {
194+
// eslint-disable-next-line deprecation/deprecation
187195
return getCurrentHub().withScope(callback);
188196
}
189197

190198
const hub = getCurrentHub();
199+
// eslint-disable-next-line deprecation/deprecation
191200
return hub.withScope(() => {
201+
// eslint-disable-next-line deprecation/deprecation
192202
hub.getStackTop().scope = scope as Scope;
193203
return callback(scope as Scope);
194204
});
195205
}
196206

207+
// eslint-disable-next-line deprecation/deprecation
197208
return getCurrentHub().withScope(rest[0]);
198209
}
199210

@@ -332,20 +343,23 @@ export async function close(timeout?: number): Promise<boolean> {
332343
* @deprecated This function will be removed in the next major version of the Sentry SDK.
333344
*/
334345
export function lastEventId(): string | undefined {
346+
// eslint-disable-next-line deprecation/deprecation
335347
return getCurrentHub().lastEventId();
336348
}
337349

338350
/**
339351
* Get the currently active client.
340352
*/
341353
export function getClient<C extends Client>(): C | undefined {
354+
// eslint-disable-next-line deprecation/deprecation
342355
return getCurrentHub().getClient<C>();
343356
}
344357

345358
/**
346359
* Get the currently active scope.
347360
*/
348361
export function getCurrentScope(): Scope {
362+
// eslint-disable-next-line deprecation/deprecation
349363
return getCurrentHub().getScope();
350364
}
351365

0 commit comments

Comments
 (0)