( options: Pick<CoreOptions, 'tracesSampleRate' | 'tracesSampler'>, samplingContext: SamplingContext, sampleRand: number, )
| 12 | * sent to Sentry. |
| 13 | */ |
| 14 | export function sampleSpan( |
| 15 | options: Pick<CoreOptions, 'tracesSampleRate' | 'tracesSampler'>, |
| 16 | samplingContext: SamplingContext, |
| 17 | sampleRand: number, |
| 18 | ): [sampled: boolean, sampleRate?: number, localSampleRateWasApplied?: boolean] { |
| 19 | // nothing to do if span recording is not enabled |
| 20 | if (!hasSpansEnabled(options)) { |
| 21 | return [false]; |
| 22 | } |
| 23 | |
| 24 | let localSampleRateWasApplied = undefined; |
| 25 | |
| 26 | // we would have bailed already if neither `tracesSampler` nor `tracesSampleRate` were defined, so one of these should |
| 27 | // work; prefer the hook if so |
| 28 | let sampleRate; |
| 29 | if (typeof options.tracesSampler === 'function') { |
| 30 | sampleRate = options.tracesSampler({ |
| 31 | ...samplingContext, |
| 32 | inheritOrSampleWith: fallbackSampleRate => { |
| 33 | // If we have an incoming parent sample rate, we'll just use that one. |
| 34 | // The sampling decision will be inherited because of the sample_rand that was generated when the trace reached the incoming boundaries of the SDK. |
| 35 | if (typeof samplingContext.parentSampleRate === 'number') { |
| 36 | return samplingContext.parentSampleRate; |
| 37 | } |
| 38 | |
| 39 | // Fallback if parent sample rate is not on the incoming trace (e.g. if there is no baggage) |
| 40 | // This is to provide backwards compatibility if there are incoming traces from older SDKs that don't send a parent sample rate or a sample rand. In these cases we just want to force either a sampling decision on the downstream traces via the sample rate. |
| 41 | if (typeof samplingContext.parentSampled === 'boolean') { |
| 42 | return Number(samplingContext.parentSampled); |
| 43 | } |
| 44 | |
| 45 | return fallbackSampleRate; |
| 46 | }, |
| 47 | }); |
| 48 | localSampleRateWasApplied = true; |
| 49 | } else if (samplingContext.parentSampled !== undefined) { |
| 50 | sampleRate = samplingContext.parentSampled; |
| 51 | } else if (typeof options.tracesSampleRate !== 'undefined') { |
| 52 | sampleRate = options.tracesSampleRate; |
| 53 | localSampleRateWasApplied = true; |
| 54 | } |
| 55 | |
| 56 | // Since this is coming from the user (or from a function provided by the user), who knows what we might get. |
| 57 | // (The only valid values are booleans or numbers between 0 and 1.) |
| 58 | const parsedSampleRate = parseSampleRate(sampleRate); |
| 59 | |
| 60 | if (parsedSampleRate === undefined) { |
| 61 | DEBUG_BUILD && |
| 62 | debug.warn( |
| 63 | `[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( |
| 64 | sampleRate, |
| 65 | )} of type ${JSON.stringify(typeof sampleRate)}.`, |
| 66 | ); |
| 67 | return [false]; |
| 68 | } |
| 69 | |
| 70 | // if the function returned 0 (or false), or if `tracesSampleRate` is 0, it's a sign the transaction should be dropped |
| 71 | if (!parsedSampleRate) { |
no test coverage detected