@@ -22,12 +22,6 @@ jest.mock("aws-xray-sdk-core", () => {
2222 throw Error ( "Unimplemented" ) ;
2323 }
2424 } ,
25- getSegment : ( ) => {
26- if ( currentSegment === undefined ) {
27- throw Error ( "Empty" ) ;
28- }
29- return currentSegment ;
30- } ,
3125 } ;
3226} ) ;
3327
@@ -127,38 +121,34 @@ describe("convertTraceContext", () => {
127121} ) ;
128122
129123describe ( "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" ,
157135 } ) ;
158136 } ) ;
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+ }
162152 } ) ;
163153} ) ;
164154
@@ -361,11 +351,11 @@ describe("readStepFunctionContextFromEvent", () => {
361351} ) ;
362352
363353describe ( "extractTraceContext" , ( ) => {
354+ afterEach ( ( ) => {
355+ process . env [ "_X_AMZN_TRACE_ID" ] = undefined ;
356+ } ) ;
364357 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" ;
369359
370360 const result = extractTraceContext ( {
371361 headers : {
@@ -382,10 +372,7 @@ describe("extractTraceContext", () => {
382372 } ) ;
383373 } ) ;
384374 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" ;
389376
390377 const result = extractTraceContext ( { } ) ;
391378 expect ( result ) . toEqual ( {
@@ -396,10 +383,7 @@ describe("extractTraceContext", () => {
396383 } ) ;
397384 } ) ;
398385 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" ;
403387
404388 const result = extractTraceContext ( { } ) ;
405389 expect ( result ) . toEqual ( {
0 commit comments