diff --git a/packages/core/src/asyncContext/index.ts b/packages/core/src/asyncContext/index.ts index d13874bd854b..05937851ff7d 100644 --- a/packages/core/src/asyncContext/index.ts +++ b/packages/core/src/asyncContext/index.ts @@ -29,3 +29,14 @@ export function getAsyncContextStrategy(carrier: Carrier): AsyncContextStrategy // Otherwise, use the default one (stack) return getStackAsyncContextStrategy(); } + +/** + * Get the implementation of a specific async context strategy method. + */ +export function getAsyncContextStrategyImplementation( + prop: T, +): AsyncContextStrategy[T] { + const carrier = getMainCarrier(); + const acs = getAsyncContextStrategy(carrier); + return acs[prop]; +} diff --git a/packages/core/src/currentScopes.ts b/packages/core/src/currentScopes.ts index 77e8aa70a02a..53183f09b30c 100644 --- a/packages/core/src/currentScopes.ts +++ b/packages/core/src/currentScopes.ts @@ -1,6 +1,6 @@ import type { Scope } from '@sentry/types'; import type { Client } from '@sentry/types'; -import { getAsyncContextStrategy } from './asyncContext'; +import { getAsyncContextStrategy, getAsyncContextStrategyImplementation } from './asyncContext'; import { getMainCarrier } from './carrier'; import { Scope as ScopeClass } from './scope'; import { getGlobalSingleton } from './utils-hoist/worldwide'; @@ -9,9 +9,7 @@ import { getGlobalSingleton } from './utils-hoist/worldwide'; * Get the currently active scope. */ export function getCurrentScope(): Scope { - const carrier = getMainCarrier(); - const acs = getAsyncContextStrategy(carrier); - return acs.getCurrentScope(); + return getAsyncContextStrategyImplementation('getCurrentScope')(); } /** @@ -19,9 +17,7 @@ export function getCurrentScope(): Scope { * The isolation scope is active for the current execution context. */ export function getIsolationScope(): Scope { - const carrier = getMainCarrier(); - const acs = getAsyncContextStrategy(carrier); - return acs.getIsolationScope(); + return getAsyncContextStrategyImplementation('getIsolationScope')(); } /** diff --git a/packages/core/src/tracing/trace.ts b/packages/core/src/tracing/trace.ts index cac212246f36..6406c9d6a41b 100644 --- a/packages/core/src/tracing/trace.ts +++ b/packages/core/src/tracing/trace.ts @@ -1,12 +1,10 @@ /* eslint-disable max-lines */ import type { ClientOptions, Scope, SentrySpanArguments, Span, SpanTimeInput, StartSpanOptions } from '@sentry/types'; -import type { AsyncContextStrategy } from '../asyncContext/types'; -import { getMainCarrier } from '../carrier'; import { getClient, getCurrentScope, getIsolationScope, withScope } from '../currentScopes'; -import { getAsyncContextStrategy } from '../asyncContext'; +import { getAsyncContextStrategyImplementation } from '../asyncContext'; import { DEBUG_BUILD } from '../debug-build'; import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '../semanticAttributes'; import { logger } from '../utils-hoist/logger'; @@ -37,9 +35,9 @@ const SUPPRESS_TRACING_KEY = '__SENTRY_SUPPRESS_TRACING__'; * it may just be a non-recording span if the span is not sampled or if tracing is disabled. */ export function startSpan(options: StartSpanOptions, callback: (span: Span) => T): T { - const acs = getAcs(); - if (acs.startSpan) { - return acs.startSpan(options, callback); + const acsStartSpan = getAsyncContextStrategyImplementation('startSpan'); + if (acsStartSpan) { + return acsStartSpan(options, callback); } const spanArguments = parseSentrySpanArguments(options); @@ -91,9 +89,9 @@ export function startSpan(options: StartSpanOptions, callback: (span: Span) = * it may just be a non-recording span if the span is not sampled or if tracing is disabled. */ export function startSpanManual(options: StartSpanOptions, callback: (span: Span, finish: () => void) => T): T { - const acs = getAcs(); - if (acs.startSpanManual) { - return acs.startSpanManual(options, callback); + const acsStartSpanManual = getAsyncContextStrategyImplementation('startSpanManual'); + if (acsStartSpanManual) { + return acsStartSpanManual(options, callback); } const spanArguments = parseSentrySpanArguments(options); @@ -147,9 +145,9 @@ export function startSpanManual(options: StartSpanOptions, callback: (span: S * it may just be a non-recording span if the span is not sampled or if tracing is disabled. */ export function startInactiveSpan(options: StartSpanOptions): Span { - const acs = getAcs(); - if (acs.startInactiveSpan) { - return acs.startInactiveSpan(options); + const acsStartInactiveSpan = getAsyncContextStrategyImplementation('startInactiveSpan'); + if (acsStartInactiveSpan) { + return acsStartInactiveSpan(options); } const spanArguments = parseSentrySpanArguments(options); @@ -217,9 +215,9 @@ export const continueTrace = ( * @returns the value returned from the provided callback function. */ export function withActiveSpan(span: Span | null, callback: (scope: Scope) => T): T { - const acs = getAcs(); - if (acs.withActiveSpan) { - return acs.withActiveSpan(span, callback); + const acsImpl = getAsyncContextStrategyImplementation('withActiveSpan'); + if (acsImpl) { + return acsImpl(span, callback); } return withScope(scope => { @@ -230,10 +228,9 @@ export function withActiveSpan(span: Span | null, callback: (scope: Scope) => /** Suppress tracing in the given callback, ensuring no spans are generated inside of it. */ export function suppressTracing(callback: () => T): T { - const acs = getAcs(); - - if (acs.suppressTracing) { - return acs.suppressTracing(callback); + const acsImpl = getAsyncContextStrategyImplementation('suppressTracing'); + if (acsImpl) { + return acsImpl(callback); } return withScope(scope => { @@ -359,11 +356,6 @@ function parseSentrySpanArguments(options: StartSpanOptions): SentrySpanArgument return initialCtx; } -function getAcs(): AsyncContextStrategy { - const carrier = getMainCarrier(); - return getAsyncContextStrategy(carrier); -} - function _startRootSpan(spanArguments: SentrySpanArguments, scope: Scope, parentSampled?: boolean): SentrySpan { const client = getClient(); const options: Partial = (client && client.getOptions()) || {}; diff --git a/packages/core/src/utils/spanUtils.ts b/packages/core/src/utils/spanUtils.ts index d9232b1f48bc..92b5adf08519 100644 --- a/packages/core/src/utils/spanUtils.ts +++ b/packages/core/src/utils/spanUtils.ts @@ -9,8 +9,7 @@ import type { SpanTimeInput, TraceContext, } from '@sentry/types'; -import { getAsyncContextStrategy } from '../asyncContext'; -import { getMainCarrier } from '../carrier'; +import { getAsyncContextStrategyImplementation } from '../asyncContext'; import { getCurrentScope } from '../currentScopes'; import { getMetricSummaryJsonForSpan, updateMetricSummaryOnSpan } from '../metrics/metric-summary'; import type { MetricType } from '../metrics/types'; @@ -259,10 +258,9 @@ export function getRootSpan(span: SpanWithPotentialChildren): Span { * Returns the currently active span. */ export function getActiveSpan(): Span | undefined { - const carrier = getMainCarrier(); - const acs = getAsyncContextStrategy(carrier); - if (acs.getActiveSpan) { - return acs.getActiveSpan(); + const acsImpl = getAsyncContextStrategyImplementation('getActiveSpan'); + if (acsImpl) { + return acsImpl(); } return _getSpanForScope(getCurrentScope()); diff --git a/packages/core/src/utils/traceData.ts b/packages/core/src/utils/traceData.ts index 04727510e07c..0fc636fe5f43 100644 --- a/packages/core/src/utils/traceData.ts +++ b/packages/core/src/utils/traceData.ts @@ -1,6 +1,5 @@ import type { SerializedTraceData } from '@sentry/types'; -import { getAsyncContextStrategy } from '../asyncContext'; -import { getMainCarrier } from '../carrier'; +import { getAsyncContextStrategyImplementation } from '../asyncContext'; import { getClient, getCurrentScope } from '../currentScopes'; import { isEnabled } from '../exports'; import { getDynamicSamplingContextFromClient, getDynamicSamplingContextFromSpan } from '../tracing'; @@ -25,10 +24,9 @@ export function getTraceData(): SerializedTraceData { return {}; } - const carrier = getMainCarrier(); - const acs = getAsyncContextStrategy(carrier); - if (acs.getTraceData) { - return acs.getTraceData(); + const acsImpl = getAsyncContextStrategyImplementation('getTraceData'); + if (acsImpl) { + return acsImpl(); } const client = getClient();