@@ -54,6 +54,21 @@ describe('tracing', () => {
5454 return transaction ;
5555 }
5656
57+ function getHub ( customOptions : Partial < NodeClientOptions > = { } ) {
58+ const options = getDefaultNodeClientOptions ( {
59+ dsn :
'https://[email protected] /12312012' , 60+ tracesSampleRate : 1.0 ,
61+ integrations : [ new HttpIntegration ( { tracing : true } ) ] ,
62+ release : '1.0.0' ,
63+ environment : 'production' ,
64+ ...customOptions ,
65+ } ) ;
66+ const hub = new Hub ( new NodeClient ( options ) ) ;
67+ jest . spyOn ( sentryCore , 'getCurrentHub' ) . mockReturnValue ( hub ) ;
68+
69+ return hub ;
70+ }
71+
5772 it ( "creates a span for each outgoing non-sentry request when there's a transaction on the scope" , ( ) => {
5873 nock ( 'http://dogs.are.great' ) . get ( '/' ) . reply ( 200 ) ;
5974
@@ -162,6 +177,51 @@ describe('tracing', () => {
162177 ] ) ;
163178 } ) ;
164179
180+ it ( 'generates and uses propagation context to attach baggage and sentry-trace header' , async ( ) => {
181+ nock ( 'http://dogs.are.great' ) . get ( '/' ) . reply ( 200 ) ;
182+
183+ const request = http . get ( 'http://dogs.are.great/' ) ;
184+ const sentryTraceHeader = request . getHeader ( 'sentry-trace' ) as string ;
185+ const baggageHeader = request . getHeader ( 'baggage' ) as string ;
186+
187+ const parts = sentryTraceHeader . split ( '-' ) ;
188+ expect ( parts . length ) . toEqual ( 3 ) ;
189+ expect ( parts [ 0 ] ) . toEqual ( '12312012123120121231201212312012' ) ;
190+ expect ( parts [ 1 ] ) . toEqual ( expect . any ( String ) ) ;
191+ expect ( parts [ 2 ] ) . toEqual ( '1' ) ;
192+
193+ expect ( baggageHeader ) . toEqual (
194+ 'sentry-environment=production,sentry-release=1.0.0,sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,sentry-trace_id=12312012123120121231201212312012,sentry-sample_rate=1' ,
195+ ) ;
196+ } ) ;
197+
198+ it ( 'uses incoming propagation context to attach baggage and sentry-trace' , async ( ) => {
199+ nock ( 'http://dogs.are.great' ) . get ( '/' ) . reply ( 200 ) ;
200+
201+ const hub = getHub ( ) ;
202+ hub . getScope ( ) . setPropagationContext ( {
203+ traceId : '86f39e84263a4de99c326acab3bfe3bd' ,
204+ spanId : '86f39e84263a4de9' ,
205+ sampled : true ,
206+ dsc : {
207+ trace_id : '86f39e84263a4de99c326acab3bfe3bd' ,
208+ public_key : 'test-public-key' ,
209+ } ,
210+ } ) ;
211+
212+ const request = http . get ( 'http://dogs.are.great/' ) ;
213+ const sentryTraceHeader = request . getHeader ( 'sentry-trace' ) as string ;
214+ const baggageHeader = request . getHeader ( 'baggage' ) as string ;
215+
216+ const parts = sentryTraceHeader . split ( '-' ) ;
217+ expect ( parts . length ) . toEqual ( 3 ) ;
218+ expect ( parts [ 0 ] ) . toEqual ( '86f39e84263a4de99c326acab3bfe3bd' ) ;
219+ expect ( parts [ 1 ] ) . toEqual ( expect . any ( String ) ) ;
220+ expect ( parts [ 2 ] ) . toEqual ( '1' ) ;
221+
222+ expect ( baggageHeader ) . toEqual ( 'sentry-trace_id=86f39e84263a4de99c326acab3bfe3bd,sentry-public_key=test-public-key' ) ;
223+ } ) ;
224+
165225 it ( "doesn't attach the sentry-trace header to outgoing sentry requests" , ( ) => {
166226 nock ( 'http://squirrelchasers.ingest.sentry.io' ) . get ( '/api/12312012/store/' ) . reply ( 200 ) ;
167227
0 commit comments