1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.B3Format = exports.NOT_SAMPLED_VALUE = exports.SAMPLED_VALUE = exports.X_B3_SAMPLED = exports.X_B3_PARENT_SPAN_ID = exports.X_B3_SPAN_ID = exports.X_B3_TRACE_ID = void 0;
|
19 | const crypto = require("crypto");
|
20 | const uuid = require("uuid");
|
21 | const validators_1 = require("./validators");
|
22 | exports.X_B3_TRACE_ID = 'x-b3-traceid';
|
23 | exports.X_B3_SPAN_ID = 'x-b3-spanid';
|
24 | exports.X_B3_PARENT_SPAN_ID = 'x-b3-parentspanid';
|
25 | exports.X_B3_SAMPLED = 'x-b3-sampled';
|
26 | exports.SAMPLED_VALUE = 0x1;
|
27 | exports.NOT_SAMPLED_VALUE = 0x0;
|
28 |
|
29 | class B3Format {
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 | extract(getter) {
|
36 | const traceId = this.parseHeader(getter.getHeader(exports.X_B3_TRACE_ID));
|
37 | const spanId = this.parseHeader(getter.getHeader(exports.X_B3_SPAN_ID));
|
38 | const opt = this.parseHeader(getter.getHeader(exports.X_B3_SAMPLED));
|
39 | if (traceId && spanId) {
|
40 | return {
|
41 | traceId,
|
42 | spanId,
|
43 | options: isNaN(Number(opt)) ? exports.NOT_SAMPLED_VALUE : Number(opt),
|
44 | };
|
45 | }
|
46 | return null;
|
47 | }
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 | inject(setter, spanContext) {
|
54 | if (!spanContext ||
|
55 | !validators_1.isValidTraceId(spanContext.traceId) ||
|
56 | !validators_1.isValidSpanId(spanContext.spanId)) {
|
57 | return;
|
58 | }
|
59 | setter.setHeader(exports.X_B3_TRACE_ID, spanContext.traceId);
|
60 | setter.setHeader(exports.X_B3_SPAN_ID, spanContext.spanId);
|
61 | if (((spanContext.options || exports.NOT_SAMPLED_VALUE) & exports.SAMPLED_VALUE) !== 0) {
|
62 | setter.setHeader(exports.X_B3_SAMPLED, `${exports.SAMPLED_VALUE}`);
|
63 | }
|
64 | else {
|
65 | setter.setHeader(exports.X_B3_SAMPLED, `${exports.NOT_SAMPLED_VALUE}`);
|
66 | }
|
67 | }
|
68 | |
69 |
|
70 |
|
71 | generate() {
|
72 | return {
|
73 | traceId: uuid
|
74 | .v4()
|
75 | .split('-')
|
76 | .join(''),
|
77 | spanId: crypto.randomBytes(8).toString('hex'),
|
78 | options: exports.SAMPLED_VALUE,
|
79 | };
|
80 | }
|
81 |
|
82 | parseHeader(str) {
|
83 | if (Array.isArray(str)) {
|
84 | return str[0];
|
85 | }
|
86 | return str;
|
87 | }
|
88 | }
|
89 | exports.B3Format = B3Format;
|
90 |
|
\ | No newline at end of file |