Skip to content

Commit 5e29f63

Browse files
avedmaladuncanista
andauthored
Step Functions Legacy Lambda Span Linking (#567)
* logic to parse payload from legacy lambda event * fix linter * add legacy lambda unit test case * fix lint * added more legacy lambda unit test cases * fix lint * Remove constant Co-authored-by: jordan gonzález <[email protected]> --------- Co-authored-by: jordan gonzález <[email protected]>
1 parent bbb0aef commit 5e29f63

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/trace/context/extractor.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff 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", () => {

src/trace/context/extractors/step-function.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff 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

src/trace/step-function-service.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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", () => {

src/trace/step-function-service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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") {

0 commit comments

Comments
 (0)