File tree Expand file tree Collapse file tree 4 files changed +73
-0
lines changed Expand file tree Collapse file tree 4 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -907,6 +907,43 @@ describe("TraceContextExtractor", () => {
907907
908908 expect ( extractor ) . toBeInstanceOf ( StepFunctionEventTraceExtractor ) ;
909909 } ) ;
910+
911+ it ( "returns StepFunctionEventTraceExtractor when event contains legacy lambda StepFunctionContext" , ( ) => {
912+ const event = {
913+ Payload : {
914+ Execution : {
915+ Id : "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf" ,
916+ Input : {
917+ MyInput : "MyValue" ,
918+ } ,
919+ Name : "85a9933e-9e11-83dc-6a61-b92367b6c3be" ,
920+ RoleArn :
921+ "arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03" ,
922+ StartTime : "2022-12-08T21:08:17.924Z" ,
923+ } ,
924+ State : {
925+ Name : "step-one" ,
926+ EnteredTime : "2022-12-08T21:08:19.224Z" ,
927+ RetryCount : 2 ,
928+ } ,
929+ StateMachine : {
930+ Id : "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential" ,
931+ Name : "my-state-machine" ,
932+ } ,
933+ } ,
934+ } ;
935+
936+ const tracerWrapper = new TracerWrapper ( ) ;
937+ const traceContextExtractor = new TraceContextExtractor ( tracerWrapper , { } as TraceConfig ) ;
938+
939+ // Mimick TraceContextService.extract initialization
940+ const instance = StepFunctionContextService . instance ( event ) ;
941+ traceContextExtractor [ "stepFunctionContextService" ] = instance ;
942+
943+ const extractor = traceContextExtractor [ "getTraceEventExtractor" ] ( event ) ;
944+
945+ expect ( extractor ) . toBeInstanceOf ( StepFunctionEventTraceExtractor ) ;
946+ } ) ;
910947 } ) ;
911948
912949 describe ( "addTraceContexToXray" , ( ) => {
Original file line number Diff line number Diff line change @@ -55,6 +55,22 @@ describe("StepFunctionEventTraceExtractor", () => {
5555 expect ( traceContext ?. source ) . toBe ( "event" ) ;
5656 } ) ;
5757
58+ it ( "extracts trace context with valid legacy lambda payload" , ( ) => {
59+ // Mimick TraceContextService.extract initialization
60+ StepFunctionContextService . instance ( { Payload : payload } ) ;
61+
62+ const extractor = new StepFunctionEventTraceExtractor ( ) ;
63+
64+ // Payload is sent again for safety in case the instance wasn't previously initialized
65+ const traceContext = extractor . extract ( { Payload : payload } ) ;
66+ expect ( traceContext ) . not . toBeNull ( ) ;
67+
68+ expect ( traceContext ?. toTraceId ( ) ) . toBe ( "1139193989631387307" ) ;
69+ expect ( traceContext ?. toSpanId ( ) ) . toBe ( "5892738536804826142" ) ;
70+ expect ( traceContext ?. sampleMode ( ) ) . toBe ( "1" ) ;
71+ expect ( traceContext ?. source ) . toBe ( "event" ) ;
72+ } ) ;
73+
5874 it ( "returns null when StepFunctionContextService.context is undefined" , async ( ) => {
5975 const extractor = new StepFunctionEventTraceExtractor ( ) ;
6076
Original file line number Diff line number Diff line change @@ -209,6 +209,21 @@ describe("StepFunctionContextService", () => {
209209
210210 expect ( spanContext ) . toBeNull ( ) ;
211211 } ) ;
212+
213+ it ( "returns a SpanContextWrapper when event is from legacy lambda" , ( ) => {
214+ const instance = StepFunctionContextService . instance ( ) ;
215+ // Force setting event
216+ instance [ "setContext" ] ( { Payload : stepFunctionEvent } ) ;
217+
218+ const spanContext = instance . spanContext ;
219+
220+ expect ( spanContext ) . not . toBeNull ( ) ;
221+
222+ expect ( spanContext ?. toTraceId ( ) ) . toBe ( "1139193989631387307" ) ;
223+ expect ( spanContext ?. toSpanId ( ) ) . toBe ( "5892738536804826142" ) ;
224+ expect ( spanContext ?. sampleMode ( ) ) . toBe ( "1" ) ;
225+ expect ( spanContext ?. source ) . toBe ( "event" ) ;
226+ } ) ;
212227 } ) ;
213228
214229 describe ( "deterministicSha256HashToBigIntString" , ( ) => {
Original file line number Diff line number Diff line change @@ -41,6 +41,11 @@ export class StepFunctionContextService {
4141 // always triggered by the same event.
4242 if ( typeof event !== "object" ) return ;
4343
44+ // Legacy lambda parsing
45+ if ( typeof event . Payload === "object" ) {
46+ event = event . Payload ;
47+ }
48+
4449 // Execution
4550 const execution = event . Execution ;
4651 if ( typeof execution !== "object" ) {
You can’t perform that action at this time.
0 commit comments