Skip to content

Commit b27c236

Browse files
authored
feat(core): Deprecate pushScope & popScope (#9890)
This deprecates using `pushScope` / `popScope` on the hub.
1 parent 0efdb21 commit b27c236

File tree

7 files changed

+34
-31
lines changed

7 files changed

+34
-31
lines changed

MIGRATION.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ npx @sentry/migr8@latest
88

99
This will let you select which updates to run, and automatically update your code. Make sure to still review all code changes!
1010

11+
## Deprecate `pushScope` & `popScope` in favor of `withScope`
12+
13+
Instead of manually pushing/popping a scope, you should use `Sentry.withScope(callback: (scope: Scope))` instead.
14+
1115
## Deprecate `configureScope` in favor of using `getCurrentScope()`
1216

1317
Instead of updating the scope in a callback via `configureScope()`, you should access it via `getCurrentScope()` and configure it directly:

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,39 @@ jest.mock('@sentry/core', () => {
3737
});
3838

3939
describe('SentryBrowser', () => {
40-
const beforeSend = jest.fn();
40+
const beforeSend = jest.fn(event => event);
4141

42-
beforeAll(() => {
42+
beforeEach(() => {
43+
WINDOW.__SENTRY__ = { hub: undefined, logger: undefined, globalEventProcessors: [] };
4344
init({
4445
beforeSend,
4546
dsn,
4647
transport: makeSimpleTransport,
4748
});
4849
});
4950

50-
beforeEach(() => {
51-
getCurrentHub().pushScope();
52-
});
53-
5451
afterEach(() => {
55-
getCurrentHub().popScope();
56-
beforeSend.mockReset();
52+
beforeSend.mockClear();
5753
});
5854

5955
describe('getContext() / setContext()', () => {
6056
it('should store/load extra', () => {
6157
getCurrentScope().setExtra('abc', { def: [1] });
62-
expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({
58+
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({
6359
abc: { def: [1] },
6460
});
6561
});
6662

6763
it('should store/load tags', () => {
6864
getCurrentScope().setTag('abc', 'def');
69-
expect(global.__SENTRY__.hub._stack[1].scope._tags).toEqual({
65+
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({
7066
abc: 'def',
7167
});
7268
});
7369

7470
it('should store/load user', () => {
7571
getCurrentScope().setUser({ id: 'def' });
76-
expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({
72+
expect(global.__SENTRY__.hub._stack[0].scope._user).toEqual({
7773
id: 'def',
7874
});
7975
});

packages/core/src/hub.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ export class Hub implements HubInterface {
139139

140140
/**
141141
* @inheritDoc
142+
*
143+
* @deprecated Use `withScope` instead.
142144
*/
143145
public pushScope(): Scope {
144146
// We want to clone the content of prev scope
@@ -152,6 +154,8 @@ export class Hub implements HubInterface {
152154

153155
/**
154156
* @inheritDoc
157+
*
158+
* @deprecated Use `withScope` instead.
155159
*/
156160
public popScope(): boolean {
157161
if (this.getStack().length <= 1) return false;
@@ -162,10 +166,12 @@ export class Hub implements HubInterface {
162166
* @inheritDoc
163167
*/
164168
public withScope<T>(callback: (scope: Scope) => T): T {
169+
// eslint-disable-next-line deprecation/deprecation
165170
const scope = this.pushScope();
166171
try {
167172
return callback(scope);
168173
} finally {
174+
// eslint-disable-next-line deprecation/deprecation
169175
this.popScope();
170176
}
171177
}

packages/node/test/index.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { LinkedErrors, SDK_VERSION, getMainCarrier, initAndBind, runWithAsyncContext } from '@sentry/core';
22
import type { EventHint, Integration } from '@sentry/types';
3+
import { GLOBAL_OBJ } from '@sentry/utils';
34

45
import type { Event } from '../src';
56
import {
@@ -33,37 +34,33 @@ const dsn = 'https://[email protected]/4291';
3334
declare var global: any;
3435

3536
describe('SentryNode', () => {
36-
beforeAll(() => {
37+
beforeEach(() => {
38+
GLOBAL_OBJ.__SENTRY__ = { hub: undefined, logger: undefined, globalEventProcessors: [] };
3739
init({ dsn });
3840
});
3941

4042
beforeEach(() => {
4143
jest.clearAllMocks();
42-
getCurrentHub().pushScope();
43-
});
44-
45-
afterEach(() => {
46-
getCurrentHub().popScope();
4744
});
4845

4946
describe('getContext() / setContext()', () => {
5047
test('store/load extra', async () => {
5148
getCurrentScope().setExtra('abc', { def: [1] });
52-
expect(global.__SENTRY__.hub._stack[1].scope._extra).toEqual({
49+
expect(global.__SENTRY__.hub._stack[0].scope._extra).toEqual({
5350
abc: { def: [1] },
5451
});
5552
});
5653

5754
test('store/load tags', async () => {
5855
getCurrentScope().setTag('abc', 'def');
59-
expect(global.__SENTRY__.hub._stack[1].scope._tags).toEqual({
56+
expect(global.__SENTRY__.hub._stack[0].scope._tags).toEqual({
6057
abc: 'def',
6158
});
6259
});
6360

6461
test('store/load user', async () => {
6562
getCurrentScope().setUser({ id: 'def' });
66-
expect(global.__SENTRY__.hub._stack[1].scope._user).toEqual({
63+
expect(global.__SENTRY__.hub._stack[0].scope._user).toEqual({
6764
id: 'def',
6865
});
6966
});

packages/opentelemetry/test/custom/hub.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('OpenTelemetryHub', () => {
2626
it('pushScope() creates correct scope', () => {
2727
const hub = new OpenTelemetryHub();
2828

29+
// eslint-disable-next-line deprecation/deprecation
2930
const scope = hub.pushScope();
3031
expect(scope).toBeInstanceOf(OpenTelemetryScope);
3132

packages/replay/test/integration/eventProcessors.test.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getCurrentHub } from '@sentry/core';
2-
import type { Event, Hub, Scope } from '@sentry/types';
1+
import { getClient, getCurrentScope } from '@sentry/core';
2+
import type { Event } from '@sentry/types';
33

44
import { BASE_TIMESTAMP } from '..';
55
import { resetSdkMock } from '../mocks/resetSdkMock';
@@ -9,16 +9,11 @@ import { useFakeTimers } from '../utils/use-fake-timers';
99
useFakeTimers();
1010

1111
describe('Integration | eventProcessors', () => {
12-
let hub: Hub;
13-
let scope: Scope;
14-
1512
beforeEach(() => {
16-
hub = getCurrentHub();
17-
scope = hub.pushScope();
13+
getCurrentScope().clear();
1814
});
1915

2016
afterEach(() => {
21-
hub.popScope();
2217
jest.resetAllMocks();
2318
});
2419

@@ -31,7 +26,7 @@ describe('Integration | eventProcessors', () => {
3126
},
3227
});
3328

34-
const client = hub.getClient()!;
29+
const client = getClient()!;
3530

3631
jest.runAllTimers();
3732
const mockTransportSend = jest.spyOn(client.getTransport()!, 'send');
@@ -47,7 +42,7 @@ describe('Integration | eventProcessors', () => {
4742
return null;
4843
});
4944

50-
scope.addEventProcessor(handler1);
45+
getCurrentScope().addEventProcessor(handler1);
5146

5247
const TEST_EVENT = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });
5348

@@ -58,7 +53,7 @@ describe('Integration | eventProcessors', () => {
5853

5954
expect(mockTransportSend).toHaveBeenCalledTimes(1);
6055

61-
scope.addEventProcessor(handler2);
56+
getCurrentScope().addEventProcessor(handler2);
6257

6358
const TEST_EVENT2 = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });
6459

packages/types/src/hub.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface Hub {
4040
* when the operation finishes or throws.
4141
*
4242
* @returns Scope, the new cloned scope
43+
*
44+
* @deprecated Use `withScope` instead.
4345
*/
4446
pushScope(): Scope;
4547

@@ -49,6 +51,8 @@ export interface Hub {
4951
* This restores the state before the scope was pushed. All breadcrumbs and
5052
* context information added since the last call to {@link this.pushScope} are
5153
* discarded.
54+
*
55+
* @deprecated Use `withScope` instead.
5256
*/
5357
popScope(): boolean;
5458

0 commit comments

Comments
 (0)