{"version":3,"file":"sampling.js","sources":["../../../src/tracing/sampling.ts"],"sourcesContent":["import type { Options, SamplingContext } from '@sentry/types';\nimport { logger } from '@sentry/utils';\n\nimport { DEBUG_BUILD } from '../debug-build';\nimport { hasTracingEnabled } from '../utils/hasTracingEnabled';\nimport { parseSampleRate } from '../utils/parseSampleRate';\n\n/**\n * Makes a sampling decision for the given options.\n *\n * Called every time a root span is created. Only root spans which emerge with a `sampled` value of `true` will be\n * sent to Sentry.\n */\nexport function sampleSpan(\n options: Pick,\n samplingContext: SamplingContext,\n): [sampled: boolean, sampleRate?: number] {\n // nothing to do if tracing is not enabled\n if (!hasTracingEnabled(options)) {\n return [false];\n }\n\n // we would have bailed already if neither `tracesSampler` nor `tracesSampleRate` nor `enableTracing` were defined, so one of these should\n // work; prefer the hook if so\n let sampleRate;\n if (typeof options.tracesSampler === 'function') {\n sampleRate = options.tracesSampler(samplingContext);\n } else if (samplingContext.parentSampled !== undefined) {\n sampleRate = samplingContext.parentSampled;\n } else if (typeof options.tracesSampleRate !== 'undefined') {\n sampleRate = options.tracesSampleRate;\n } else {\n // When `enableTracing === true`, we use a sample rate of 100%\n sampleRate = 1;\n }\n\n // Since this is coming from the user (or from a function provided by the user), who knows what we might get.\n // (The only valid values are booleans or numbers between 0 and 1.)\n const parsedSampleRate = parseSampleRate(sampleRate);\n\n if (parsedSampleRate === undefined) {\n DEBUG_BUILD && logger.warn('[Tracing] Discarding transaction because of invalid sample rate.');\n return [false];\n }\n\n // if the function returned 0 (or false), or if `tracesSampleRate` is 0, it's a sign the transaction should be dropped\n if (!parsedSampleRate) {\n DEBUG_BUILD &&\n logger.log(\n `[Tracing] Discarding transaction because ${\n typeof options.tracesSampler === 'function'\n ? 'tracesSampler returned 0 or false'\n : 'a negative sampling decision was inherited or tracesSampleRate is set to 0'\n }`,\n );\n return [false, parsedSampleRate];\n }\n\n // Now we roll the dice. Math.random is inclusive of 0, but not of 1, so strict < is safe here. In case sampleRate is\n // a boolean, the < comparison will cause it to be automatically cast to 1 if it's true and 0 if it's false.\n const shouldSample = Math.random() < parsedSampleRate;\n\n // if we're not going to keep it, we're done\n if (!shouldSample) {\n DEBUG_BUILD &&\n logger.log(\n `[Tracing] Discarding transaction because it's not included in the random sample (sampling rate = ${Number(\n sampleRate,\n )})`,\n );\n return [false, parsedSampleRate];\n }\n\n return [true, parsedSampleRate];\n}\n"],"names":["hasTracingEnabled","parseSampleRate","DEBUG_BUILD","logger"],"mappings":";;;;;;;AAOA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,UAAU;AAC1B,EAAE,OAAO;AACT,EAAE,eAAe;AACjB,EAA2C;AAC3C;AACA,EAAE,IAAI,CAACA,mCAAiB,CAAC,OAAO,CAAC,EAAE;AACnC,IAAI,OAAO,CAAC,KAAK,CAAC;AAClB;;AAEA;AACA;AACA,EAAE,IAAI,UAAU;AAChB,EAAE,IAAI,OAAO,OAAO,CAAC,aAAc,KAAI,UAAU,EAAE;AACnD,IAAI,aAAa,OAAO,CAAC,aAAa,CAAC,eAAe,CAAC;AACvD,GAAE,MAAO,IAAI,eAAe,CAAC,aAAA,KAAkB,SAAS,EAAE;AAC1D,IAAI,UAAW,GAAE,eAAe,CAAC,aAAa;AAC9C,GAAE,MAAO,IAAI,OAAO,OAAO,CAAC,gBAAA,KAAqB,WAAW,EAAE;AAC9D,IAAI,UAAW,GAAE,OAAO,CAAC,gBAAgB;AACzC,SAAS;AACT;AACA,IAAI,UAAA,GAAa,CAAC;AAClB;;AAEA;AACA;AACA,EAAE,MAAM,gBAAiB,GAAEC,+BAAe,CAAC,UAAU,CAAC;;AAEtD,EAAE,IAAI,gBAAiB,KAAI,SAAS,EAAE;AACtC,IAAIC,0BAAeC,YAAM,CAAC,IAAI,CAAC,kEAAkE,CAAC;AAClG,IAAI,OAAO,CAAC,KAAK,CAAC;AAClB;;AAEA;AACA,EAAE,IAAI,CAAC,gBAAgB,EAAE;AACzB,IAAID,sBAAY;AAChB,MAAMC,YAAM,CAAC,GAAG;A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