diff --git a/packages/core/src/globals.ts b/packages/core/src/globals.ts deleted file mode 100644 index e889b4fa0709..000000000000 --- a/packages/core/src/globals.ts +++ /dev/null @@ -1,22 +0,0 @@ -import type { Scope } from '@sentry/types'; - -interface GlobalData { - globalScope?: Scope; -} - -const GLOBAL_DATA: GlobalData = {}; - -/** - * Get the global data. - */ -export function getGlobalData(): GlobalData { - return GLOBAL_DATA; -} - -/** - * Reset all global data. - * Mostly useful for tests. - */ -export function clearGlobalData(): void { - delete GLOBAL_DATA.globalScope; -} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 400aac801ebb..dbf18135ef67 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -42,8 +42,7 @@ export { } from './hub'; export { makeSession, closeSession, updateSession } from './session'; export { SessionFlusher } from './sessionflusher'; -export { Scope, getGlobalScope } from './scope'; -export { clearGlobalData, getGlobalData } from './globals'; +export { Scope, getGlobalScope, setGlobalScope } from './scope'; export { notifyEventProcessors, // eslint-disable-next-line deprecation/deprecation diff --git a/packages/core/src/scope.ts b/packages/core/src/scope.ts index 9931f8d94279..08b77c4c4108 100644 --- a/packages/core/src/scope.ts +++ b/packages/core/src/scope.ts @@ -26,15 +26,20 @@ import type { import { dateTimestampInSeconds, isPlainObject, uuid4 } from '@sentry/utils'; import { getGlobalEventProcessors, notifyEventProcessors } from './eventProcessors'; -import { getGlobalData } from './globals'; import { updateSession } from './session'; -import { applyScopeDataToEvent, mergeScopeData } from './utils/applyScopeDataToEvent'; +import { applyScopeDataToEvent } from './utils/applyScopeDataToEvent'; /** * Default value for maximum number of breadcrumbs added to an event. */ const DEFAULT_MAX_BREADCRUMBS = 100; +/** + * The global scope is kept in this module. + * When accessing this via `getGlobalScope()` we'll make sure to set one if none is currently present. + */ +let globalScope: ScopeInterface | undefined; + /** * Holds additional event information. {@link Scope.applyToEvent} will be * called by the client before an event will be sent. @@ -579,12 +584,19 @@ export class Scope implements ScopeInterface { * This scope is applied to _all_ events. */ export function getGlobalScope(): ScopeInterface { - const globalData = getGlobalData(); - if (!globalData.globalScope) { - globalData.globalScope = new Scope(); + if (!globalScope) { + globalScope = new Scope(); } - return globalData.globalScope; + return globalScope; +} + +/** + * This is mainly needed for tests. + * @hidden + */ +export function setGlobalScope(scope: ScopeInterface | undefined): void { + globalScope = scope; } function generatePropagationContext(): PropagationContext { diff --git a/packages/core/test/lib/base.test.ts b/packages/core/test/lib/base.test.ts index 9ba1d60efeab..0b39216e19a5 100644 --- a/packages/core/test/lib/base.test.ts +++ b/packages/core/test/lib/base.test.ts @@ -1,7 +1,7 @@ import type { Client, Envelope, Event, Span, Transaction } from '@sentry/types'; import { SentryError, SyncPromise, dsnToString, logger } from '@sentry/utils'; -import { Hub, Scope, clearGlobalData, makeSession } from '../../src'; +import { Hub, Scope, makeSession, setGlobalScope } from '../../src'; import * as integrationModule from '../../src/integration'; import { TestClient, getDefaultTestClientOptions } from '../mocks/client'; import { AdHocIntegration, TestIntegration } from '../mocks/integration'; @@ -54,7 +54,7 @@ describe('BaseClient', () => { beforeEach(() => { TestClient.sendEventCalled = undefined; TestClient.instance = undefined; - clearGlobalData(); + setGlobalScope(undefined); }); afterEach(() => { diff --git a/packages/core/test/lib/prepareEvent.test.ts b/packages/core/test/lib/prepareEvent.test.ts index 9e2f69e0f654..09c48afffc35 100644 --- a/packages/core/test/lib/prepareEvent.test.ts +++ b/packages/core/test/lib/prepareEvent.test.ts @@ -9,7 +9,7 @@ import type { ScopeContext, } from '@sentry/types'; import { GLOBAL_OBJ, createStackParser } from '@sentry/utils'; -import { clearGlobalData } from '../../src'; +import { setGlobalScope } from '../../src'; import { Scope, getGlobalScope } from '../../src/scope'; import { @@ -191,7 +191,7 @@ describe('parseEventHintOrCaptureContext', () => { describe('prepareEvent', () => { beforeEach(() => { - clearGlobalData(); + setGlobalScope(undefined); }); it('works without any scope data', async () => { diff --git a/packages/core/test/lib/scope.test.ts b/packages/core/test/lib/scope.test.ts index 3bcdbf4022ce..e04f5638c5d0 100644 --- a/packages/core/test/lib/scope.test.ts +++ b/packages/core/test/lib/scope.test.ts @@ -1,11 +1,10 @@ -import { applyScopeDataToEvent } from '@sentry/core'; -import type { Attachment, Breadcrumb, EventProcessor } from '@sentry/types'; -import { clearGlobalData } from '../../src/globals'; -import { Scope, getGlobalScope } from '../../src/scope'; +import type { Attachment, Breadcrumb } from '@sentry/types'; +import { applyScopeDataToEvent } from '../../src'; +import { Scope, getGlobalScope, setGlobalScope } from '../../src/scope'; describe('Unit | Scope', () => { beforeEach(() => { - clearGlobalData(); + setGlobalScope(undefined); }); it('allows to create & update a scope', () => { @@ -91,7 +90,7 @@ describe('Unit | Scope', () => { describe('global scope', () => { beforeEach(() => { - clearGlobalData(); + setGlobalScope(undefined); }); it('works', () => { diff --git a/packages/node-experimental/src/otel/asyncContextStrategy.ts b/packages/node-experimental/src/otel/asyncContextStrategy.ts index e0d976c71ff1..10b4ea6d7b91 100644 --- a/packages/node-experimental/src/otel/asyncContextStrategy.ts +++ b/packages/node-experimental/src/otel/asyncContextStrategy.ts @@ -1,6 +1,6 @@ import * as api from '@opentelemetry/api'; -import { setAsyncContextStrategy } from './../sdk/globals'; +import { setAsyncContextStrategy } from '../sdk/globals'; import { getCurrentHub } from './../sdk/hub'; import type { CurrentScopes } from './../sdk/types'; import { getScopesFromContext } from './../utils/contextData'; diff --git a/packages/node-experimental/src/sdk/scope.ts b/packages/node-experimental/src/sdk/scope.ts index 72b558c02e1f..f169f95250e2 100644 --- a/packages/node-experimental/src/sdk/scope.ts +++ b/packages/node-experimental/src/sdk/scope.ts @@ -1,4 +1,4 @@ -import { getGlobalData, mergeScopeData } from '@sentry/core'; +import { getGlobalScope as _getGlobalScope, mergeScopeData, setGlobalScope } from '@sentry/core'; import { OpenTelemetryScope } from '@sentry/opentelemetry'; import type { Breadcrumb, Client, Event, EventHint, Severity, SeverityLevel } from '@sentry/types'; import { uuid4 } from '@sentry/utils'; @@ -24,20 +24,17 @@ export function setCurrentScope(scope: Scope): void { * We overwrite this from the core implementation to make sure we get the correct Scope class. */ export function getGlobalScope(): Scope { - const globalData = getGlobalData(); - - if (!globalData.globalScope) { - globalData.globalScope = new Scope(); - } + const globalScope = _getGlobalScope(); // If we have a default Scope here by chance, make sure to "upgrade" it to our custom Scope - if (!(globalData.globalScope instanceof Scope)) { - const oldScope = globalData.globalScope; - globalData.globalScope = new Scope(); - globalData.globalScope.update(oldScope); + if (!(globalScope instanceof Scope)) { + const newScope = new Scope(); + newScope.update(globalScope); + setGlobalScope(newScope); + return newScope; } - return globalData.globalScope as Scope; + return globalScope; } /** Get the currently active isolation scope. */ @@ -97,6 +94,7 @@ export class Scope extends OpenTelemetryScope implements ScopeInterface { newScope._attachments = [...this['_attachments']]; newScope._sdkProcessingMetadata = { ...this['_sdkProcessingMetadata'] }; newScope._propagationContext = { ...this['_propagationContext'] }; + newScope._client = this._client; return newScope; } diff --git a/packages/node-experimental/test/integration/scope.test.ts b/packages/node-experimental/test/integration/scope.test.ts index 9604aa3c1ac3..3efb3d09506d 100644 --- a/packages/node-experimental/test/integration/scope.test.ts +++ b/packages/node-experimental/test/integration/scope.test.ts @@ -1,4 +1,4 @@ -import { clearGlobalData } from '@sentry/core'; +import { setGlobalScope } from '@sentry/core'; import { getCurrentHub, getSpanScope } from '@sentry/opentelemetry'; import * as Sentry from '../../src/'; @@ -231,7 +231,7 @@ describe('Integration | Scope', () => { describe('global scope', () => { beforeEach(() => { - clearGlobalData(); + setGlobalScope(undefined); }); it('works before calling init', () => {