@multiplayer-app/otlp-core
Version:
Multiplayer otlp core
75 lines • 3.91 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { RandomIdGenerator, SamplingDecision, } from '@opentelemetry/sdk-trace-base';
import { DebugSessionType } from './type';
import { MultiplayerTraceIdRatioBasedSampler } from './MultiplayerTraceIdRatioBasedSampler';
import { MULTIPLAYER_TRACE_DEBUG_PREFIX, MULTIPLAYER_TRACE_DOC_PREFIX, MULTIPLAYER_TRACE_CONTINUOUS_DEBUG_PREFIX, } from './constants.base';
var SHARED_CHAR_CODES_ARRAY = Array(32);
function getIdGenerator(bytes) {
return function generateId() {
for (var 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));
};
}
var MultiplayerIdGenerator = /** @class */ (function (_super) {
__extends(MultiplayerIdGenerator, _super);
function MultiplayerIdGenerator(_a) {
var _b = _a.autoDocTracesRatio, autoDocTracesRatio = _b === void 0 ? 0 : _b;
var _this = _super.call(this) || this;
_this.docSpanSampler = new MultiplayerTraceIdRatioBasedSampler(autoDocTracesRatio);
_this.generateLongId = getIdGenerator(16);
_this.debugSessionShortId = '';
_this.debugSessionType = DebugSessionType.PLAIN;
_this.generateTraceId = function () {
var traceId = _this.generateLongId();
if (_this.debugSessionShortId) {
var debugSessionTypePrefix = '';
switch (_this.debugSessionType) {
case DebugSessionType.CONTINUOUS:
debugSessionTypePrefix = MULTIPLAYER_TRACE_CONTINUOUS_DEBUG_PREFIX;
break;
default:
debugSessionTypePrefix = MULTIPLAYER_TRACE_DEBUG_PREFIX;
}
var prefix = "".concat(debugSessionTypePrefix).concat(_this.debugSessionShortId);
var debugSessionTraceId = "".concat(prefix).concat(traceId.substring(prefix.length, traceId.length));
return debugSessionTraceId;
}
else if (_this._isDocTrace(traceId)) {
return "".concat(MULTIPLAYER_TRACE_DOC_PREFIX).concat(traceId.slice(MULTIPLAYER_TRACE_DOC_PREFIX.length, traceId.length));
}
return traceId;
};
return _this;
}
MultiplayerIdGenerator.prototype.setSessionId = function (debugSessionShortId, debugSessionType) {
if (debugSessionType === void 0) { debugSessionType = DebugSessionType.PLAIN; }
this.debugSessionShortId = debugSessionShortId;
this.debugSessionType = debugSessionType;
};
MultiplayerIdGenerator.prototype._isDocTrace = function (traceId) {
return this.docSpanSampler.shouldSample(undefined, traceId).decision === SamplingDecision.RECORD_AND_SAMPLED;
};
return MultiplayerIdGenerator;
}(RandomIdGenerator));
export { MultiplayerIdGenerator };
//# sourceMappingURL=MultiplayerIdGenerator.js.map