Skip to content

Commit

Permalink
feat(core): Add async context abstraction (#7753)
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish authored Apr 5, 2023
1 parent 3a758e3 commit 170ffc8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
45 changes: 45 additions & 0 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export const API_VERSION = 4;
*/
const DEFAULT_BREADCRUMBS = 100;

/**
* Strategy used to track async context.
*/
export interface AsyncContextStrategy {
getCurrentHub: () => Hub | undefined;
runWithAsyncContext<T, A>(callback: (hub: Hub, ...args: A[]) => T, ...args: A[]): T;
}

/**
* A layer in the process stack.
* @hidden
Expand All @@ -66,6 +74,7 @@ export interface Layer {
export interface Carrier {
__SENTRY__?: {
hub?: Hub;
acs?: AsyncContextStrategy;
/**
* Extra Hub properties injected by various SDKs
*/
Expand Down Expand Up @@ -519,6 +528,14 @@ export function getCurrentHub(): Hub {
// Get main carrier (global for every environment)
const registry = getMainCarrier();

if (registry.__SENTRY__ && registry.__SENTRY__.acs) {
const hub = registry.__SENTRY__.acs.getCurrentHub();

if (hub) {
return hub;
}
}

// If there's no hub, or its an old API, assign a new one
if (!hasHubOnCarrier(registry) || getHubFromCarrier(registry).isOlderThan(API_VERSION)) {
setHubOnCarrier(registry, new Hub());
Expand All @@ -532,6 +549,34 @@ export function getCurrentHub(): Hub {
return getHubFromCarrier(registry);
}

/**
* @private Private API with no semver guarantees!
*
* Sets the global async context strategy
*/
export function setAsyncContextStrategy(strategy: AsyncContextStrategy): void {
// Get main carrier (global for every environment)
const registry = getMainCarrier();
registry.__SENTRY__ = registry.__SENTRY__ || {};
registry.__SENTRY__.acs = strategy;
}

/**
* @private Private API with no semver guarantees!
*
* Runs the given callback function with the global async context strategy
*/
export function runWithAsyncContext<T, A>(callback: (hub: Hub, ...args: A[]) => T, ...args: A[]): T {
const registry = getMainCarrier();

if (registry.__SENTRY__ && registry.__SENTRY__.acs) {
return registry.__SENTRY__.acs.runWithAsyncContext(callback, ...args);
}

// if there was no strategy, fallback to just calling the callback
return callback(getCurrentHub(), ...args);
}

/**
* Try to read the hub from an active domain, and fallback to the registry if one doesn't exist
* @returns discovered hub
Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type { ClientClass } from './sdk';
export type { Carrier, Layer } from './hub';
export type { AsyncContextStrategy, Carrier, Layer } from './hub';
export type { OfflineStore, OfflineTransportOptions } from './transports/offline';

export * from './tracing';
Expand All @@ -18,7 +18,16 @@ export {
setUser,
withScope,
} from './exports';
export { getCurrentHub, getHubFromCarrier, Hub, makeMain, getMainCarrier, setHubOnCarrier } from './hub';
export {
getCurrentHub,
getHubFromCarrier,
Hub,
makeMain,
getMainCarrier,
runWithAsyncContext,
setHubOnCarrier,
setAsyncContextStrategy,
} from './hub';
export { makeSession, closeSession, updateSession } from './session';
export { SessionFlusher } from './sessionflusher';
export { addGlobalEventProcessor, Scope } from './scope';
Expand Down

0 comments on commit 170ffc8

Please sign in to comment.