1 | ;
|
2 | /*
|
3 | * Copyright The OpenTelemetry Authors
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * https://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.RandomIdGenerator = void 0;
|
19 | const SPAN_ID_BYTES = 8;
|
20 | const TRACE_ID_BYTES = 16;
|
21 | /**
|
22 | * @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead.
|
23 | */
|
24 | class RandomIdGenerator {
|
25 | constructor() {
|
26 | /**
|
27 | * Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
|
28 | * characters corresponding to 128 bits.
|
29 | */
|
30 | this.generateTraceId = getIdGenerator(TRACE_ID_BYTES);
|
31 | /**
|
32 | * Returns a random 8-byte span ID formatted/encoded as a 16 lowercase hex
|
33 | * characters corresponding to 64 bits.
|
34 | */
|
35 | this.generateSpanId = getIdGenerator(SPAN_ID_BYTES);
|
36 | }
|
37 | }
|
38 | exports.RandomIdGenerator = RandomIdGenerator;
|
39 | const SHARED_BUFFER = Buffer.allocUnsafe(TRACE_ID_BYTES);
|
40 | function getIdGenerator(bytes) {
|
41 | return function generateId() {
|
42 | for (let i = 0; i < bytes / 4; i++) {
|
43 | // unsigned right shift drops decimal part of the number
|
44 | // it is required because if a number between 2**32 and 2**32 - 1 is generated, an out of range error is thrown by writeUInt32BE
|
45 | SHARED_BUFFER.writeUInt32BE((Math.random() * 2 ** 32) >>> 0, i * 4);
|
46 | }
|
47 | // If buffer is all 0, set the last byte to 1 to guarantee a valid w3c id is generated
|
48 | for (let i = 0; i < bytes; i++) {
|
49 | if (SHARED_BUFFER[i] > 0) {
|
50 | break;
|
51 | }
|
52 | else if (i === bytes - 1) {
|
53 | SHARED_BUFFER[bytes - 1] = 1;
|
54 | }
|
55 | }
|
56 | return SHARED_BUFFER.toString('hex', 0, bytes);
|
57 | };
|
58 | }
|
59 | //# sourceMappingURL=RandomIdGenerator.js.map |
\ | No newline at end of file |