Skip to content

Commit c25d4e8

Browse files
committed
fix circular dep checks
1 parent ac94dc5 commit c25d4e8

File tree

2 files changed

+84
-82
lines changed

2 files changed

+84
-82
lines changed

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

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -19,65 +19,15 @@ import type {
1919
User,
2020
} from '@sentry/types';
2121
import { GLOBAL_OBJ, consoleSandbox, dateTimestampInSeconds } from '@sentry/utils';
22-
2322
import { getScopesFromContext, setScopesOnContext } from '../utils/contextData';
23+
2424
import type { ExclusiveEventHintOrCaptureContext } from '../utils/prepareEvent';
2525
import { parseEventHintOrCaptureContext } from '../utils/prepareEvent';
26-
import { getGlobalCarrier } from './globals';
27-
import { Scope } from './scope';
28-
import type { CurrentScopes, SentryCarrier } from './types';
29-
30-
/** Get the currently active client. */
31-
export function getClient<C extends Client>(): C {
32-
const currentScope = getCurrentScope();
33-
const isolationScope = getIsolationScope();
34-
const globalScope = getGlobalScope();
35-
36-
const client = currentScope.getClient() || isolationScope.getClient() || globalScope.getClient();
37-
if (client) {
38-
return client as C;
39-
}
40-
41-
// TODO otherwise ensure we use a noop client
42-
return {} as C;
43-
}
26+
import type { Scope } from './scope';
27+
import { getClient, getCurrentScope, getGlobalScope, getIsolationScope } from './scope';
4428

45-
/** Get the current scope. */
46-
export function getCurrentScope(): Scope {
47-
return getScopes().scope as Scope;
48-
}
49-
50-
/**
51-
* Set the current scope on the execution context.
52-
* This should mostly only be called in Sentry.init()
53-
*/
54-
export function setCurrentScope(scope: Scope): void {
55-
getScopes().scope = scope;
56-
}
57-
58-
/** Get the global scope. */
59-
export function getGlobalScope(): Scope {
60-
const carrier = getGlobalCarrier();
61-
62-
if (!carrier.globalScope) {
63-
carrier.globalScope = new Scope();
64-
}
65-
66-
return carrier.globalScope as Scope;
67-
}
68-
69-
/** Get the currently active isolation scope. */
70-
export function getIsolationScope(): Scope {
71-
return getScopes().isolationScope as Scope;
72-
}
73-
74-
/**
75-
* Set the currently active isolation scope.
76-
* Use this with caution! As it updates the isolation scope for the current execution context.
77-
*/
78-
export function setIsolationScope(isolationScope: Scope): void {
79-
getScopes().isolationScope = isolationScope;
80-
}
29+
export { getCurrentScope, getGlobalScope, getIsolationScope, getClient };
30+
export { setCurrentScope, setIsolationScope } from './scope';
8131

8232
/**
8333
* Fork a scope from the current scope, and make it the current scope in the given callback
@@ -261,31 +211,6 @@ export function endSession(): void {
261211
scope.setSession();
262212
}
263213

264-
function getScopes(): CurrentScopes {
265-
const carrier = getGlobalCarrier();
266-
267-
if (carrier.acs && carrier.acs.getScopes) {
268-
const scopes = carrier.acs.getScopes();
269-
270-
if (scopes) {
271-
return scopes;
272-
}
273-
}
274-
275-
return getGlobalCurrentScopes(carrier);
276-
}
277-
278-
function getGlobalCurrentScopes(carrier: SentryCarrier): CurrentScopes {
279-
if (!carrier.scopes) {
280-
carrier.scopes = {
281-
scope: new Scope(),
282-
isolationScope: new Scope(),
283-
};
284-
}
285-
286-
return carrier.scopes;
287-
}
288-
289214
/**
290215
* Sends the current Session on the scope
291216
*/

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

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,60 @@ import { OpenTelemetryScope } from '@sentry/opentelemetry';
33
import type { Breadcrumb, Client, Event, EventHint, EventProcessor, Severity, SeverityLevel } from '@sentry/types';
44
import { uuid4 } from '@sentry/utils';
55

6-
import { getClient, getGlobalScope, getIsolationScope } from './api';
7-
import type { Scope as ScopeInterface, ScopeData } from './types';
6+
import { getGlobalCarrier } from './globals';
7+
import type { CurrentScopes, Scope as ScopeInterface, ScopeData, SentryCarrier } from './types';
8+
9+
/** Get the current scope. */
10+
export function getCurrentScope(): Scope {
11+
return getScopes().scope as Scope;
12+
}
13+
14+
/**
15+
* Set the current scope on the execution context.
16+
* This should mostly only be called in Sentry.init()
17+
*/
18+
export function setCurrentScope(scope: Scope): void {
19+
getScopes().scope = scope;
20+
}
21+
22+
/** Get the global scope. */
23+
export function getGlobalScope(): Scope {
24+
const carrier = getGlobalCarrier();
25+
26+
if (!carrier.globalScope) {
27+
carrier.globalScope = new Scope();
28+
}
29+
30+
return carrier.globalScope as Scope;
31+
}
32+
33+
/** Get the currently active isolation scope. */
34+
export function getIsolationScope(): Scope {
35+
return getScopes().isolationScope as Scope;
36+
}
37+
38+
/**
39+
* Set the currently active isolation scope.
40+
* Use this with caution! As it updates the isolation scope for the current execution context.
41+
*/
42+
export function setIsolationScope(isolationScope: Scope): void {
43+
getScopes().isolationScope = isolationScope;
44+
}
45+
46+
/** Get the currently active client. */
47+
export function getClient<C extends Client>(): C {
48+
const currentScope = getCurrentScope();
49+
const isolationScope = getIsolationScope();
50+
const globalScope = getGlobalScope();
51+
52+
const client = currentScope.getClient() || isolationScope.getClient() || globalScope.getClient();
53+
if (client) {
54+
return client as C;
55+
}
56+
57+
// TODO otherwise ensure we use a noop client
58+
return {} as C;
59+
}
860

961
/** A fork of the classic scope with some otel specific stuff. */
1062
export class Scope extends OpenTelemetryScope implements ScopeInterface {
@@ -293,3 +345,28 @@ function mergeArray<Prop extends 'breadcrumbs' | 'fingerprint'>(
293345
const merged = [...(prevVal || []), ...mergeVal] as ScopeData[Prop];
294346
event[prop] = merged.length ? merged : undefined;
295347
}
348+
349+
function getScopes(): CurrentScopes {
350+
const carrier = getGlobalCarrier();
351+
352+
if (carrier.acs && carrier.acs.getScopes) {
353+
const scopes = carrier.acs.getScopes();
354+
355+
if (scopes) {
356+
return scopes;
357+
}
358+
}
359+
360+
return getGlobalCurrentScopes(carrier);
361+
}
362+
363+
function getGlobalCurrentScopes(carrier: SentryCarrier): CurrentScopes {
364+
if (!carrier.scopes) {
365+
carrier.scopes = {
366+
scope: new Scope(),
367+
isolationScope: new Scope(),
368+
};
369+
}
370+
371+
return carrier.scopes;
372+
}

0 commit comments

Comments
 (0)