|
| 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 | + * NOTICE from the Sentry authors: |
| 17 | + * - Minimal vendored implementation of `TraceState` from `@opentelemetry/core` |
| 18 | + * to avoid pulling in that dependency for a single class. |
| 19 | + * - Drops raw-string parsing and key/value validation, neither of which are |
| 20 | + * used by the SDK — the W3C `tracestate` header is parsed by OTel's own |
| 21 | + * propagators (which use their own `TraceState`), and every key we `set` |
| 22 | + * is a known constant. |
| 23 | + */ |
| 24 | +import type { TraceState as TraceStateInterface } from '@opentelemetry/api'; |
| 25 | + |
| 26 | +/** |
| 27 | + * Minimal implementation of the W3C `tracestate` field as a `@opentelemetry/api` |
| 28 | + * `TraceState`. New entries are inserted at the front of the list, and updating |
| 29 | + * an existing key moves it to the front. |
| 30 | + * |
| 31 | + * See https://www.w3.org/TR/trace-context/#tracestate-field for the field spec. |
| 32 | + */ |
| 33 | +export class TraceState implements TraceStateInterface { |
| 34 | + private _internalState: Map<string, string> = new Map(); |
| 35 | + |
| 36 | + /** @inheritDoc */ |
| 37 | + public set(key: string, value: string): TraceState { |
| 38 | + const next = this._clone(); |
| 39 | + if (next._internalState.has(key)) { |
| 40 | + next._internalState.delete(key); |
| 41 | + } |
| 42 | + next._internalState.set(key, value); |
| 43 | + return next; |
| 44 | + } |
| 45 | + |
| 46 | + /** @inheritDoc */ |
| 47 | + public unset(key: string): TraceState { |
| 48 | + const next = this._clone(); |
| 49 | + next._internalState.delete(key); |
| 50 | + return next; |
| 51 | + } |
| 52 | + |
| 53 | + /** @inheritDoc */ |
| 54 | + public get(key: string): string | undefined { |
| 55 | + return this._internalState.get(key); |
| 56 | + } |
| 57 | + |
| 58 | + /** @inheritDoc */ |
| 59 | + public serialize(): string { |
| 60 | + return Array.from(this._internalState.keys()) |
| 61 | + .reverse() |
| 62 | + .map(key => `${key}=${this._internalState.get(key)}`) |
| 63 | + .join(','); |
| 64 | + } |
| 65 | + |
| 66 | + private _clone(): TraceState { |
| 67 | + const next = new TraceState(); |
| 68 | + next._internalState = new Map(this._internalState); |
| 69 | + return next; |
| 70 | + } |
| 71 | +} |
0 commit comments