11import { LogLevel , setLogLevel } from "../utils" ;
2- import { SampleMode } from "./constants" ;
2+ import { SampleMode , xrayBaggageSubsegmentKey , xraySubsegmentNamespace } from "./constants" ;
33import {
44 convertToAPMParentID ,
55 convertToAPMTraceID ,
@@ -8,14 +8,19 @@ import {
88 extractTraceContext ,
99 readTraceContextFromXray ,
1010 readTraceFromEvent ,
11+ readStepFunctionContextFromEvent ,
1112} from "./context" ;
1213
1314let currentSegment : any ;
1415
1516jest . mock ( "aws-xray-sdk-core" , ( ) => {
1617 return {
17- captureFunc : ( ) => {
18- throw Error ( "Unimplemented" ) ;
18+ captureFunc : ( subsegmentName : string , callback : ( segment : any ) => void ) => {
19+ if ( currentSegment ) {
20+ callback ( currentSegment ) ;
21+ } else {
22+ throw Error ( "Unimplemented" ) ;
23+ }
1924 } ,
2025 getSegment : ( ) => {
2126 if ( currentSegment === undefined ) {
@@ -219,6 +224,137 @@ describe("readTraceFromEvent", () => {
219224 expect ( result ) . toBeUndefined ( ) ;
220225 } ) ;
221226} ) ;
227+
228+ describe ( "readStepFunctionContextFromEvent" , ( ) => {
229+ const stepFunctionEvent = {
230+ dd : {
231+ Execution : {
232+ Name : "fb7b1e15-e4a2-4cb2-963f-8f1fa4aec492" ,
233+ StartTime : "2019-09-30T20:28:24.236Z" ,
234+ } ,
235+ State : {
236+ Name : "step-one" ,
237+ RetryCount : 2 ,
238+ } ,
239+ StateMachine : {
240+ Id : "arn:aws:states:us-east-1:601427279990:stateMachine:HelloStepOneStepFunctionsStateMachine-z4T0mJveJ7pJ" ,
241+ Name : "my-state-machine" ,
242+ } ,
243+ } ,
244+ } as const ;
245+ it ( "reads a trace from an execution id" , ( ) => {
246+ const result = readStepFunctionContextFromEvent ( stepFunctionEvent ) ;
247+ expect ( result ) . toEqual ( {
248+ "step_function.execution_id" : "fb7b1e15-e4a2-4cb2-963f-8f1fa4aec492" ,
249+ "step_function.retry_count" : 2 ,
250+ "step_function.state_machine_arn" :
251+ "arn:aws:states:us-east-1:601427279990:stateMachine:HelloStepOneStepFunctionsStateMachine-z4T0mJveJ7pJ" ,
252+ "step_function.state_machine_name" : "my-state-machine" ,
253+ "step_function.step_name" : "step-one" ,
254+ } ) ;
255+ } ) ;
256+ it ( "returns undefined when event isn't an object" , ( ) => {
257+ const result = readStepFunctionContextFromEvent ( "event" ) ;
258+ expect ( result ) . toBeUndefined ( ) ;
259+ } ) ;
260+ it ( "returns undefined when event is missing datadogContext property" , ( ) => {
261+ const result = readStepFunctionContextFromEvent ( { } ) ;
262+ expect ( result ) . toBeUndefined ( ) ;
263+ } ) ;
264+ it ( "returns undefined when datadogContext is missing Execution property" , ( ) => {
265+ const result = readStepFunctionContextFromEvent ( {
266+ dd : { } ,
267+ } ) ;
268+ expect ( result ) . toBeUndefined ( ) ;
269+ } ) ;
270+ it ( "returns undefined when Execution is missing Name field" , ( ) => {
271+ const result = readStepFunctionContextFromEvent ( {
272+ dd : {
273+ ...stepFunctionEvent . dd ,
274+ Execution : { } ,
275+ } ,
276+ } ) ;
277+ expect ( result ) . toBeUndefined ( ) ;
278+ } ) ;
279+ it ( "returns undefined when Name isn't a string" , ( ) => {
280+ const result = readStepFunctionContextFromEvent ( {
281+ dd : {
282+ ...stepFunctionEvent . dd ,
283+ Execution : {
284+ Name : 12345 ,
285+ } ,
286+ } ,
287+ } ) ;
288+ expect ( result ) . toBeUndefined ( ) ;
289+ } ) ;
290+ it ( "returns undefined when State isn't defined" , ( ) => {
291+ const result = readStepFunctionContextFromEvent ( {
292+ dd : {
293+ ...stepFunctionEvent . dd ,
294+ State : undefined ,
295+ } ,
296+ } ) ;
297+ expect ( result ) . toBeUndefined ( ) ;
298+ } ) ;
299+ it ( "returns undefined when try retry count isn't a number" , ( ) => {
300+ const result = readStepFunctionContextFromEvent ( {
301+ dd : {
302+ ...stepFunctionEvent . dd ,
303+ State : {
304+ ...stepFunctionEvent . dd . State ,
305+ RetryCount : "1" ,
306+ } ,
307+ } ,
308+ } ) ;
309+ expect ( result ) . toBeUndefined ( ) ;
310+ } ) ;
311+ it ( "returns undefined when try step name isn't a string" , ( ) => {
312+ const result = readStepFunctionContextFromEvent ( {
313+ dd : {
314+ ...stepFunctionEvent . dd ,
315+ State : {
316+ ...stepFunctionEvent . dd . State ,
317+ Name : 1 ,
318+ } ,
319+ } ,
320+ } ) ;
321+ expect ( result ) . toBeUndefined ( ) ;
322+ } ) ;
323+ it ( "returns undefined when StateMachine is undefined" , ( ) => {
324+ const result = readStepFunctionContextFromEvent ( {
325+ dd : {
326+ ...stepFunctionEvent . dd ,
327+ StateMachine : undefined ,
328+ } ,
329+ } ) ;
330+ expect ( result ) . toBeUndefined ( ) ;
331+ } ) ;
332+ it ( "returns undefined when StateMachineId isn't a string" , ( ) => {
333+ const result = readStepFunctionContextFromEvent ( {
334+ dd : {
335+ ...stepFunctionEvent . dd ,
336+ StateMachine : {
337+ ...stepFunctionEvent . dd . StateMachine ,
338+ Id : 1 ,
339+ } ,
340+ } ,
341+ } ) ;
342+ expect ( result ) . toBeUndefined ( ) ;
343+ } ) ;
344+ it ( "returns undefined when StateMachineName isn't a string" , ( ) => {
345+ const result = readStepFunctionContextFromEvent ( {
346+ dd : {
347+ ...stepFunctionEvent . dd ,
348+ StateMachine : {
349+ ...stepFunctionEvent . dd . StateMachine ,
350+ Name : 1 ,
351+ } ,
352+ } ,
353+ } ) ;
354+ expect ( result ) . toBeUndefined ( ) ;
355+ } ) ;
356+ } ) ;
357+
222358describe ( "extractTraceContext" , ( ) => {
223359 it ( "returns trace read from header as highest priority" , ( ) => {
224360 currentSegment = {
@@ -252,4 +388,51 @@ describe("extractTraceContext", () => {
252388 traceID : "4110911582297405557" ,
253389 } ) ;
254390 } ) ;
391+ it ( "returns trace read from env if no headers present" , ( ) => {
392+ currentSegment = {
393+ id : "0b11cc4230d3e09e" ,
394+ trace_id : "1-5ce31dc2-2c779014b90ce44db5e03875" ,
395+ } ;
396+
397+ const result = extractTraceContext ( { } ) ;
398+ expect ( result ) . toEqual ( {
399+ parentID : "797643193680388254" ,
400+ sampleMode : SampleMode . USER_KEEP ,
401+ traceID : "4110911582297405557" ,
402+ } ) ;
403+ } ) ;
404+
405+ it ( "adds step function metadata to xray" , ( ) => {
406+ const stepFunctionEvent = {
407+ dd : {
408+ Execution : {
409+ Name : "fb7b1e15-e4a2-4cb2-963f-8f1fa4aec492" ,
410+ StartTime : "2019-09-30T20:28:24.236Z" ,
411+ } ,
412+ State : {
413+ Name : "step-one" ,
414+ RetryCount : 2 ,
415+ } ,
416+ StateMachine : {
417+ Id : "arn:aws:states:us-east-1:601427279990:stateMachine:HelloStepOneStepFunctionsStateMachine-z4T0mJveJ7pJ" ,
418+ Name : "my-state-machine" ,
419+ } ,
420+ } ,
421+ } as const ;
422+ const addMetadata = jest . fn ( ) ;
423+ currentSegment = { addMetadata } ;
424+ extractTraceContext ( stepFunctionEvent ) ;
425+ expect ( addMetadata ) . toHaveBeenCalledWith (
426+ xrayBaggageSubsegmentKey ,
427+ {
428+ "step_function.execution_id" : "fb7b1e15-e4a2-4cb2-963f-8f1fa4aec492" ,
429+ "step_function.retry_count" : 2 ,
430+ "step_function.state_machine_arn" :
431+ "arn:aws:states:us-east-1:601427279990:stateMachine:HelloStepOneStepFunctionsStateMachine-z4T0mJveJ7pJ" ,
432+ "step_function.state_machine_name" : "my-state-machine" ,
433+ "step_function.step_name" : "step-one" ,
434+ } ,
435+ xraySubsegmentNamespace ,
436+ ) ;
437+ } ) ;
255438} ) ;
0 commit comments