Skip to content

Commit 94ae54c

Browse files
committed
ref: avoid globals.ts for scope
1 parent 783c49f commit 94ae54c

File tree

9 files changed

+40
-54
lines changed

9 files changed

+40
-54
lines changed

packages/core/src/globals.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/core/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ export {
4242
} from './hub';
4343
export { makeSession, closeSession, updateSession } from './session';
4444
export { SessionFlusher } from './sessionflusher';
45-
export { Scope, getGlobalScope } from './scope';
46-
export { clearGlobalData, getGlobalData } from './globals';
45+
export { Scope, getGlobalScope, setGlobalScope } from './scope';
4746
export {
4847
notifyEventProcessors,
4948
// eslint-disable-next-line deprecation/deprecation

packages/core/src/scope.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@ import type {
2626
import { dateTimestampInSeconds, isPlainObject, uuid4 } from '@sentry/utils';
2727

2828
import { getGlobalEventProcessors, notifyEventProcessors } from './eventProcessors';
29-
import { getGlobalData } from './globals';
3029
import { updateSession } from './session';
31-
import { applyScopeDataToEvent, mergeScopeData } from './utils/applyScopeDataToEvent';
30+
import { applyScopeDataToEvent } from './utils/applyScopeDataToEvent';
3231

3332
/**
3433
* Default value for maximum number of breadcrumbs added to an event.
3534
*/
3635
const DEFAULT_MAX_BREADCRUMBS = 100;
3736

37+
/**
38+
* The global scope is kept in this module.
39+
* When accessing this via `getGlobalScope()` we'll make sure to set one if none is currently present.
40+
*/
41+
let globalScope: ScopeInterface | undefined;
42+
3843
/**
3944
* Holds additional event information. {@link Scope.applyToEvent} will be
4045
* called by the client before an event will be sent.
@@ -579,12 +584,19 @@ export class Scope implements ScopeInterface {
579584
* This scope is applied to _all_ events.
580585
*/
581586
export function getGlobalScope(): ScopeInterface {
582-
const globalData = getGlobalData();
583-
if (!globalData.globalScope) {
584-
globalData.globalScope = new Scope();
587+
if (!globalScope) {
588+
globalScope = new Scope();
585589
}
586590

587-
return globalData.globalScope;
591+
return globalScope;
592+
}
593+
594+
/**
595+
* This is mainly needed for tests.
596+
* @hidden
597+
*/
598+
export function setGlobalScope(scope: ScopeInterface | undefined): void {
599+
globalScope = scope;
588600
}
589601

590602
function generatePropagationContext(): PropagationContext {

packages/core/test/lib/base.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Client, Envelope, Event, Span, Transaction } from '@sentry/types';
22
import { SentryError, SyncPromise, dsnToString, logger } from '@sentry/utils';
33

4-
import { Hub, Scope, clearGlobalData, makeSession } from '../../src';
4+
import { Hub, Scope, makeSession, setGlobalScope } from '../../src';
55
import * as integrationModule from '../../src/integration';
66
import { TestClient, getDefaultTestClientOptions } from '../mocks/client';
77
import { AdHocIntegration, TestIntegration } from '../mocks/integration';
@@ -54,7 +54,7 @@ describe('BaseClient', () => {
5454
beforeEach(() => {
5555
TestClient.sendEventCalled = undefined;
5656
TestClient.instance = undefined;
57-
clearGlobalData();
57+
setGlobalScope(undefined);
5858
});
5959

6060
afterEach(() => {

packages/core/test/lib/prepareEvent.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
ScopeContext,
1010
} from '@sentry/types';
1111
import { GLOBAL_OBJ, createStackParser } from '@sentry/utils';
12-
import { clearGlobalData } from '../../src';
12+
import { setGlobalScope } from '../../src';
1313

1414
import { Scope, getGlobalScope } from '../../src/scope';
1515
import {
@@ -191,7 +191,7 @@ describe('parseEventHintOrCaptureContext', () => {
191191

192192
describe('prepareEvent', () => {
193193
beforeEach(() => {
194-
clearGlobalData();
194+
setGlobalScope(undefined);
195195
});
196196

197197
it('works without any scope data', async () => {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { applyScopeDataToEvent } from '@sentry/core';
2-
import type { Attachment, Breadcrumb, EventProcessor } from '@sentry/types';
3-
import { clearGlobalData } from '../../src/globals';
4-
import { Scope, getGlobalScope } from '../../src/scope';
1+
import type { Attachment, Breadcrumb } from '@sentry/types';
2+
import { applyScopeDataToEvent } from '../../src';
3+
import { Scope, getGlobalScope, setGlobalScope } from '../../src/scope';
54

65
describe('Unit | Scope', () => {
76
beforeEach(() => {
8-
clearGlobalData();
7+
setGlobalScope(undefined);
98
});
109

1110
it('allows to create & update a scope', () => {
@@ -91,7 +90,7 @@ describe('Unit | Scope', () => {
9190

9291
describe('global scope', () => {
9392
beforeEach(() => {
94-
clearGlobalData();
93+
setGlobalScope(undefined);
9594
});
9695

9796
it('works', () => {

packages/node-experimental/src/otel/asyncContextStrategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as api from '@opentelemetry/api';
22

3-
import { setAsyncContextStrategy } from './../sdk/globals';
3+
import { setAsyncContextStrategy } from '../sdk/globals';
44
import { getCurrentHub } from './../sdk/hub';
55
import type { CurrentScopes } from './../sdk/types';
66
import { getScopesFromContext } from './../utils/contextData';

packages/node-experimental/src/sdk/scope.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getGlobalData, mergeScopeData } from '@sentry/core';
1+
import { getGlobalScope as _getGlobalScope, mergeScopeData, setGlobalScope } from '@sentry/core';
22
import { OpenTelemetryScope } from '@sentry/opentelemetry';
33
import type { Breadcrumb, Client, Event, EventHint, Severity, SeverityLevel } from '@sentry/types';
44
import { uuid4 } from '@sentry/utils';
@@ -24,20 +24,17 @@ export function setCurrentScope(scope: Scope): void {
2424
* We overwrite this from the core implementation to make sure we get the correct Scope class.
2525
*/
2626
export function getGlobalScope(): Scope {
27-
const globalData = getGlobalData();
28-
29-
if (!globalData.globalScope) {
30-
globalData.globalScope = new Scope();
31-
}
27+
const globalScope = _getGlobalScope();
3228

3329
// If we have a default Scope here by chance, make sure to "upgrade" it to our custom Scope
34-
if (!(globalData.globalScope instanceof Scope)) {
35-
const oldScope = globalData.globalScope;
36-
globalData.globalScope = new Scope();
37-
globalData.globalScope.update(oldScope);
30+
if (!(globalScope instanceof Scope)) {
31+
const newScope = new Scope();
32+
newScope.update(globalScope);
33+
setGlobalScope(newScope);
34+
return newScope;
3835
}
3936

40-
return globalData.globalScope as Scope;
37+
return globalScope;
4138
}
4239

4340
/** Get the currently active isolation scope. */
@@ -97,6 +94,7 @@ export class Scope extends OpenTelemetryScope implements ScopeInterface {
9794
newScope._attachments = [...this['_attachments']];
9895
newScope._sdkProcessingMetadata = { ...this['_sdkProcessingMetadata'] };
9996
newScope._propagationContext = { ...this['_propagationContext'] };
97+
newScope._client = this._client;
10098

10199
return newScope;
102100
}

packages/node-experimental/test/integration/scope.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { clearGlobalData } from '@sentry/core';
1+
import { setGlobalScope } from '@sentry/core';
22
import { getCurrentHub, getSpanScope } from '@sentry/opentelemetry';
33

44
import * as Sentry from '../../src/';
@@ -231,7 +231,7 @@ describe('Integration | Scope', () => {
231231

232232
describe('global scope', () => {
233233
beforeEach(() => {
234-
clearGlobalData();
234+
setGlobalScope(undefined);
235235
});
236236

237237
it('works before calling init', () => {

0 commit comments

Comments
 (0)