{"version":3,"file":"TraceIdRatioBasedSampler.cjs","names":["isValidTraceId"],"sources":["../../src/sampler/TraceIdRatioBasedSampler.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { isValidTraceId } from '@opentelemetry/api';\nimport type { Sampler, SamplingResult } from '../Sampler';\nimport { SamplingDecision } from '../Sampler';\n\n/** Sampler that samples a given fraction of traces based of trace id deterministically. */\nexport class TraceIdRatioBasedSampler implements Sampler {\n  private readonly _ratio;\n  private _upperBound: number;\n\n  constructor(ratio = 0) {\n    this._ratio = this._normalize(ratio);\n    this._upperBound =\n      this._ratio === 1 ? 0x100000000 : Math.floor(this._ratio * 0xffffffff);\n  }\n\n  shouldSample(context: unknown, traceId: string): SamplingResult {\n    return {\n      decision:\n        isValidTraceId(traceId) && this._accumulate(traceId) < this._upperBound\n          ? SamplingDecision.RECORD_AND_SAMPLED\n          : SamplingDecision.NOT_RECORD,\n    };\n  }\n\n  toString(): string {\n    return `TraceIdRatioBased{${this._ratio}}`;\n  }\n\n  private _normalize(ratio: number): number {\n    if (typeof ratio !== 'number' || isNaN(ratio)) return 0;\n    return ratio >= 1 ? 1 : ratio <= 0 ? 0 : ratio;\n  }\n\n  private _accumulate(traceId: string): number {\n    let accumulation = 0;\n    for (let i = 0; i < 32; i += 8) {\n      let part = 0;\n      for (let j = 0; j < 8; j++) {\n        const c = traceId.charCodeAt(i + j);\n        // Convert hex char code to value: '0'-'9' -> 0-9, 'a'-'f' -> 10-15, 'A'-'F' -> 10-15\n        const v = c < 58 ? c - 48 : c < 71 ? c - 55 : c - 87;\n        part = (part << 4) | v;\n      }\n      accumulation = (accumulation ^ part) >>> 0;\n    }\n    return accumulation;\n  }\n}\n"],"mappings":";;;;AAUA,IAAa,2BAAb,MAAyD;CACvD;CACA;CAEA,YAAY,QAAQ,GAAG;EACrB,KAAK,SAAS,KAAK,WAAW,KAAK;EACnC,KAAK,cACH,KAAK,WAAW,IAAI,aAAc,KAAK,MAAM,KAAK,SAAS,UAAU;CACzE;CAEA,aAAa,SAAkB,SAAiC;EAC9D,OAAO,EACL,WAAA,GACEA,mBAAAA,eAAAA,CAAe,OAAO,KAAK,KAAK,YAAY,OAAO,IAAI,KAAK,cAAA,IAAA,EAGhE;CACF;CAEA,WAAmB;EACjB,OAAO,qBAAqB,KAAK,OAAO;CAC1C;CAEA,WAAmB,OAAuB;EACxC,IAAI,OAAO,UAAU,YAAY,MAAM,KAAK,GAAG,OAAO;EACtD,OAAO,SAAS,IAAI,IAAI,SAAS,IAAI,IAAI;CAC3C;CAEA,YAAoB,SAAyB;EAC3C,IAAI,eAAe;EACnB,KAAK,IAAI,IAAI,GAAG,IAAI,IAAI,KAAK,GAAG;GAC9B,IAAI,OAAO;GACX,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK;IAC1B,MAAM,IAAI,QAAQ,WAAW,IAAI,CAAC;IAElC,MAAM,IAAI,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI;IAClD,OAAQ,QAAQ,IAAK;GACvB;GACA,gBAAgB,eAAe,UAAU;EAC3C;EACA,OAAO;CACT;AACF"}