Skip to content

Commit 1e8d2b3

Browse files
authored
ref: Use getCurrentScope()/hub.getScope() instead of configureScope() (#9846)
In preparation for #9841, as we want to deprecate `configureScope`, refactor our own usage of this away. I used a new transformer of sentry-migr8 for this, which did most of the heavy lifting. I only needed to fix the block usage (which is more a stylistic issue than an actual problem). In some follow up, I'll also create a migr8 transform to refactor hub usage away, to e.g. refactor `getCurrentHub().getScope()` to `getCurrentScope()` etc. But for now this is OK I think.
1 parent 383c816 commit 1e8d2b3

File tree

55 files changed

+242
-342
lines changed

Some content is hidden

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

55 files changed

+242
-342
lines changed

packages/astro/src/client/sdk.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { BrowserOptions } from '@sentry/browser';
22
import { BrowserTracing, init as initBrowserSdk } from '@sentry/browser';
3-
import { configureScope, hasTracingEnabled } from '@sentry/core';
3+
import { getCurrentScope, hasTracingEnabled } from '@sentry/core';
44
import { addOrUpdateIntegration } from '@sentry/utils';
55

66
import { applySdkMetadata } from '../common/metadata';
@@ -20,9 +20,7 @@ export function init(options: BrowserOptions): void {
2020

2121
initBrowserSdk(options);
2222

23-
configureScope(scope => {
24-
scope.setTag('runtime', 'browser');
25-
});
23+
getCurrentScope().setTag('runtime', 'browser');
2624
}
2725

2826
function addClientIntegrations(options: BrowserOptions): void {

packages/astro/src/server/middleware.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
captureException,
3-
configureScope,
43
continueTrace,
54
getCurrentHub,
5+
getCurrentScope,
66
runWithAsyncContext,
77
startSpan,
88
} from '@sentry/node';
@@ -106,9 +106,7 @@ async function instrumentRequest(
106106
}
107107

108108
if (options.trackClientIp) {
109-
configureScope(scope => {
110-
scope.setUser({ ip_address: ctx.clientAddress });
111-
});
109+
getCurrentScope().setUser({ ip_address: ctx.clientAddress });
112110
}
113111

114112
try {

packages/astro/src/server/sdk.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { configureScope } from '@sentry/core';
1+
import { getCurrentScope } from '@sentry/core';
22
import type { NodeOptions } from '@sentry/node';
33
import { init as initNodeSdk } from '@sentry/node';
44

@@ -13,7 +13,5 @@ export function init(options: NodeOptions): void {
1313

1414
initNodeSdk(options);
1515

16-
configureScope(scope => {
17-
scope.setTag('runtime', 'node');
18-
});
16+
getCurrentScope().setTag('runtime', 'node');
1917
}

packages/astro/test/server/middleware.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ describe('sentryMiddleware', () => {
172172
it('attaches client IP and request headers if options are set', async () => {
173173
const scope = { setUser: vi.fn(), setPropagationContext: vi.fn() };
174174
// @ts-expect-error, only passing a partial Scope object
175-
const configureScopeSpy = vi.spyOn(SentryNode, 'configureScope').mockImplementation(cb => cb(scope));
175+
const getCurrentScopeSpy = vi.spyOn(SentryNode, 'getCurrentScope').mockImplementation(() => scope);
176176

177177
const middleware = handleRequest({ trackClientIp: true, trackHeaders: true });
178178
const ctx = {
@@ -192,7 +192,7 @@ describe('sentryMiddleware', () => {
192192
// @ts-expect-error, a partial ctx object is fine here
193193
await middleware(ctx, next);
194194

195-
expect(configureScopeSpy).toHaveBeenCalledTimes(1);
195+
expect(getCurrentScopeSpy).toHaveBeenCalledTimes(1);
196196
expect(scope.setUser).toHaveBeenCalledWith({ ip_address: '192.168.0.1' });
197197

198198
expect(startSpanSpy).toHaveBeenCalledWith(

packages/browser-integration-tests/suites/replay/dsc/test.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ sentryTest(
2121

2222
const transactionReq = waitForTransactionRequest(page);
2323

24+
// Wait for this to be available
25+
await page.waitForFunction('!!window.Replay');
26+
2427
await page.evaluate(() => {
2528
(window as unknown as TestWindow).Replay.start();
2629
});
2730

2831
await waitForReplayRunning(page);
2932

3033
await page.evaluate(() => {
31-
(window as unknown as TestWindow).Sentry.configureScope(scope => {
32-
scope.setUser({ id: 'user123', segment: 'segmentB' });
33-
scope.setTransactionName('testTransactionDSC');
34-
});
34+
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
35+
scope.setUser({ id: 'user123', segment: 'segmentB' });
36+
scope.setTransactionName('testTransactionDSC');
3537
});
3638

3739
const req0 = await transactionReq;
@@ -74,10 +76,9 @@ sentryTest(
7476
await waitForReplayRunning(page);
7577

7678
await page.evaluate(() => {
77-
(window as unknown as TestWindow).Sentry.configureScope(scope => {
78-
scope.setUser({ id: 'user123', segment: 'segmentB' });
79-
scope.setTransactionName('testTransactionDSC');
80-
});
79+
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
80+
scope.setUser({ id: 'user123', segment: 'segmentB' });
81+
scope.setTransactionName('testTransactionDSC');
8182
});
8283

8384
const req0 = await transactionReq;
@@ -132,10 +133,9 @@ sentryTest(
132133
});
133134

134135
await page.evaluate(() => {
135-
(window as unknown as TestWindow).Sentry.configureScope(scope => {
136-
scope.setUser({ id: 'user123', segment: 'segmentB' });
137-
scope.setTransactionName('testTransactionDSC');
138-
});
136+
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
137+
scope.setUser({ id: 'user123', segment: 'segmentB' });
138+
scope.setTransactionName('testTransactionDSC');
139139
});
140140

141141
const req0 = await transactionReq;
@@ -181,10 +181,9 @@ sentryTest(
181181
const transactionReq = waitForTransactionRequest(page);
182182

183183
await page.evaluate(async () => {
184-
(window as unknown as TestWindow).Sentry.configureScope(scope => {
185-
scope.setUser({ id: 'user123', segment: 'segmentB' });
186-
scope.setTransactionName('testTransactionDSC');
187-
});
184+
const scope = (window as unknown as TestWindow).Sentry.getCurrentScope();
185+
scope.setUser({ id: 'user123', segment: 'segmentB' });
186+
scope.setTransactionName('testTransactionDSC');
188187
});
189188

190189
const req0 = await transactionReq;

packages/browser-integration-tests/suites/tracing/envelope-header-transaction-name/init.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ Sentry.init({
1111
debug: true,
1212
});
1313

14-
Sentry.configureScope(scope => {
15-
scope.setUser({ id: 'user123', segment: 'segmentB' });
16-
scope.setTransactionName('testTransactionDSC');
17-
scope.getTransaction().setMetadata({ source: 'custom' });
18-
});
14+
const scope = Sentry.getCurrentScope();
15+
scope.setUser({ id: 'user123', segment: 'segmentB' });
16+
scope.setTransactionName('testTransactionDSC');
17+
scope.getTransaction().setMetadata({ source: 'custom' });

packages/browser-integration-tests/suites/tracing/envelope-header/init.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Sentry.init({
1111
debug: true,
1212
});
1313

14-
Sentry.configureScope(scope => {
15-
scope.setUser({ id: 'user123', segment: 'segmentB' });
16-
scope.setTransactionName('testTransactionDSC');
17-
});
14+
const scope = Sentry.getCurrentScope();
15+
scope.setUser({ id: 'user123', segment: 'segmentB' });
16+
scope.setTransactionName('testTransactionDSC');

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

+6-16
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import {
1212
captureEvent,
1313
captureException,
1414
captureMessage,
15-
configureScope,
1615
flush,
1716
getClient,
1817
getCurrentHub,
18+
getCurrentScope,
1919
init,
2020
showReportDialog,
2121
wrap,
@@ -58,27 +58,21 @@ describe('SentryBrowser', () => {
5858

5959
describe('getContext() / setContext()', () => {
6060
it('should store/load extra', () => {
61-
configureScope((scope: Scope) => {
62-
scope.setExtra('abc', { def: [1] });
63-
});
61+
getCurrentScope().setExtra('abc', { def: [1] });
6462
expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({
6563
abc: { def: [1] },
6664
});
6765
});
6866

6967
it('should store/load tags', () => {
70-
configureScope((scope: Scope) => {
71-
scope.setTag('abc', 'def');
72-
});
68+
getCurrentScope().setTag('abc', 'def');
7369
expect(global.__SENTRY__.hub._stack[1].scope._tags).toEqual({
7470
abc: 'def',
7571
});
7672
});
7773

7874
it('should store/load user', () => {
79-
configureScope((scope: Scope) => {
80-
scope.setUser({ id: 'def' });
81-
});
75+
getCurrentScope().setUser({ id: 'def' });
8276
expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({
8377
id: 'def',
8478
});
@@ -95,9 +89,7 @@ describe('SentryBrowser', () => {
9589
const options = getDefaultBrowserClientOptions({ dsn });
9690
const client = new BrowserClient(options);
9791
it('uses the user on the scope', () => {
98-
configureScope(scope => {
99-
scope.setUser(EX_USER);
100-
});
92+
getCurrentScope().setUser(EX_USER);
10193
getCurrentHub().bindClient(client);
10294

10395
showReportDialog();
@@ -110,9 +102,7 @@ describe('SentryBrowser', () => {
110102
});
111103

112104
it('prioritizes options user over scope user', () => {
113-
configureScope(scope => {
114-
scope.setUser(EX_USER);
115-
});
105+
getCurrentScope().setUser(EX_USER);
116106
getCurrentHub().bindClient(client);
117107

118108
const DIALOG_OPTION_USER = { email: '[email protected]' };

packages/core/src/tracing/idletransaction.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export class IdleTransaction extends Transaction {
121121
// We set the transaction here on the scope so error events pick up the trace
122122
// context and attach it to the error.
123123
DEBUG_BUILD && logger.log(`Setting idle transaction on scope. Span ID: ${this.spanId}`);
124-
_idleHub.configureScope(scope => scope.setSpan(this));
124+
_idleHub.getScope().setSpan(this);
125125
}
126126

127127
this._restartIdleTimeout();

packages/core/test/lib/hint.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { captureEvent, configureScope } from '@sentry/core';
1+
import { captureEvent, getCurrentScope } from '@sentry/core';
22
import { GLOBAL_OBJ } from '@sentry/utils';
33

44
import { initAndBind } from '../../src/sdk';
@@ -109,7 +109,7 @@ describe('Hint', () => {
109109
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
110110
initAndBind(TestClient, options);
111111

112-
configureScope(scope => scope.addAttachment({ filename: 'scope.file', data: 'great content!' }));
112+
getCurrentScope().addAttachment({ filename: 'scope.file', data: 'great content!' });
113113

114114
captureEvent({}, { attachments: [{ filename: 'some-file.txt', data: 'Hello' }] });
115115

packages/core/test/lib/tracing/errors.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('registerErrorHandlers()', () => {
4040
});
4141

4242
afterEach(() => {
43-
hub.configureScope(scope => scope.setSpan(undefined));
43+
hub.getScope().setSpan(undefined);
4444
});
4545

4646
it('registers error instrumentation', () => {
@@ -67,7 +67,7 @@ describe('registerErrorHandlers()', () => {
6767
it('sets status for transaction on scope on error', () => {
6868
registerErrorInstrumentation();
6969
const transaction = hub.startTransaction({ name: 'test' });
70-
hub.configureScope(scope => scope.setSpan(transaction));
70+
hub.getScope().setSpan(transaction);
7171

7272
mockErrorCallback({} as HandlerDataError);
7373
expect(transaction.status).toBe('internal_error');
@@ -78,7 +78,7 @@ describe('registerErrorHandlers()', () => {
7878
it('sets status for transaction on scope on unhandledrejection', () => {
7979
registerErrorInstrumentation();
8080
const transaction = hub.startTransaction({ name: 'test' });
81-
hub.configureScope(scope => scope.setSpan(transaction));
81+
hub.getScope().setSpan(transaction);
8282

8383
mockUnhandledRejectionCallback({});
8484
expect(transaction.status).toBe('internal_error');

packages/core/test/mocks/integration.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Event, EventProcessor, Integration } from '@sentry/types';
22

3-
import { configureScope, getCurrentHub } from '../../src';
3+
import { getCurrentHub, getCurrentScope } from '../../src';
44

55
export class TestIntegration implements Integration {
66
public static id: string = 'TestIntegration';
@@ -18,9 +18,7 @@ export class TestIntegration implements Integration {
1818

1919
eventProcessor.id = this.name;
2020

21-
configureScope(scope => {
22-
scope.addEventProcessor(eventProcessor);
23-
});
21+
getCurrentScope().addEventProcessor(eventProcessor);
2422
}
2523
}
2624

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
44

55
export default function handler(req: NextApiRequest, res: NextApiResponse) {
66
const transaction = Sentry.startTransaction({ name: 'test-transaction', op: 'e2e-test' });
7-
Sentry.getCurrentHub().configureScope(scope => scope.setSpan(transaction));
7+
Sentry.getCurrentHub().getScope().setSpan(transaction);
88

99
const span = transaction.startChild();
1010

packages/e2e-tests/test-applications/node-express-app/src/app.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ app.get('/test-param/:param', function (req, res) {
3535

3636
app.get('/test-transaction', async function (req, res) {
3737
const transaction = Sentry.startTransaction({ name: 'test-transaction', op: 'e2e-test' });
38-
Sentry.getCurrentHub().configureScope(scope => scope.setSpan(transaction));
38+
Sentry.getCurrentHub().getScope().setSpan(transaction);
3939

4040
const span = transaction.startChild();
4141

packages/nextjs/src/client/index.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type { BrowserOptions } from '@sentry/react';
44
import {
55
BrowserTracing,
66
Integrations,
7-
configureScope,
87
defaultRequestInstrumentationOptions,
8+
getCurrentScope,
99
init as reactInit,
1010
} from '@sentry/react';
1111
import type { EventProcessor } from '@sentry/types';
@@ -56,17 +56,16 @@ export function init(options: BrowserOptions): void {
5656

5757
reactInit(opts);
5858

59-
configureScope(scope => {
60-
scope.setTag('runtime', 'browser');
61-
const filterTransactions: EventProcessor = event =>
62-
event.type === 'transaction' && event.transaction === '/404' ? null : event;
63-
filterTransactions.id = 'NextClient404Filter';
64-
scope.addEventProcessor(filterTransactions);
59+
const scope = getCurrentScope();
60+
scope.setTag('runtime', 'browser');
61+
const filterTransactions: EventProcessor = event =>
62+
event.type === 'transaction' && event.transaction === '/404' ? null : event;
63+
filterTransactions.id = 'NextClient404Filter';
64+
scope.addEventProcessor(filterTransactions);
6565

66-
if (process.env.NODE_ENV === 'development') {
67-
scope.addEventProcessor(devErrorSymbolicationEventProcessor);
68-
}
69-
});
66+
if (process.env.NODE_ENV === 'development') {
67+
scope.addEventProcessor(devErrorSymbolicationEventProcessor);
68+
}
7069
}
7170

7271
function addClientIntegrations(options: BrowserOptions): void {

0 commit comments

Comments
 (0)