Skip to content

Commit

Permalink
ref: avoid globals.ts for scope
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Dec 21, 2023
1 parent 783c49f commit 94ae54c
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 54 deletions.
22 changes: 0 additions & 22 deletions packages/core/src/globals.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/core/src/index.ts
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions packages/core/src/scope.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions 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';
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('BaseClient', () => {
beforeEach(() => {
TestClient.sendEventCalled = undefined;
TestClient.instance = undefined;
clearGlobalData();
setGlobalScope(undefined);
});

afterEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/lib/prepareEvent.test.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('parseEventHintOrCaptureContext', () => {

describe('prepareEvent', () => {
beforeEach(() => {
clearGlobalData();
setGlobalScope(undefined);
});

it('works without any scope data', async () => {
Expand Down
11 changes: 5 additions & 6 deletions 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', () => {
Expand Down Expand Up @@ -91,7 +90,7 @@ describe('Unit | Scope', () => {

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

it('works', () => {
Expand Down
@@ -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';
Expand Down
20 changes: 9 additions & 11 deletions 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';
Expand All @@ -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. */
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions 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/';
Expand Down Expand Up @@ -231,7 +231,7 @@ describe('Integration | Scope', () => {

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

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

0 comments on commit 94ae54c

Please sign in to comment.