Skip to content

ref(core): Always use a (default) ACS #10644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions packages/core/src/asyncContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,6 @@ export function setAsyncContextStrategy(strategy: AsyncContextStrategy | undefin
sentry.acs = strategy;
}

/**
* Runs the supplied callback in its own async context. Async Context strategies are defined per SDK.
*
* @param callback The callback to run in its own async context
* @param options Options to pass to the async context strategy
* @returns The result of the callback
*/
export function runWithAsyncContext<T>(callback: () => T, options: RunWithAsyncContextOptions = {}): T {
const registry = getMainCarrier();
const sentry = getSentryCarrier(registry);

if (sentry.acs) {
return sentry.acs.runWithAsyncContext(callback, options);
}

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

/** Will either get the existing sentry carrier, or create a new one. */
export function getSentryCarrier(carrier: Carrier): SentryCarrier {
if (!carrier.__SENTRY__) {
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import type {
User,
} from '@sentry/types';
import { GLOBAL_OBJ, isThenable, logger, timestampInSeconds, uuid4 } from '@sentry/utils';
import { runWithAsyncContext } from './asyncContext';

import { DEFAULT_ENVIRONMENT } from './constants';
import { getCurrentScope, getIsolationScope } from './currentScopes';
import { DEBUG_BUILD } from './debug-build';
import type { Hub } from './hub';
import { getCurrentHub } from './hub';
import { getCurrentHub, runWithAsyncContext } from './hub';
import type { Scope } from './scope';
import { closeSession, makeSession, updateSession } from './session';
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
Expand Down
50 changes: 40 additions & 10 deletions packages/core/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
} from '@sentry/types';
import { GLOBAL_OBJ, consoleSandbox, dateTimestampInSeconds, isThenable, logger, uuid4 } from '@sentry/utils';

import type { Carrier } from './asyncContext';
import type { AsyncContextStrategy, Carrier, RunWithAsyncContextOptions } from './asyncContext';
import { getMainCarrier, getSentryCarrier } from './asyncContext';
import { DEFAULT_ENVIRONMENT } from './constants';
import { DEBUG_BUILD } from './debug-build';
Expand Down Expand Up @@ -649,18 +649,24 @@ export function setHubOnCarrier(carrier: Carrier, hub: HubInterface): boolean {
export function getCurrentHub(): HubInterface {
// Get main carrier (global for every environment)
const carrier = getMainCarrier();
const sentry = getSentryCarrier(carrier);

if (sentry.acs) {
const hub = sentry.acs.getCurrentHub();
const acs = getAsyncContextStrategy(carrier);
return acs.getCurrentHub() || getGlobalHub();
}

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

// Return hub that lives on a global object
return getGlobalHub();
const acs = getAsyncContextStrategy(carrier);
return acs.runWithAsyncContext(callback, options);
}

function getGlobalHub(): HubInterface {
Expand Down Expand Up @@ -727,3 +733,27 @@ export function ensureHubOnCarrier(carrier: Carrier, parent: HubInterface = getG
setHubOnCarrier(carrier, new Hub(client, scope.clone() as Scope, isolationScope.clone() as Scope));
}
}

/**
* Get the current async context strategy.
* If none has been setup, the default will be used.
*/
export function getAsyncContextStrategy(carrier: Carrier): AsyncContextStrategy {
const sentry = getSentryCarrier(carrier);

if (sentry.acs) {
return sentry.acs;
}

// Otherwise, use the default one
return getHubStackAsyncContextStrategy();
}

function getHubStackAsyncContextStrategy(): AsyncContextStrategy {
return {
getCurrentHub: getGlobalHub,
runWithAsyncContext: <T>(callback: () => T, _options: RunWithAsyncContextOptions = {}): T => {
return callback();
},
};
}
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export {
makeMain,
setHubOnCarrier,
ensureHubOnCarrier,
runWithAsyncContext,
} from './hub';
export {
getCurrentScope,
Expand All @@ -53,7 +54,6 @@ export {
} from './currentScopes';
export {
getMainCarrier,
runWithAsyncContext,
setAsyncContextStrategy,
} from './asyncContext';
export { makeSession, closeSession, updateSession } from './session';
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { Hub, Scope, Span, SpanTimeInput, StartSpanOptions, TransactionContext } from '@sentry/types';

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

import { DEBUG_BUILD } from '../debug-build';
import { withScope } from '../exports';
import { getCurrentHub } from '../hub';
import { getCurrentHub, runWithAsyncContext } from '../hub';
import { handleCallbackErrors } from '../utils/handleCallbackErrors';
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
import { spanTimeInputToSeconds, spanToJSON } from '../utils/spanUtils';
Expand Down