Skip to content

Commit 170ffc8

Browse files
authored
feat(core): Add async context abstraction (#7753)
1 parent 3a758e3 commit 170ffc8

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

packages/core/src/hub.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ export const API_VERSION = 4;
5050
*/
5151
const DEFAULT_BREADCRUMBS = 100;
5252

53+
/**
54+
* Strategy used to track async context.
55+
*/
56+
export interface AsyncContextStrategy {
57+
getCurrentHub: () => Hub | undefined;
58+
runWithAsyncContext<T, A>(callback: (hub: Hub, ...args: A[]) => T, ...args: A[]): T;
59+
}
60+
5361
/**
5462
* A layer in the process stack.
5563
* @hidden
@@ -66,6 +74,7 @@ export interface Layer {
6674
export interface Carrier {
6775
__SENTRY__?: {
6876
hub?: Hub;
77+
acs?: AsyncContextStrategy;
6978
/**
7079
* Extra Hub properties injected by various SDKs
7180
*/
@@ -519,6 +528,14 @@ export function getCurrentHub(): Hub {
519528
// Get main carrier (global for every environment)
520529
const registry = getMainCarrier();
521530

531+
if (registry.__SENTRY__ && registry.__SENTRY__.acs) {
532+
const hub = registry.__SENTRY__.acs.getCurrentHub();
533+
534+
if (hub) {
535+
return hub;
536+
}
537+
}
538+
522539
// If there's no hub, or its an old API, assign a new one
523540
if (!hasHubOnCarrier(registry) || getHubFromCarrier(registry).isOlderThan(API_VERSION)) {
524541
setHubOnCarrier(registry, new Hub());
@@ -532,6 +549,34 @@ export function getCurrentHub(): Hub {
532549
return getHubFromCarrier(registry);
533550
}
534551

552+
/**
553+
* @private Private API with no semver guarantees!
554+
*
555+
* Sets the global async context strategy
556+
*/
557+
export function setAsyncContextStrategy(strategy: AsyncContextStrategy): void {
558+
// Get main carrier (global for every environment)
559+
const registry = getMainCarrier();
560+
registry.__SENTRY__ = registry.__SENTRY__ || {};
561+
registry.__SENTRY__.acs = strategy;
562+
}
563+
564+
/**
565+
* @private Private API with no semver guarantees!
566+
*
567+
* Runs the given callback function with the global async context strategy
568+
*/
569+
export function runWithAsyncContext<T, A>(callback: (hub: Hub, ...args: A[]) => T, ...args: A[]): T {
570+
const registry = getMainCarrier();
571+
572+
if (registry.__SENTRY__ && registry.__SENTRY__.acs) {
573+
return registry.__SENTRY__.acs.runWithAsyncContext(callback, ...args);
574+
}
575+
576+
// if there was no strategy, fallback to just calling the callback
577+
return callback(getCurrentHub(), ...args);
578+
}
579+
535580
/**
536581
* Try to read the hub from an active domain, and fallback to the registry if one doesn't exist
537582
* @returns discovered hub

packages/core/src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export type { ClientClass } from './sdk';
2-
export type { Carrier, Layer } from './hub';
2+
export type { AsyncContextStrategy, Carrier, Layer } from './hub';
33
export type { OfflineStore, OfflineTransportOptions } from './transports/offline';
44

55
export * from './tracing';
@@ -18,7 +18,16 @@ export {
1818
setUser,
1919
withScope,
2020
} from './exports';
21-
export { getCurrentHub, getHubFromCarrier, Hub, makeMain, getMainCarrier, setHubOnCarrier } from './hub';
21+
export {
22+
getCurrentHub,
23+
getHubFromCarrier,
24+
Hub,
25+
makeMain,
26+
getMainCarrier,
27+
runWithAsyncContext,
28+
setHubOnCarrier,
29+
setAsyncContextStrategy,
30+
} from './hub';
2231
export { makeSession, closeSession, updateSession } from './session';
2332
export { SessionFlusher } from './sessionflusher';
2433
export { addGlobalEventProcessor, Scope } from './scope';

0 commit comments

Comments
 (0)