1
1
import {
2
+ Baggage ,
2
3
Context ,
3
4
isSpanContextValid ,
5
+ propagation ,
4
6
TextMapGetter ,
5
- TextMapPropagator ,
6
7
TextMapSetter ,
7
8
trace ,
8
9
TraceFlags ,
9
10
} from '@opentelemetry/api' ;
10
- import { isTracingSuppressed } from '@opentelemetry/core' ;
11
+ import { isTracingSuppressed , W3CBaggagePropagator } from '@opentelemetry/core' ;
11
12
import {
12
13
baggageHeaderToDynamicSamplingContext ,
13
- dynamicSamplingContextToSentryBaggageHeader ,
14
14
extractTraceparentData ,
15
+ SENTRY_BAGGAGE_KEY_PREFIX ,
15
16
} from '@sentry/utils' ;
16
17
17
18
import {
@@ -25,7 +26,7 @@ import { SENTRY_SPAN_PROCESSOR_MAP } from './spanprocessor';
25
26
/**
26
27
* Injects and extracts `sentry-trace` and `baggage` headers from carriers.
27
28
*/
28
- export class SentryPropagator implements TextMapPropagator {
29
+ export class SentryPropagator extends W3CBaggagePropagator {
29
30
/**
30
31
* @inheritDoc
31
32
*/
@@ -41,10 +42,18 @@ export class SentryPropagator implements TextMapPropagator {
41
42
42
43
if ( span . transaction ) {
43
44
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 ) ;
48
57
}
49
58
}
50
59
}
0 commit comments