tracestate-impl.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. */
  16. import { validateKey, validateValue } from './tracestate-validators';
  17. const MAX_TRACE_STATE_ITEMS = 32;
  18. const MAX_TRACE_STATE_LEN = 512;
  19. const LIST_MEMBERS_SEPARATOR = ',';
  20. const 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. */
  30. export class TraceStateImpl {
  31. constructor(rawTraceState) {
  32. this._internalState = new Map();
  33. if (rawTraceState)
  34. this._parse(rawTraceState);
  35. }
  36. set(key, value) {
  37. // TODO: Benchmark the different approaches(map vs list) and
  38. // use the faster one.
  39. const 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. unset(key) {
  47. const traceState = this._clone();
  48. traceState._internalState.delete(key);
  49. return traceState;
  50. }
  51. get(key) {
  52. return this._internalState.get(key);
  53. }
  54. serialize() {
  55. return this._keys()
  56. .reduce((agg, key) => {
  57. agg.push(key + LIST_MEMBER_KEY_VALUE_SPLITTER + this.get(key));
  58. return agg;
  59. }, [])
  60. .join(LIST_MEMBERS_SEPARATOR);
  61. }
  62. _parse(rawTraceState) {
  63. if (rawTraceState.length > MAX_TRACE_STATE_LEN)
  64. return;
  65. this._internalState = rawTraceState
  66. .split(LIST_MEMBERS_SEPARATOR)
  67. .reverse() // Store in reverse so new keys (.set(...)) will be placed at the beginning
  68. .reduce((agg, part) => {
  69. const listMember = part.trim(); // Optional Whitespace (OWS) handling
  70. const i = listMember.indexOf(LIST_MEMBER_KEY_VALUE_SPLITTER);
  71. if (i !== -1) {
  72. const key = listMember.slice(0, i);
  73. const value = listMember.slice(i + 1, part.length);
  74. if (validateKey(key) && validateValue(value)) {
  75. agg.set(key, value);
  76. }
  77. else {
  78. // TODO: Consider to add warning log
  79. }
  80. }
  81. return agg;
  82. }, new Map());
  83. // Because of the reverse() requirement, trunc must be done after map is created
  84. if (this._internalState.size > MAX_TRACE_STATE_ITEMS) {
  85. this._internalState = new Map(Array.from(this._internalState.entries())
  86. .reverse() // Use reverse same as original tracestate parse chain
  87. .slice(0, MAX_TRACE_STATE_ITEMS));
  88. }
  89. }
  90. _keys() {
  91. return Array.from(this._internalState.keys()).reverse();
  92. }
  93. _clone() {
  94. const traceState = new TraceStateImpl();
  95. traceState._internalState = new Map(this._internalState);
  96. return traceState;
  97. }
  98. }
  99. //# sourceMappingURL=tracestate-impl.js.map