UNPKG

1.36 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.RandomIdGenerator = void 0;
4const SPAN_ID_BYTES = 8;
5const TRACE_ID_BYTES = 16;
6/**
7 * @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead.
8 */
9class RandomIdGenerator {
10 constructor() {
11 /**
12 * Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
13 * characters corresponding to 128 bits.
14 */
15 this.generateTraceId = getIdGenerator(TRACE_ID_BYTES);
16 /**
17 * Returns a random 8-byte span ID formatted/encoded as a 16 lowercase hex
18 * characters corresponding to 64 bits.
19 */
20 this.generateSpanId = getIdGenerator(SPAN_ID_BYTES);
21 }
22}
23exports.RandomIdGenerator = RandomIdGenerator;
24const SHARED_CHAR_CODES_ARRAY = Array(32);
25function getIdGenerator(bytes) {
26 return function generateId() {
27 for (let i = 0; i < bytes * 2; i++) {
28 SHARED_CHAR_CODES_ARRAY[i] = Math.floor(Math.random() * 16) + 48;
29 // valid hex characters in the range 48-57 and 97-102
30 if (SHARED_CHAR_CODES_ARRAY[i] >= 58) {
31 SHARED_CHAR_CODES_ARRAY[i] += 39;
32 }
33 }
34 return String.fromCharCode.apply(null, SHARED_CHAR_CODES_ARRAY.slice(0, bytes * 2));
35 };
36}
37//# sourceMappingURL=RandomIdGenerator.js.map
\No newline at end of file