Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/opentelemetry/src/sampler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable complexity */
import type { Context, Span, TraceState as TraceStateInterface } from '@opentelemetry/api';
import { isSpanContextValid, SpanKind, trace } from '@opentelemetry/api';
import { TraceState } from '@opentelemetry/core';
import { TraceState } from './utils/TraceState';
import type { Sampler, SamplingResult } from '@opentelemetry/sdk-trace-base';
import { SamplingDecision } from '@opentelemetry/sdk-trace-base';
import {
Expand Down
71 changes: 71 additions & 0 deletions packages/opentelemetry/src/utils/TraceState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NOTICE from the Sentry authors:
* - Minimal vendored implementation of `TraceState` from `@opentelemetry/core`
* to avoid pulling in that dependency for a single class.
* - Drops raw-string parsing and key/value validation, neither of which are
* used by the SDK — the W3C `tracestate` header is parsed by OTel's own
* propagators (which use their own `TraceState`), and every key we `set`
* is a known constant.
*/
import type { TraceState as TraceStateInterface } from '@opentelemetry/api';

/**
* Minimal implementation of the W3C `tracestate` field as a `@opentelemetry/api`
* `TraceState`. New entries are inserted at the front of the list, and updating
* an existing key moves it to the front.
*
* See https://www.w3.org/TR/trace-context/#tracestate-field for the field spec.
*/
export class TraceState implements TraceStateInterface {
private _internalState: Map<string, string> = new Map();

/** @inheritDoc */
public set(key: string, value: string): TraceState {
const next = this._clone();
if (next._internalState.has(key)) {
next._internalState.delete(key);
}
next._internalState.set(key, value);
return next;
}

/** @inheritDoc */
public unset(key: string): TraceState {
const next = this._clone();
next._internalState.delete(key);
return next;
}

/** @inheritDoc */
public get(key: string): string | undefined {
return this._internalState.get(key);
}

/** @inheritDoc */
public serialize(): string {
return Array.from(this._internalState.keys())
.reverse()
.map(key => `${key}=${this._internalState.get(key)}`)
.join(',');
}

private _clone(): TraceState {
const next = new TraceState();
next._internalState = new Map(this._internalState);
return next;
}
}
2 changes: 1 addition & 1 deletion packages/opentelemetry/src/utils/makeTraceState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TraceState } from '@opentelemetry/core';
import type { DynamicSamplingContext } from '@sentry/core';
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/core';
import { SENTRY_TRACE_STATE_DSC, SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING } from '../constants';
import { TraceState } from './TraceState';

/**
* Generate a TraceState for the given data.
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry/test/contextManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { context, trace, TraceFlags } from '@opentelemetry/api';
import { TraceState } from '@opentelemetry/core';
import { TraceState } from '../src/utils/TraceState';
import { afterEach, describe, expect, it } from 'vitest';
import { SENTRY_TRACE_STATE_CHILD_IGNORED } from '../src/constants';
import { cleanupOtel, mockSdkInit } from './helpers/mockSdkInit';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SpanContext } from '@opentelemetry/api';
import { context, ROOT_CONTEXT, trace, TraceFlags } from '@opentelemetry/api';
import { TraceState } from '@opentelemetry/core';
import { TraceState } from '../../src/utils/TraceState';
import type { Event, TransactionEvent } from '@sentry/core';
import {
addBreadcrumb,
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry/test/sampler.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { context, SpanKind, trace, TraceFlags } from '@opentelemetry/api';
import { TraceState } from '@opentelemetry/core';
import { TraceState } from '../src/utils/TraceState';
import { SamplingDecision } from '@opentelemetry/sdk-trace-base';
import { ATTR_HTTP_REQUEST_METHOD } from '@opentelemetry/semantic-conventions';
import { generateSpanId, generateTraceId } from '@sentry/core';
Expand Down
44 changes: 44 additions & 0 deletions packages/opentelemetry/test/utils/TraceState.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest';
import { TraceState } from '../../src/utils/TraceState';

describe('TraceState', () => {
it('returns undefined for unknown keys', () => {
expect(new TraceState().get('missing')).toBeUndefined();
});

it('set returns a new instance and leaves the original unchanged', () => {
const original = new TraceState();
const next = original.set('a', '1');

expect(next).not.toBe(original);
expect(original.get('a')).toBeUndefined();
expect(next.get('a')).toBe('1');
});

it('moves an updated key to the front of the serialized list', () => {
const state = new TraceState().set('a', '1').set('b', '2').set('a', '3');

expect(state.get('a')).toBe('3');
expect(state.serialize()).toBe('a=3,b=2');
});

it('serializes newest entries first', () => {
const state = new TraceState().set('a', '1').set('b', '2').set('c', '3');

expect(state.serialize()).toBe('c=3,b=2,a=1');
});

it('unset removes the key and returns a new instance', () => {
const state = new TraceState().set('a', '1').set('b', '2');
const next = state.unset('a');

expect(next).not.toBe(state);
expect(state.get('a')).toBe('1');
expect(next.get('a')).toBeUndefined();
expect(next.serialize()).toBe('b=2');
});

it('serializes an empty state to an empty string', () => {
expect(new TraceState().serialize()).toBe('');
});
});
Loading