{"version":3,"file":"sampler.mjs","names":[],"sources":["../../src/domain/sampler.ts"],"sourcesContent":["export function correctedChildSampleRate(parentRate: number, childRate: number): number {\n  return (parentRate * childRate) / 100\n}\n\nconst sampleDecisionCache: Map<number, { sessionId: string; decision: boolean }> = new Map()\n\nexport function isSampled(sessionId: string, sampleRate: number): boolean {\n  // Shortcuts for common cases. This is not strictly necessary, but it makes the code faster for\n  // customers willing to ingest all traces.\n  if (sampleRate === 100) {\n    return true\n  }\n\n  if (sampleRate === 0) {\n    return false\n  }\n\n  const cachedDecision = sampleDecisionCache.get(sampleRate)\n  if (sessionId === cachedDecision?.sessionId) {\n    return cachedDecision.decision\n  }\n\n  const decision = sampleUsingKnuthFactor(BigInt(`0x${sessionId.split('-')[4]}`), sampleRate)\n  sampleDecisionCache.set(sampleRate, { sessionId, decision })\n  return decision\n}\n\n// Exported for tests\nexport function resetSampleDecisionCache() {\n  sampleDecisionCache.clear()\n}\n\n/**\n * Perform sampling using the Knuth factor method. This method offer consistent sampling result\n * based on the provided identifier.\n *\n * @param identifier - The identifier to use for sampling.\n * @param sampleRate - The sample rate in percentage between 0 and 100.\n */\nexport function sampleUsingKnuthFactor(identifier: bigint, sampleRate: number): boolean {\n  // The formula is:\n  //\n  //   (identifier * knuthFactor) % 2^64 < sampleRate * 2^64\n  //\n  // Because JavaScript numbers are 64-bit floats, we can't represent 64-bit integers, and the\n  // modulo would be incorrect. Thus, we are using BigInts here.\n  //\n  // Implementation in other languages:\n  // * Go     https://github.com/DataDog/dd-trace-go/blob/ec6fbb1f2d517b7b8e69961052adf7136f3af773/ddtrace/tracer/sampler.go#L86-L91\n  // * Python https://github.com/DataDog/dd-trace-py/blob/0cee2f066fb6e79aa15947c1514c0f406dea47c5/ddtrace/sampling_rule.py#L197\n  // * Ruby   https://github.com/DataDog/dd-trace-rb/blob/1a6e255cdcb7e7e22235ea5955f90f6dfa91045d/lib/datadog/tracing/sampling/rate_sampler.rb#L42\n  // * C++    https://github.com/DataDog/dd-trace-cpp/blob/159629edc438ae45f2bb318eb7bd51abd05e94b5/src/datadog/trace_sampler.cpp#L58\n  // * Java   https://github.com/DataDog/dd-trace-java/blob/896dd6b380533216e0bdee59614606c8272d313e/dd-trace-core/src/main/java/datadog/trace/common/sampling/DeterministicSampler.java#L48\n  //\n  // Note: All implementations have slight variations. Some of them use '<=' instead of '<', and\n  // use `sampleRate * 2^64 - 1` instead of `sampleRate * 2^64`. The following implementation\n  // should adhere to the spec and is a bit simpler than using a 2^64-1 limit as there are less\n  // BigInt arithmetic to write. In practice this does not matter, as we are using floating point\n  // numbers in the end, and Number(2n**64n-1n) === Number(2n**64n).\n  const knuthFactor = 1111111111111111111n\n  const twoPow64 = 2n ** 64n\n  const hash = (identifier * knuthFactor) % twoPow64\n  return Number(hash) <= (sampleRate / 100) * Number(twoPow64)\n}\n"],"mappings":";AAAA,SAAgB,yBAAyB,YAAoB,WAA2B;CACtF,OAAQ,aAAa,YAAa;AACpC;AAEA,MAAM,sCAA6E,IAAI,IAAI;AAE3F,SAAgB,UAAU,WAAmB,YAA6B;CAGxE,IAAI,eAAe,KACjB,OAAO;CAGT,IAAI,eAAe,GACjB,OAAO;CAGT,MAAM,iBAAiB,oBAAoB,IAAI,UAAU;CACzD,IAAI,cAAc,gBAAgB,WAChC,OAAO,eAAe;CAGxB,MAAM,WAAW,uBAAuB,OAAO,KAAK,UAAU,MAAM,GAAG,CAAC,CAAC,IAAI,GAAG,UAAU;CAC1F,oBAAoB,IAAI,YAAY;EAAE;EAAW;CAAS,CAAC;CAC3D,OAAO;AACT;AAGA,SAAgB,2BAA2B;CACzC,oBAAoB,MAAM;AAC5B;;;;;;;;AASA,SAAgB,uBAAuB,YAAoB,YAA6B;CAoBtF,MAAM,cAAc;CACpB,MAAM,WAAW,MAAM;CACvB,MAAM,OAAQ,aAAa,cAAe;CAC1C,OAAO,OAAO,IAAI,KAAM,aAAa,MAAO,OAAO,QAAQ;AAC7D"}