@@ -22,12 +22,6 @@ jest.mock("aws-xray-sdk-core", () => {
22
22
throw Error ( "Unimplemented" ) ;
23
23
}
24
24
} ,
25
- getSegment : ( ) => {
26
- if ( currentSegment === undefined ) {
27
- throw Error ( "Empty" ) ;
28
- }
29
- return currentSegment ;
30
- } ,
31
25
} ;
32
26
} ) ;
33
27
@@ -127,38 +121,34 @@ describe("convertTraceContext", () => {
127
121
} ) ;
128
122
129
123
describe ( "readTraceContextFromXray" , ( ) => {
130
- it ( "will parse a trace context from the xray" , ( ) => {
131
- currentSegment = {
132
- id : "0b11cc4230d3e09e" ,
133
- trace_id : "1-5ce31dc2-2c779014b90ce44db5e03875" ,
134
- } ;
135
-
136
- const traceContext = readTraceContextFromXray ( ) ;
137
- expect ( traceContext ) . toEqual ( {
138
- parentID : "797643193680388254" ,
139
- sampleMode : SampleMode . USER_KEEP ,
140
- traceID : "4110911582297405557" ,
141
- source : Source . Xray ,
142
- } ) ;
143
- } ) ;
144
- it ( "will ignore a trace context from the xray, when sampling is turned off" , ( ) => {
145
- currentSegment = {
146
- id : "0b11cc4230d3e09e" ,
147
- notTraced : true ,
148
- trace_id : "1-5ce31dc2-2c779014b90ce44db5e03875" ,
149
- } ;
150
-
151
- const traceContext = readTraceContextFromXray ( ) ;
152
- expect ( traceContext ) . toEqual ( {
153
- parentID : "797643193680388254" ,
154
- sampleMode : SampleMode . USER_REJECT ,
155
- traceID : "4110911582297405557" ,
156
- source : Source . Xray ,
124
+ afterEach ( ( ) => {
125
+ process . env [ "_X_AMZN_TRACE_ID" ] = undefined ;
126
+ } ) ;
127
+ it ( "returns a trace context from a valid env var" , ( ) => {
128
+ process . env [ "_X_AMZN_TRACE_ID" ] = "Root=1-5e272390-8c398be037738dc042009320;Parent=94ae789b969f1cc5;Sampled=1" ;
129
+ const context = readTraceContextFromXray ( ) ;
130
+ expect ( context ) . toEqual ( {
131
+ parentID : "10713633173203262661" ,
132
+ sampleMode : 2 ,
133
+ source : "xray" ,
134
+ traceID : "3995693151288333088" ,
157
135
} ) ;
158
136
} ) ;
159
- it ( "returns undefined when trace header isn't in environment" , ( ) => {
160
- const traceContext = readTraceContextFromXray ( ) ;
161
- expect ( traceContext ) . toBeUndefined ( ) ;
137
+ it ( "returns undefined when given an invalid env var" , ( ) => {
138
+ const badCases = [
139
+ "Root=1-5e272390-8c398be037738dc042009320;Parent=94ae789b969f1cc5" ,
140
+ "Root=1-5e272390-8c398be037738dc042009320" ,
141
+ "1-5e272390-8c398be037738dc042009320;Parent=94ae789b969f1cc5;Sampled=1" ,
142
+ "Root=1-5e272390-8c398be037738dc042009320;94ae789b969f1cc5;Sampled=1" ,
143
+ "Root=1-5e272390-8c398be037738dc042009320;Parent=94ae789b969f1cc5;1" ,
144
+ "Root=a;Parent=94ae789b969f1cc5;Sampled=1" ,
145
+ "Root=1-5e272390-8c398be037738dc042009320;Parent=b;Sampled=1" ,
146
+ undefined ,
147
+ ] ;
148
+ for ( const badCase of badCases ) {
149
+ process . env [ "_X_AMZN_TRACE_ID" ] = badCase ;
150
+ expect ( readTraceContextFromXray ( ) ) . toBeUndefined ( ) ;
151
+ }
162
152
} ) ;
163
153
} ) ;
164
154
@@ -361,11 +351,11 @@ describe("readStepFunctionContextFromEvent", () => {
361
351
} ) ;
362
352
363
353
describe ( "extractTraceContext" , ( ) => {
354
+ afterEach ( ( ) => {
355
+ process . env [ "_X_AMZN_TRACE_ID" ] = undefined ;
356
+ } ) ;
364
357
it ( "returns trace read from header as highest priority" , ( ) => {
365
- currentSegment = {
366
- parent_id : "0b11cc4230d3e09e" ,
367
- trace_id : "1-5ce31dc2-2c779014b90ce44db5e03875" ,
368
- } ;
358
+ process . env [ "_X_AMZN_TRACE_ID" ] = "Root=1-5ce31dc2-2c779014b90ce44db5e03875;Parent=0b11cc4230d3e09e;Sampled=1" ;
369
359
370
360
const result = extractTraceContext ( {
371
361
headers : {
@@ -382,10 +372,7 @@ describe("extractTraceContext", () => {
382
372
} ) ;
383
373
} ) ;
384
374
it ( "returns trace read from env if no headers present" , ( ) => {
385
- currentSegment = {
386
- id : "0b11cc4230d3e09e" ,
387
- trace_id : "1-5ce31dc2-2c779014b90ce44db5e03875" ,
388
- } ;
375
+ process . env [ "_X_AMZN_TRACE_ID" ] = "Root=1-5ce31dc2-2c779014b90ce44db5e03875;Parent=0b11cc4230d3e09e;Sampled=1" ;
389
376
390
377
const result = extractTraceContext ( { } ) ;
391
378
expect ( result ) . toEqual ( {
@@ -396,10 +383,7 @@ describe("extractTraceContext", () => {
396
383
} ) ;
397
384
} ) ;
398
385
it ( "returns trace read from env if no headers present" , ( ) => {
399
- currentSegment = {
400
- id : "0b11cc4230d3e09e" ,
401
- trace_id : "1-5ce31dc2-2c779014b90ce44db5e03875" ,
402
- } ;
386
+ process . env [ "_X_AMZN_TRACE_ID" ] = "Root=1-5ce31dc2-2c779014b90ce44db5e03875;Parent=0b11cc4230d3e09e;Sampled=1" ;
403
387
404
388
const result = extractTraceContext ( { } ) ;
405
389
expect ( result ) . toEqual ( {
0 commit comments