UNPKG

4.32 kBJavaScriptView Raw
1"use strict";
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 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.TraceStateImpl = void 0;
19var tracestate_validators_1 = require("./tracestate-validators");
20var MAX_TRACE_STATE_ITEMS = 32;
21var MAX_TRACE_STATE_LEN = 512;
22var LIST_MEMBERS_SEPARATOR = ',';
23var LIST_MEMBER_KEY_VALUE_SPLITTER = '=';
24/**
25 * TraceState must be a class and not a simple object type because of the spec
26 * requirement (https://www.w3.org/TR/trace-context/#tracestate-field).
27 *
28 * Here is the list of allowed mutations:
29 * - New key-value pair should be added into the beginning of the list
30 * - The value of any key can be updated. Modified keys MUST be moved to the
31 * beginning of the list.
32 */
33var TraceStateImpl = /** @class */ (function () {
34 function TraceStateImpl(rawTraceState) {
35 this._internalState = new Map();
36 if (rawTraceState)
37 this._parse(rawTraceState);
38 }
39 TraceStateImpl.prototype.set = function (key, value) {
40 // TODO: Benchmark the different approaches(map vs list) and
41 // use the faster one.
42 var traceState = this._clone();
43 if (traceState._internalState.has(key)) {
44 traceState._internalState.delete(key);
45 }
46 traceState._internalState.set(key, value);
47 return traceState;
48 };
49 TraceStateImpl.prototype.unset = function (key) {
50 var traceState = this._clone();
51 traceState._internalState.delete(key);
52 return traceState;
53 };
54 TraceStateImpl.prototype.get = function (key) {
55 return this._internalState.get(key);
56 };
57 TraceStateImpl.prototype.serialize = function () {
58 var _this = this;
59 return this._keys()
60 .reduce(function (agg, key) {
61 agg.push(key + LIST_MEMBER_KEY_VALUE_SPLITTER + _this.get(key));
62 return agg;
63 }, [])
64 .join(LIST_MEMBERS_SEPARATOR);
65 };
66 TraceStateImpl.prototype._parse = function (rawTraceState) {
67 if (rawTraceState.length > MAX_TRACE_STATE_LEN)
68 return;
69 this._internalState = rawTraceState
70 .split(LIST_MEMBERS_SEPARATOR)
71 .reverse() // Store in reverse so new keys (.set(...)) will be placed at the beginning
72 .reduce(function (agg, part) {
73 var listMember = part.trim(); // Optional Whitespace (OWS) handling
74 var i = listMember.indexOf(LIST_MEMBER_KEY_VALUE_SPLITTER);
75 if (i !== -1) {
76 var key = listMember.slice(0, i);
77 var value = listMember.slice(i + 1, part.length);
78 if (tracestate_validators_1.validateKey(key) && tracestate_validators_1.validateValue(value)) {
79 agg.set(key, value);
80 }
81 else {
82 // TODO: Consider to add warning log
83 }
84 }
85 return agg;
86 }, new Map());
87 // Because of the reverse() requirement, trunc must be done after map is created
88 if (this._internalState.size > MAX_TRACE_STATE_ITEMS) {
89 this._internalState = new Map(Array.from(this._internalState.entries())
90 .reverse() // Use reverse same as original tracestate parse chain
91 .slice(0, MAX_TRACE_STATE_ITEMS));
92 }
93 };
94 TraceStateImpl.prototype._keys = function () {
95 return Array.from(this._internalState.keys()).reverse();
96 };
97 TraceStateImpl.prototype._clone = function () {
98 var traceState = new TraceStateImpl();
99 traceState._internalState = new Map(this._internalState);
100 return traceState;
101 };
102 return TraceStateImpl;
103}());
104exports.TraceStateImpl = TraceStateImpl;
105//# sourceMappingURL=tracestate-impl.js.map
\No newline at end of file