@multiplayer-app/otlp-core
Version:
Multiplayer otlp core
58 lines • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultiplayerIdGenerator = void 0;
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
const type_1 = require("./type");
const MultiplayerTraceIdRatioBasedSampler_1 = require("./MultiplayerTraceIdRatioBasedSampler");
const constants_base_1 = require("./constants.base");
const SHARED_CHAR_CODES_ARRAY = Array(32);
function getIdGenerator(bytes) {
return function generateId() {
for (let i = 0; i < bytes * 2; i++) {
SHARED_CHAR_CODES_ARRAY[i] = Math.floor(Math.random() * 16) + 48;
// valid hex characters in the range 48-57 and 97-102
if (SHARED_CHAR_CODES_ARRAY[i] >= 58) {
SHARED_CHAR_CODES_ARRAY[i] += 39;
}
}
return String.fromCharCode.apply(null, SHARED_CHAR_CODES_ARRAY.slice(0, bytes * 2));
};
}
class MultiplayerIdGenerator extends sdk_trace_base_1.RandomIdGenerator {
constructor({ autoDocTracesRatio = 0 }) {
super();
this.docSpanSampler = new MultiplayerTraceIdRatioBasedSampler_1.MultiplayerTraceIdRatioBasedSampler(autoDocTracesRatio);
this.generateLongId = getIdGenerator(16);
this.debugSessionShortId = '';
this.debugSessionType = type_1.DebugSessionType.PLAIN;
this.generateTraceId = () => {
const traceId = this.generateLongId();
if (this.debugSessionShortId) {
let debugSessionTypePrefix = '';
switch (this.debugSessionType) {
case type_1.DebugSessionType.CONTINUOUS:
debugSessionTypePrefix = constants_base_1.MULTIPLAYER_TRACE_CONTINUOUS_DEBUG_PREFIX;
break;
default:
debugSessionTypePrefix = constants_base_1.MULTIPLAYER_TRACE_DEBUG_PREFIX;
}
const prefix = `${debugSessionTypePrefix}${this.debugSessionShortId}`;
const debugSessionTraceId = `${prefix}${traceId.substring(prefix.length, traceId.length)}`;
return debugSessionTraceId;
}
else if (this._isDocTrace(traceId)) {
return `${constants_base_1.MULTIPLAYER_TRACE_DOC_PREFIX}${traceId.slice(constants_base_1.MULTIPLAYER_TRACE_DOC_PREFIX.length, traceId.length)}`;
}
return traceId;
};
}
setSessionId(debugSessionShortId, debugSessionType = type_1.DebugSessionType.PLAIN) {
this.debugSessionShortId = debugSessionShortId;
this.debugSessionType = debugSessionType;
}
_isDocTrace(traceId) {
return this.docSpanSampler.shouldSample(undefined, traceId).decision === sdk_trace_base_1.SamplingDecision.RECORD_AND_SAMPLED;
}
}
exports.MultiplayerIdGenerator = MultiplayerIdGenerator;
//# sourceMappingURL=MultiplayerIdGenerator.js.map