11import type { Baggage , Context , SpanContext , TextMapGetter , TextMapSetter } from '@opentelemetry/api' ;
22import { TraceFlags , propagation , trace } from '@opentelemetry/api' ;
3- import { W3CBaggagePropagator , isTracingSuppressed } from '@opentelemetry/core' ;
3+ import { TraceState , W3CBaggagePropagator , isTracingSuppressed } from '@opentelemetry/core' ;
44import { getClient , getDynamicSamplingContextFromClient } from '@sentry/core' ;
55import type { DynamicSamplingContext , PropagationContext } from '@sentry/types' ;
6- import { SENTRY_BAGGAGE_KEY_PREFIX , generateSentryTraceHeader , propagationContextFromHeaders } from '@sentry/utils' ;
7-
8- import { SENTRY_BAGGAGE_HEADER , SENTRY_TRACE_HEADER } from './constants' ;
6+ import {
7+ SENTRY_BAGGAGE_KEY_PREFIX ,
8+ dynamicSamplingContextToSentryBaggageHeader ,
9+ generateSentryTraceHeader ,
10+ propagationContextFromHeaders ,
11+ } from '@sentry/utils' ;
12+
13+ import { SENTRY_BAGGAGE_HEADER , SENTRY_TRACE_HEADER , SENTRY_TRACE_STATE_DSC } from './constants' ;
914import { getPropagationContextFromContext , setPropagationContextOnContext } from './utils/contextData' ;
1015
1116/**
@@ -24,7 +29,9 @@ export class SentryPropagator extends W3CBaggagePropagator {
2429
2530 const propagationContext = getPropagationContextFromContext ( context ) ;
2631 const { spanId, traceId, sampled } = getSentryTraceData ( context , propagationContext ) ;
27- const dynamicSamplingContext = propagationContext ? getDsc ( propagationContext , traceId ) : undefined ;
32+ const dynamicSamplingContext = propagationContext
33+ ? getDynamicSamplingContext ( propagationContext , traceId )
34+ : undefined ;
2835
2936 if ( dynamicSamplingContext ) {
3037 baggage = Object . entries ( dynamicSamplingContext ) . reduce < Baggage > ( ( b , [ dscKey , dscValue ] ) => {
@@ -58,11 +65,19 @@ export class SentryPropagator extends W3CBaggagePropagator {
5865 // Add propagation context to context
5966 const contextWithPropagationContext = setPropagationContextOnContext ( context , propagationContext ) ;
6067
68+ // We store the DSC as OTEL trace state on the span context
69+ const dscString = propagationContext . dsc
70+ ? dynamicSamplingContextToSentryBaggageHeader ( propagationContext . dsc )
71+ : undefined ;
72+
73+ const traceState = dscString ? new TraceState ( ) . set ( SENTRY_TRACE_STATE_DSC , dscString ) : undefined ;
74+
6175 const spanContext : SpanContext = {
6276 traceId : propagationContext . traceId ,
6377 spanId : propagationContext . parentSpanId || '' ,
6478 isRemote : true ,
6579 traceFlags : propagationContext . sampled === true ? TraceFlags . SAMPLED : TraceFlags . NONE ,
80+ traceState,
6681 } ;
6782
6883 // Add remote parent span context
@@ -77,7 +92,8 @@ export class SentryPropagator extends W3CBaggagePropagator {
7792 }
7893}
7994
80- function getDsc (
95+ /** Get the DSC. */
96+ function getDynamicSamplingContext (
8197 propagationContext : PropagationContext ,
8298 traceId : string | undefined ,
8399) : Partial < DynamicSamplingContext > | undefined {
@@ -96,6 +112,7 @@ function getDsc(
96112 return undefined ;
97113}
98114
115+ /** Get the trace data for propagation. */
99116function getSentryTraceData (
100117 context : Context ,
101118 propagationContext : PropagationContext | undefined ,
0 commit comments