Skip to content

Commit

Permalink
remove trace state handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Nov 26, 2024
1 parent a4c84aa commit 9e88097
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 44 deletions.
1 change: 0 additions & 1 deletion packages/opentelemetry/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export const SENTRY_TRACE_HEADER = 'sentry-trace';
export const SENTRY_BAGGAGE_HEADER = 'baggage';

export const SENTRY_TRACE_STATE_DSC = 'sentry.dsc';
export const SENTRY_TRACE_STATE_PARENT_SPAN_ID = 'sentry.parent_span_id';
export const SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING = 'sentry.sampled_not_recording';
export const SENTRY_TRACE_STATE_URL = 'sentry.url';

Expand Down
5 changes: 2 additions & 3 deletions packages/opentelemetry/src/propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import {
SENTRY_BAGGAGE_HEADER,
SENTRY_TRACE_HEADER,
SENTRY_TRACE_STATE_DSC,
SENTRY_TRACE_STATE_PARENT_SPAN_ID,
SENTRY_TRACE_STATE_URL,
} from './constants';
import { DEBUG_BUILD } from './debug-build';
import { getScopesFromContext, setScopesOnContext } from './utils/contextData';
import { generateSpanContextForPropagationContext } from './utils/generateSpanContextForPropagationContext';
import { getSamplingDecision } from './utils/getSamplingDecision';
import { setIsSetup } from './utils/setupCheck';
import { spanHasParentId } from './utils/spanTypes';

