11import {
2+ Baggage ,
23 Context ,
34 isSpanContextValid ,
5+ propagation ,
46 TextMapGetter ,
5- TextMapPropagator ,
67 TextMapSetter ,
78 trace ,
89 TraceFlags ,
910} from '@opentelemetry/api' ;
10- import { isTracingSuppressed } from '@opentelemetry/core' ;
11+ import { isTracingSuppressed , W3CBaggagePropagator } from '@opentelemetry/core' ;
1112import {
1213 baggageHeaderToDynamicSamplingContext ,
13- dynamicSamplingContextToSentryBaggageHeader ,
1414 extractTraceparentData ,
15+ SENTRY_BAGGAGE_KEY_PREFIX ,
1516} from '@sentry/utils' ;
1617
1718import {
@@ -25,7 +26,7 @@ import { SENTRY_SPAN_PROCESSOR_MAP } from './spanprocessor';
2526/**
2627 * Injects and extracts `sentry-trace` and `baggage` headers from carriers.
2728 */
28- export class SentryPropagator implements TextMapPropagator {
29+ export class SentryPropagator extends W3CBaggagePropagator {
2930 /**
3031 * @inheritDoc
3132 */
@@ -41,10 +42,18 @@ export class SentryPropagator implements TextMapPropagator {
4142
4243 if ( span . transaction ) {
4344 const dynamicSamplingContext = span . transaction . getDynamicSamplingContext ( ) ;
44- const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader ( dynamicSamplingContext ) ;
45- if ( sentryBaggageHeader ) {
46- setter . set ( carrier , SENTRY_BAGGAGE_HEADER , sentryBaggageHeader ) ;
47- }
45+
46+ const baggage = propagation . getBaggage ( context ) || propagation . createBaggage ( { } ) ;
47+ const baggageWithSentryInfo = Object . entries ( dynamicSamplingContext ) . reduce < Baggage > (
48+ ( b , [ dscKey , dscValue ] ) => {
49+ if ( dscValue ) {
50+ return b . setEntry ( `${ SENTRY_BAGGAGE_KEY_PREFIX } ${ dscKey } ` , { value : dscValue } ) ;
51+ }
52+ return b ;
53+ } ,
54+ baggage ,
55+ ) ;
56+ super . inject ( propagation . setBaggage ( context , baggageWithSentryInfo ) , carrier , setter ) ;
4857 }
4958 }
5059 }
0 commit comments