|
| 1 | +import { getClient } from '../currentScopes'; |
| 2 | +import type { Options } from '../types-hoist'; |
| 3 | + |
| 4 | +// Treeshakable guard to remove all code related to tracing |
| 5 | +declare const __SENTRY_TRACING__: boolean | undefined; |
| 6 | + |
| 7 | +/** |
| 8 | + * Determines if span recording is currently enabled. |
| 9 | + * |
| 10 | + * Spans are recorded when at least one of `tracesSampleRate` and `tracesSampler` |
| 11 | + * is defined in the SDK config. This function does not make any assumption about |
| 12 | + * sampling decisions, it only checks if the SDK is configured to record spans. |
| 13 | + * |
| 14 | + * Important: This function only determines if span recording is enabled. Trace |
| 15 | + * continuation and propagation is separately controlled and not covered by this function. |
| 16 | + * If this function returns `false`, traces can still be propagated (which is what |
| 17 | + * we refer to by "Tracing without Performance") |
| 18 | + * @see https://develop.sentry.dev/sdk/telemetry/traces/tracing-without-performance/ |
| 19 | + * |
| 20 | + * @param maybeOptions An SDK options object to be passed to this function. |
| 21 | + * If this option is not provided, the function will use the current client's options. |
| 22 | + */ |
| 23 | +export function hasSpansEnabled( |
| 24 | + maybeOptions?: Pick<Options, 'tracesSampleRate' | 'tracesSampler'> | undefined, |
| 25 | +): boolean { |
| 26 | + if (typeof __SENTRY_TRACING__ === 'boolean' && !__SENTRY_TRACING__) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + const options = maybeOptions || getClient()?.getOptions(); |
| 31 | + return ( |
| 32 | + !!options && |
| 33 | + // Note: This check is `!= null`, meaning "nullish". `0` is not "nullish", `undefined` and `null` are. (This comment was brought to you by 15 minutes of questioning life) |
| 34 | + (options.tracesSampleRate != null || !!options.tracesSampler) |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * @see JSDoc of `hasSpansEnabled` |
| 40 | + * @deprecated Use `hasSpansEnabled` instead, which is a more accurately named version of this function. |
| 41 | + * This function will be removed in the next major version of the SDK. |
| 42 | + */ |
| 43 | +// TODO(v10): Remove this export |
| 44 | +export const hasTracingEnabled = hasSpansEnabled; |
0 commit comments