UNPKG

4.15 kBJavaScriptView Raw
1/*
2 * Copyright The OpenTelemetry Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { validateKey, validateValue } from './tracestate-validators';
17var MAX_TRACE_STATE_ITEMS = 32;
18var MAX_TRACE_STATE_LEN = 512;
19var LIST_MEMBERS_SEPARATOR = ',';
20var LIST_MEMBER_KEY_VALUE_SPLITTER = '=';
21/**
22 * TraceState must be a class and not a simple object type because of the spec
23 * requirement (https://www.w3.org/TR/trace-context/#tracestate-field).
24 *
25 * Here is the list of allowed mutations:
26 * - New key-value pair should be added into the beginning of the list
27 * - The value of any key can be updated. Modified keys MUST be moved to the
28 * beginning of the list.
29 */
30var TraceStateImpl = /** @class */ (function () {
31 function TraceStateImpl(rawTraceState) {
32 this._internalState = new Map();
33 if (rawTraceState)
34 this._parse(rawTraceState);
35 }
36 TraceStateImpl.prototype.set = function (key, value) {
37 // TODO: Benchmark the different approaches(map vs list) and
38 // use the faster one.
39 var traceState = this._clone();
40 if (traceState._internalState.has(key)) {
41 traceState._internalState.delete(key);
42 }
43 traceState._internalState.set(key, value);
44 return traceState;
45 };
46 TraceStateImpl.prototype.unset = function (key) {
47 var traceState = this._clone();
48 traceState._internalState.delete(key);
49 return traceState;
50 };
51 TraceStateImpl.prototype.get = function (key) {
52 return this._internalState.get(key);
53 };
54 TraceStateImpl.prototype.serialize = function () {
55 var _this = this;
56 return this._keys()
57 .reduce(function (agg, key) {
58 agg.push(key + LIST_MEMBER_KEY_VALUE_SPLITTER + _this.get(key));
59 return agg;
60 }, [])
61 .join(LIST_MEMBERS_SEPARATOR);
62 };
63 TraceStateImpl.prototype._parse = function (rawTraceState) {
64 if (rawTraceState.length > MAX_TRACE_STATE_LEN)
65 return;
66 this._internalState = rawTraceState
67 .split(LIST_MEMBERS_SEPARATOR)
68 .reverse() // Store in reverse so new keys (.set(...)) will be placed at the beginning
69 .reduce(function (agg, part) {
70 var listMember = part.trim(); // Optional Whitespace (OWS) handling
71 var i = listMember.indexOf(LIST_MEMBER_KEY_VALUE_SPLITTER);
72 if (i !== -1) {
73 var key = listMember.slice(0, i);
74 var value = listMember.slice(i + 1, part.length);
75 if (validateKey(key) && validateValue(value)) {
76 agg.set(key, value);
77 }
78 else {
79 // TODO: Consider to add warning log
80 }
81 }
82 return agg;
83 }, new Map());
84 // Because of the reverse() requirement, trunc must be done after map is created
85 if (this._internalState.size > MAX_TRACE_STATE_ITEMS) {
86 this._internalState = new Map(Array.from(this._internalState.entries())
87 .reverse() // Use reverse same as original tracestate parse chain
88 .slice(0, MAX_TRACE_STATE_ITEMS));
89 }
90 };
91 TraceStateImpl.prototype._keys = function () {
92 return Array.from(this._internalState.keys()).reverse();
93 };
94 TraceStateImpl.prototype._clone = function () {
95 var traceState = new TraceStateImpl();
96 traceState._internalState = new Map(this._internalState);
97 return traceState;
98 };
99 return TraceStateImpl;
100}());
101export { TraceStateImpl };
102//# sourceMappingURL=tracestate-impl.js.map
\No newline at end of file