UNPKG

@multiplayer-app/session-recorder-common

Version:
45 lines 2.14 kB
import { isValidTraceId } from '@opentelemetry/api'; import { SamplingDecision, } from '@opentelemetry/sdk-trace-base'; import { MULTIPLAYER_TRACE_DEBUG_PREFIX, MULTIPLAYER_TRACE_CONTINUOUS_DEBUG_PREFIX, } from './constants/constants.base'; var SessionRecorderTraceIdRatioBasedSampler = /** @class */ (function () { function SessionRecorderTraceIdRatioBasedSampler(_ratio) { if (_ratio === void 0) { _ratio = 0; } this._ratio = _ratio; this._ratio = this._normalize(_ratio); this._upperBound = Math.floor(this._ratio * 0xffffffff); } SessionRecorderTraceIdRatioBasedSampler.prototype.shouldSample = function (context, traceId) { if (traceId.startsWith(MULTIPLAYER_TRACE_DEBUG_PREFIX) || traceId.startsWith(MULTIPLAYER_TRACE_CONTINUOUS_DEBUG_PREFIX)) { return { decision: SamplingDecision.RECORD_AND_SAMPLED, }; } var decision = SamplingDecision.NOT_RECORD; if (isValidTraceId(traceId) && this._accumulate(traceId) < this._upperBound) { decision = SamplingDecision.RECORD_AND_SAMPLED; } return { decision: decision }; }; SessionRecorderTraceIdRatioBasedSampler.prototype.toString = function () { return "SessionRecorderTraceIdRatioBasedSampler{".concat(this._ratio, "}"); }; SessionRecorderTraceIdRatioBasedSampler.prototype._normalize = function (ratio) { if (typeof ratio !== 'number' || isNaN(ratio)) return 0; return ratio >= 1 ? 1 : ratio <= 0 ? 0 : ratio; }; SessionRecorderTraceIdRatioBasedSampler.prototype._accumulate = function (traceId) { var accumulation = 0; for (var i = 0; i < traceId.length / 8; i++) { var pos = i * 8; var part = parseInt(traceId.slice(pos, pos + 8), 16); accumulation = (accumulation ^ part) >>> 0; } return accumulation; }; return SessionRecorderTraceIdRatioBasedSampler; }()); export { SessionRecorderTraceIdRatioBasedSampler }; //# sourceMappingURL=SessionRecorderTraceIdRatioBasedSampler.js.map