UNPKG

@sentry/core

Version:
66 lines (65 loc) • 2.93 kB
import { DEBUG_BUILD } from "../debug-build.js"; import { debug } from "../utils/debug-logger.js"; import { hasSpansEnabled } from "../utils/hasSpansEnabled.js"; import { parseSampleRate } from "../utils/parseSampleRate.js"; import { safeCallback } from "../utils/safeCallback.js"; // src/tracing/sampling.ts /** * Makes a sampling decision for the given options. * * Called every time a root span is created. Only root spans which emerge with a `sampled` value of `true` will be * sent to Sentry. */ function sampleSpan(options, samplingContext, sampleRand) { if (!hasSpansEnabled(options)) return [false]; const resolved = resolveSampleRate(options, samplingContext); if (!resolved) return [ false, void 0, void 0, "callback_error" ]; const [sampleRate, localSampleRateWasApplied] = resolved; const parsedSampleRate = parseSampleRate(sampleRate); if (parsedSampleRate === void 0) { DEBUG_BUILD && debug.warn(`[Tracing] Discarding root span because of invalid sample rate. Sample rate must be a boolean or a number between 0 and 1. Got ${JSON.stringify(sampleRate)} of type ${JSON.stringify(typeof sampleRate)}.`); return [false]; } if (!parsedSampleRate) { DEBUG_BUILD && debug.log(`[Tracing] Discarding transaction because ${typeof options.tracesSampler === "function" ? "tracesSampler returned 0 or false" : "a negative sampling decision was inherited or tracesSampleRate is set to 0"}`); return [ false, parsedSampleRate, localSampleRateWasApplied ]; } const shouldSample = sampleRand < parsedSampleRate; if (!shouldSample) DEBUG_BUILD && debug.log(`[Tracing] Discarding transaction because it's not included in the random sample (sampling rate = ${Number(sampleRate)})`); return [ shouldSample, parsedSampleRate, localSampleRateWasApplied ]; } /** * Prefers `tracesSampler`. If it throws, falls back to the parent decision, then `tracesSampleRate`. * Returns `undefined` when there is nothing to fall back to. */ function resolveSampleRate(options, samplingContext) { const { tracesSampler, tracesSampleRate } = options; if (typeof tracesSampler === "function") { const samplerResult = safeCallback(DEBUG_BUILD ? "The `tracesSampler` callback threw an error, falling back to the parent sampling decision or `tracesSampleRate`:" : "", () => [tracesSampler({ ...samplingContext, inheritOrSampleWith: (fallbackSampleRate) => { if (typeof samplingContext.parentSampleRate === "number") return samplingContext.parentSampleRate; if (typeof samplingContext.parentSampled === "boolean") return Number(samplingContext.parentSampled); return fallbackSampleRate; } }), true], () => void 0); if (samplerResult) return samplerResult; } if (samplingContext.parentSampled !== void 0) return [samplingContext.parentSampled]; if (typeof tracesSampleRate !== "undefined") return [tracesSampleRate, true]; } export { sampleSpan }; //# sourceMappingURL=sampling.js.map