Skip to content

Commit eb11f34

Browse files
authored
ref(core): Always use a (default) ACS (#10644)
Small refactor to ensure we _always_ use a default async context strategy, making the default behavior more explicit. This will (hopefully) make it easier to encapsulate this going forward.
1 parent e6596af commit eb11f34

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
lines changed

packages/core/src/asyncContext.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,6 @@ export function setAsyncContextStrategy(strategy: AsyncContextStrategy | undefin
7070
sentry.acs = strategy;
7171
}
7272

73-
/**
74-
* Runs the supplied callback in its own async context. Async Context strategies are defined per SDK.
75-
*
76-
* @param callback The callback to run in its own async context
77-
* @param options Options to pass to the async context strategy
78-
* @returns The result of the callback
79-
*/
80-
export function runWithAsyncContext<T>(callback: () => T, options: RunWithAsyncContextOptions = {}): T {
81-
const registry = getMainCarrier();
82-
const sentry = getSentryCarrier(registry);
83-
84-
if (sentry.acs) {
85-
return sentry.acs.runWithAsyncContext(callback, options);
86-
}
87-
88-
// if there was no strategy, fallback to just calling the callback
89-
return callback();
90-
}
91-
9273
/** Will either get the existing sentry carrier, or create a new one. */
9374
export function getSentryCarrier(carrier: Carrier): SentryCarrier {
9475
if (!carrier.__SENTRY__) {

packages/core/src/exports.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ import type {
2020
User,
2121
} from '@sentry/types';
2222
import { GLOBAL_OBJ, isThenable, logger, timestampInSeconds, uuid4 } from '@sentry/utils';
23-
import { runWithAsyncContext } from './asyncContext';
2423

2524
import { DEFAULT_ENVIRONMENT } from './constants';
2625
import { getCurrentScope, getIsolationScope } from './currentScopes';
2726
import { DEBUG_BUILD } from './debug-build';
2827
import type { Hub } from './hub';
29-
import { getCurrentHub } from './hub';
28+
import { getCurrentHub, runWithAsyncContext } from './hub';
3029
import type { Scope } from './scope';
3130
import { closeSession, makeSession, updateSession } from './session';
3231
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';

packages/core/src/hub.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type {
2222
} from '@sentry/types';
2323
import { GLOBAL_OBJ, consoleSandbox, dateTimestampInSeconds, isThenable, logger, uuid4 } from '@sentry/utils';
2424

25-
import type { Carrier } from './asyncContext';
25+
import type { AsyncContextStrategy, Carrier, RunWithAsyncContextOptions } from './asyncContext';
2626
import { getMainCarrier, getSentryCarrier } from './asyncContext';
2727
import { DEFAULT_ENVIRONMENT } from './constants';
2828
import { DEBUG_BUILD } from './debug-build';
@@ -649,18 +649,24 @@ export function setHubOnCarrier(carrier: Carrier, hub: HubInterface): boolean {
649649
export function getCurrentHub(): HubInterface {
650650
// Get main carrier (global for every environment)
651651
const carrier = getMainCarrier();
652-
const sentry = getSentryCarrier(carrier);
653652

654-
if (sentry.acs) {
655-
const hub = sentry.acs.getCurrentHub();
653+
const acs = getAsyncContextStrategy(carrier);
654+
return acs.getCurrentHub() || getGlobalHub();
655+
}
656656

657-
if (hub) {
658-
return hub;
659-
}
660-
}
657+
/**
658+
* Runs the supplied callback in its own async context. Async Context strategies are defined per SDK.
659+
*
660+
* @param callback The callback to run in its own async context
661+
* @param options Options to pass to the async context strategy
662+
* @returns The result of the callback
663+
*/
664+
export function runWithAsyncContext<T>(callback: () => T, options: RunWithAsyncContextOptions = {}): T {
665+
// Get main carrier (global for every environment)
666+
const carrier = getMainCarrier();
661667

662-
// Return hub that lives on a global object
663-
return getGlobalHub();
668+
const acs = getAsyncContextStrategy(carrier);
669+
return acs.runWithAsyncContext(callback, options);
664670
}
665671

666672
function getGlobalHub(): HubInterface {
@@ -727,3 +733,27 @@ export function ensureHubOnCarrier(carrier: Carrier, parent: HubInterface = getG
727733
setHubOnCarrier(carrier, new Hub(client, scope.clone() as Scope, isolationScope.clone() as Scope));
728734
}
729735
}
736+
737+
/**
738+
* Get the current async context strategy.
739+
* If none has been setup, the default will be used.
740+
*/
741+
export function getAsyncContextStrategy(carrier: Carrier): AsyncContextStrategy {
742+
const sentry = getSentryCarrier(carrier);
743+
744+
if (sentry.acs) {
745+
return sentry.acs;
746+
}
747+
748+
// Otherwise, use the default one
749+
return getHubStackAsyncContextStrategy();
750+
}
751+
752+
function getHubStackAsyncContextStrategy(): AsyncContextStrategy {
753+
return {
754+
getCurrentHub: getGlobalHub,
755+
runWithAsyncContext: <T>(callback: () => T, _options: RunWithAsyncContextOptions = {}): T => {
756+
return callback();
757+
},
758+
};
759+
}

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export {
4444
makeMain,
4545
setHubOnCarrier,
4646
ensureHubOnCarrier,
47+
runWithAsyncContext,
4748
} from './hub';
4849
export {
4950
getCurrentScope,
@@ -53,7 +54,6 @@ export {
5354
} from './currentScopes';
5455
export {
5556
getMainCarrier,
56-
runWithAsyncContext,
5757
setAsyncContextStrategy,
5858
} from './asyncContext';
5959
export { makeSession, closeSession, updateSession } from './session';

packages/core/src/tracing/trace.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { Hub, Scope, Span, SpanTimeInput, StartSpanOptions, TransactionContext } from '@sentry/types';
22

33
import { addNonEnumerableProperty, dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/utils';
4-
import { runWithAsyncContext } from '../asyncContext';
54
import { getCurrentScope, getIsolationScope } from '../currentScopes';
65

76
import { DEBUG_BUILD } from '../debug-build';
87
import { withScope } from '../exports';
9-
import { getCurrentHub } from '../hub';
8+
import { getCurrentHub, runWithAsyncContext } from '../hub';
109
import { handleCallbackErrors } from '../utils/handleCallbackErrors';
1110
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
1211
import { spanTimeInputToSeconds, spanToJSON } from '../utils/spanUtils';

0 commit comments

Comments
 (0)