Skip to content
Closed
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
11 changes: 11 additions & 0 deletions packages/core/src/asyncContext/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends keyof AsyncContextStrategy>(
prop: T,
): AsyncContextStrategy[T] {
const carrier = getMainCarrier();
const acs = getAsyncContextStrategy(carrier);
return acs[prop];
}
10 changes: 3 additions & 7 deletions packages/core/src/currentScopes.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,19 +9,15 @@ 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')();
}

/**
* Get the currently active isolation 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')();
}

/**
Expand Down
40 changes: 16 additions & 24 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<T>(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);
Expand Down Expand Up @@ -91,9 +89,9 @@ export function startSpan<T>(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<T>(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);
Expand Down Expand Up @@ -147,9 +145,9 @@ export function startSpanManual<T>(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);
Expand Down Expand Up @@ -217,9 +215,9 @@ export const continueTrace = <V>(
* @returns the value returned from the provided callback function.
*/
export function withActiveSpan<T>(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 => {
Expand All @@ -230,10 +228,9 @@ export function withActiveSpan<T>(span: Span | null, callback: (scope: Scope) =>

/** Suppress tracing in the given callback, ensuring no spans are generated inside of it. */
export function suppressTracing<T>(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 => {
Expand Down Expand Up @@ -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<ClientOptions> = (client && client.getOptions()) || {};
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/utils/spanUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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());
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/utils/traceData.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
Expand Down
Loading