UNPKG

@sentry/core

Version:
60 lines (57 loc) 2.3 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'; function sampleSpan(options, samplingContext, sampleRand) { if (!hasSpansEnabled(options)) { return [false]; } let localSampleRateWasApplied = void 0; let sampleRate; if (typeof options.tracesSampler === "function") { sampleRate = options.tracesSampler({ ...samplingContext, inheritOrSampleWith: (fallbackSampleRate) => { if (typeof samplingContext.parentSampleRate === "number") { return samplingContext.parentSampleRate; } if (typeof samplingContext.parentSampled === "boolean") { return Number(samplingContext.parentSampled); } return fallbackSampleRate; } }); localSampleRateWasApplied = true; } else if (samplingContext.parentSampled !== void 0) { sampleRate = samplingContext.parentSampled; } else if (typeof options.tracesSampleRate !== "undefined") { sampleRate = options.tracesSampleRate; localSampleRateWasApplied = true; } 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]; } export { sampleSpan }; //# sourceMappingURL=sampling.js.map