Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Move globals to __SENTRY__ singleton #11034

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions packages/core/src/currentScopes.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import type { Scope } from '@sentry/types';
import type { Client } from '@sentry/types';
import { getGlobalSingleton } from '@sentry/utils';
import { getMainCarrier } from './asyncContext';
import { getAsyncContextStrategy } from './hub';
import { Scope as ScopeClass } from './scope';

/**
* The global scope is kept in this module.
* When accessing it, we'll make sure to set one if none is currently present.
*/
let globalScope: Scope | undefined;

/**
* Get the currently active scope.
*/
Expand All @@ -34,20 +29,7 @@ export function getIsolationScope(): Scope {
* This scope is applied to _all_ events.
*/
export function getGlobalScope(): Scope {
if (!globalScope) {
globalScope = new ScopeClass();
}

return globalScope;
}

/**
* This is mainly needed for tests.
* DO NOT USE this, as this is an internal API and subject to change.
* @hidden
*/
export function setGlobalScope(scope: Scope | undefined): void {
globalScope = scope;
return getGlobalSingleton('globalScope', () => new ScopeClass());
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export {
getCurrentScope,
getIsolationScope,
getGlobalScope,
setGlobalScope,
withScope,
withIsolationScope,
getClient,
Expand Down
14 changes: 5 additions & 9 deletions packages/core/src/metrics/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
MetricsAggregator as MetricsAggregatorInterface,
Primitive,
} from '@sentry/types';
import { logger } from '@sentry/utils';
import { getGlobalSingleton, logger } from '@sentry/utils';
import { getCurrentScope } from '../currentScopes';
import { getClient } from '../currentScopes';
import { DEBUG_BUILD } from '../debug-build';
Expand All @@ -23,11 +23,6 @@ type MetricsAggregatorConstructor = {
new (client: Client): MetricsAggregatorInterface;
};

/**
* A metrics aggregator instance per Client.
*/
let globalMetricsAggregators: WeakMap<Client, MetricsAggregatorInterface> | undefined;

/**
* Gets the metrics aggregator for a given client.
* @param client The client for which to get the metrics aggregator.
Expand All @@ -37,9 +32,10 @@ function getMetricsAggregatorForClient(
client: Client,
Aggregator: MetricsAggregatorConstructor,
): MetricsAggregatorInterface {
if (!globalMetricsAggregators) {
globalMetricsAggregators = new WeakMap();
}
const globalMetricsAggregators = getGlobalSingleton<WeakMap<Client, MetricsAggregatorInterface>>(
'globalMetricsAggregators',
() => new WeakMap(),
);

const aggregator = globalMetricsAggregators.get(client);
if (aggregator) {
Expand Down
13 changes: 3 additions & 10 deletions packages/core/test/lib/base.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import type { Client, Envelope, Event } from '@sentry/types';
import { SentryError, SyncPromise, dsnToString, logger } from '@sentry/utils';

import {
Scope,
addBreadcrumb,
getCurrentScope,
getIsolationScope,
makeSession,
setCurrentClient,
setGlobalScope,
} from '../../src';
import { Scope, addBreadcrumb, getCurrentScope, getIsolationScope, makeSession, setCurrentClient } from '../../src';
import * as integrationModule from '../../src/integration';
import { TestClient, getDefaultTestClientOptions } from '../mocks/client';
import { AdHocIntegration, TestIntegration } from '../mocks/integration';
import { makeFakeTransport } from '../mocks/transport';
import { clearGlobalScope } from './clear-global-scope';

const PUBLIC_DSN = 'https://username@domain/123';
// eslint-disable-next-line no-var
Expand Down Expand Up @@ -62,7 +55,7 @@ describe('BaseClient', () => {
beforeEach(() => {
TestClient.sendEventCalled = undefined;
TestClient.instance = undefined;
setGlobalScope(undefined);
clearGlobalScope();
getCurrentScope().clear();
getCurrentScope().setClient(undefined);
getIsolationScope().clear();
Expand Down
6 changes: 6 additions & 0 deletions packages/core/test/lib/clear-global-scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { GLOBAL_OBJ } from '@sentry/utils';

export function clearGlobalScope() {
const __SENTRY__ = (GLOBAL_OBJ.__SENTRY__ = GLOBAL_OBJ.__SENTRY__ || {});
__SENTRY__.globalScope = undefined;
}
5 changes: 3 additions & 2 deletions packages/core/test/lib/prepareEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
ScopeContext,
} from '@sentry/types';
import { GLOBAL_OBJ, createStackParser } from '@sentry/utils';
import { getGlobalScope, getIsolationScope, setGlobalScope } from '../../src';
import { getGlobalScope, getIsolationScope } from '../../src';

import { Scope } from '../../src/scope';
import {
Expand All @@ -18,6 +18,7 @@ import {
parseEventHintOrCaptureContext,
prepareEvent,
} from '../../src/utils/prepareEvent';
import { clearGlobalScope } from './clear-global-scope';

describe('applyDebugIds', () => {
afterEach(() => {
Expand Down Expand Up @@ -191,7 +192,7 @@ describe('parseEventHintOrCaptureContext', () => {

describe('prepareEvent', () => {
beforeEach(() => {
setGlobalScope(undefined);
clearGlobalScope();
getIsolationScope().clear();
});

Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/lib/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import {
getCurrentScope,
getGlobalScope,
getIsolationScope,
setGlobalScope,
withIsolationScope,
} from '../../src';

import { Scope } from '../../src/scope';
import { TestClient, getDefaultTestClientOptions } from '../mocks/client';
import { clearGlobalScope } from './clear-global-scope';

describe('Scope', () => {
beforeEach(() => {
setGlobalScope(undefined);
clearGlobalScope();
});

it('allows to create & update a scope', () => {
Expand Down Expand Up @@ -491,7 +491,7 @@ describe('Scope', () => {

describe('global scope', () => {
beforeEach(() => {
setGlobalScope(undefined);
clearGlobalScope();
});

it('works', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/node-experimental/test/integration/scope.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getCurrentScope, setGlobalScope } from '@sentry/core';
import { getCurrentScope } from '@sentry/core';
import { getClient, getSpanScopes } from '@sentry/opentelemetry';
import { clearGlobalScope } from '../../../core/test/lib/clear-global-scope';

import * as Sentry from '../../src/';
import type { NodeClient } from '../../src/sdk/client';
Expand Down Expand Up @@ -246,7 +247,7 @@ describe('Integration | Scope', () => {

describe('global scope', () => {
beforeEach(() => {
setGlobalScope(undefined);
clearGlobalScope();
});

it('works before calling init', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/src/worldwide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/* eslint-disable @typescript-eslint/no-explicit-any */

import type { Integration } from '@sentry/types';
import type { Client, Integration, MetricsAggregator, Scope } from '@sentry/types';

import type { SdkSource } from './env';

Expand Down Expand Up @@ -54,6 +54,8 @@ export interface InternalGlobal {
// eslint-disable-next-line @typescript-eslint/ban-types
[key: string]: Function;
};
globalScope: Scope | undefined;
globalMetricsAggregators: WeakMap<Client, MetricsAggregator> | undefined;
};
/**
* Raw module metadata that is injected by bundler plugins.
Expand Down
Loading