/** Get the Sentry propagation context from a span context. */
export function getPropagationContextFromSpan(span: Span): PropagationContext {
Expand All @@ -44,8 +44,7 @@ export function getPropagationContextFromSpan(span: Span): PropagationContext {
const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
const traceStateDsc = dscString ? baggageHeaderToDynamicSamplingContext(dscString) : undefined;

const parentSpanId = traceState ? traceState.get(SENTRY_TRACE_STATE_PARENT_SPAN_ID) || undefined : undefined;

const parentSpanId = spanHasParentId(span) ? span.parentSpanId : undefined;
const sampled = getSamplingDecision(spanContext);

// No trace state? --> Take DSC from root span
Expand Down
12 changes: 1 addition & 11 deletions packages/opentelemetry/src/setupEventContextTrace.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getDynamicSamplingContextFromSpan, getRootSpan } from '@sentry/core';
import { dropUndefinedKeys } from '@sentry/core';
import type { Client } from '@sentry/types';
import { SENTRY_TRACE_STATE_PARENT_SPAN_ID } from './constants';
import { getActiveSpan } from './utils/getActiveSpan';
import { spanHasParentId } from './utils/spanTypes';

Expand All @@ -16,16 +15,7 @@ export function setupEventContextTrace(client: Client): void {
}

const spanContext = span.spanContext();

// If we have a parent span id from trace state, use that ('' means no parent should be used)
// Else, pick the one from the span
const parentSpanIdFromTraceState = spanContext.traceState?.get(SENTRY_TRACE_STATE_PARENT_SPAN_ID);
const parent_span_id =
typeof parentSpanIdFromTraceState === 'string'
? parentSpanIdFromTraceState || undefined
: spanHasParentId(span)
? span.parentSpanId
: undefined;
const parent_span_id = spanHasParentId(span) ? span.parentSpanId : undefined;

// If event has already set `trace` context, use that one.
event.contexts = {
Expand Down
6 changes: 1 addition & 5 deletions packages/opentelemetry/src/spanExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
} from '@sentry/core';
import { dropUndefinedKeys, logger } from '@sentry/core';
import type { SpanJSON, SpanOrigin, TraceContext, TransactionEvent, TransactionSource } from '@sentry/types';
import { SENTRY_TRACE_STATE_PARENT_SPAN_ID } from './constants';

import { DEBUG_BUILD } from './debug-build';
import { SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE } from './semanticAttributes';
Expand Down Expand Up @@ -207,15 +206,12 @@ function createTransactionForOtelSpan(span: ReadableSpan): TransactionEvent {

const { traceId: trace_id, spanId: span_id } = span.spanContext();

const parentSpanIdFromTraceState = span.spanContext().traceState?.get(SENTRY_TRACE_STATE_PARENT_SPAN_ID);

// If parentSpanIdFromTraceState is defined at all, we want it to take precedence
// In that case, an empty string should be interpreted as "no parent span id",
// even if `span.parentSpanId` is set
// this is the case when we are starting a new trace, where we have a virtual span based on the propagationContext
// We only want to continue the traceId in this case, but ignore the parent span
const parent_span_id =
typeof parentSpanIdFromTraceState === 'string' ? parentSpanIdFromTraceState || undefined : span.parentSpanId;
const parent_span_id = span.parentSpanId;

const status = mapStatus(span);

Expand Down
3 changes: 1 addition & 2 deletions packages/opentelemetry/src/trace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Context, Span, SpanContext, SpanOptions, Tracer } from '@opentelemetry/api';
import { INVALID_SPANID, SpanStatusCode, TraceFlags, context, trace } from '@opentelemetry/api';
import { SpanStatusCode, TraceFlags, context, trace } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';
import {
SDK_VERSION,
Expand Down Expand Up @@ -209,7 +209,6 @@ function getContext(scope: Scope | undefined, forceTransaction: boolean | undefi

const traceState = makeTraceState({
dsc,
parentSpanId: spanId !== INVALID_SPANID ? spanId : undefined,
sampled,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { makeTraceState } from './makeTraceState';
export function generateSpanContextForPropagationContext(propagationContext: PropagationContext): SpanContext {
// We store the DSC as OTEL trace state on the span context
const traceState = makeTraceState({
parentSpanId: propagationContext.parentSpanId,
dsc: propagationContext.dsc,
sampled: propagationContext.sampled,
});
Expand Down
14 changes: 3 additions & 11 deletions packages/opentelemetry/src/utils/makeTraceState.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
import { TraceState } from '@opentelemetry/core';
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/core';
import type { DynamicSamplingContext } from '@sentry/types';
import {
SENTRY_TRACE_STATE_DSC,
SENTRY_TRACE_STATE_PARENT_SPAN_ID,
SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING,
} from '../constants';
import { SENTRY_TRACE_STATE_DSC, SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING } from '../constants';

/**
* Generate a TraceState for the given data.
*/
export function makeTraceState({
parentSpanId,
dsc,
sampled,
}: {
/** @deprecated parentSpanId does not do anything anymore and will be removed in v9. */
parentSpanId?: string;
dsc?: Partial<DynamicSamplingContext>;
sampled?: boolean;
}): TraceState {
// We store the DSC as OTEL trace state on the span context
const dscString = dsc ? dynamicSamplingContextToSentryBaggageHeader(dsc) : undefined;

// We _always_ set the parent span ID, even if it is empty
// If we'd set this to 'undefined' we could not know if the trace state was set, but there was no parentSpanId,
// vs the trace state was not set at all (in which case we want to do fallback handling)
// If `''`, it should be considered "no parent"
const traceStateBase = new TraceState().set(SENTRY_TRACE_STATE_PARENT_SPAN_ID, parentSpanId || '');
const traceStateBase = new TraceState();

const traceStateWithDsc = dscString ? traceStateBase.set(SENTRY_TRACE_STATE_DSC, dscString) : traceStateBase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ describe('Integration | Transactions', () => {
const parentSpanId = '6e0c63257de34c92';

const traceState = makeTraceState({
parentSpanId,
dsc: undefined,
sampled: true,
});
Expand Down
10 changes: 3 additions & 7 deletions packages/opentelemetry/test/propagator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ describe('SentryPropagator', () => {
traceFlags: TraceFlags.SAMPLED,
isRemote: true,
traceState: makeTraceState({
parentSpanId: '6e0c63257de34c92',
dsc: {
transaction: 'sampled-transaction',
sampled: 'true',
Expand Down Expand Up @@ -256,7 +255,6 @@ describe('SentryPropagator', () => {
traceFlags: TraceFlags.NONE,
isRemote: true,
traceState: makeTraceState({
parentSpanId: '6e0c63257de34c92',
dsc: {
transaction: 'sampled-transaction',
sampled: 'false',
Expand Down Expand Up @@ -291,7 +289,6 @@ describe('SentryPropagator', () => {
isRemote: true,
traceState: makeTraceState({
sampled: false,
parentSpanId: '6e0c63257de34c92',
dsc: {
transaction: 'sampled-transaction',
trace_id: 'dsc_trace_id',
Expand Down Expand Up @@ -557,7 +554,7 @@ describe('SentryPropagator', () => {
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
traceState: makeTraceState({ parentSpanId: '6e0c63257de34c92' }),
traceState: makeTraceState({}),
});
expect(getSamplingDecision(trace.getSpanContext(context)!)).toBe(true);
});
Expand All @@ -571,7 +568,7 @@ describe('SentryPropagator', () => {
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.NONE,
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
traceState: makeTraceState({ parentSpanId: '6e0c63257de34c92', sampled: false }),
traceState: makeTraceState({ sampled: false }),
});
expect(getSamplingDecision(trace.getSpanContext(context)!)).toBe(false);
});
Expand All @@ -585,7 +582,7 @@ describe('SentryPropagator', () => {
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.NONE,
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
traceState: makeTraceState({ parentSpanId: '6e0c63257de34c92' }),
traceState: makeTraceState({}),
});
expect(getSamplingDecision(trace.getSpanContext(context)!)).toBe(undefined);
});
Expand Down Expand Up @@ -635,7 +632,6 @@ describe('SentryPropagator', () => {
traceFlags: TraceFlags.SAMPLED,
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
traceState: makeTraceState({
parentSpanId: '6e0c63257de34c92',
dsc: {
environment: 'production',
release: '1.0.0',
Expand Down
2 changes: 0 additions & 2 deletions packages/opentelemetry/test/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,6 @@ describe('trace', () => {
isRemote: false,
traceFlags: TraceFlags.SAMPLED,
traceState: makeTraceState({
parentSpanId: '1121201211212011',
dsc: {
release: '1.0',
environment: 'production',
Expand Down Expand Up @@ -1114,7 +1113,6 @@ describe('trace', () => {
isRemote: true,
traceFlags: TraceFlags.SAMPLED,
traceState: makeTraceState({
parentSpanId: '1121201211212011',
dsc: {
release: '1.0',
environment: 'production',
Expand Down

0 comments on commit 9e88097

Please sign in to comment.