Skip to content

Commit

Permalink
Refactor xfn Lambda injection code my merging two functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lym953 committed Sep 16, 2024
1 parent c48ed11 commit 3566b41
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 60 deletions.
20 changes: 11 additions & 9 deletions src/commands/stepfunctions/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
getStepFunctionLogGroupArn,
parseArn,
buildLogAccessPolicyName,
shouldUpdateStepForTracesMerging,
injectContextForLambdaFunctions,
StepType,
injectContextForStepFunctions,
shouldUpdateStepForStepFunctionContextInjection,
Expand All @@ -29,7 +29,7 @@ const createMockContext = (): BaseContext => {

describe('stepfunctions command helpers tests', () => {
const context = createMockContext()
describe('shouldUpdateStepForTracesMerging test', () => {
describe('injectContextForLambdaFunctions test', () => {
test('already has JsonMerge added to payload field', () => {
const step: StepType = {
Type: 'Task',
Expand All @@ -40,7 +40,7 @@ describe('stepfunctions command helpers tests', () => {
},
End: true,
}
expect(shouldUpdateStepForTracesMerging(step, context, 'Lambda Invoke')).toBeFalsy()
expect(injectContextForLambdaFunctions(step, context, 'Lambda Invoke')).toBeFalsy()
})

test('no payload field', () => {
Expand All @@ -52,7 +52,8 @@ describe('stepfunctions command helpers tests', () => {
},
End: true,
}
expect(shouldUpdateStepForTracesMerging(step, context, 'Lambda Invoke')).toBeTruthy()
expect(injectContextForLambdaFunctions(step, context, 'Lambda Invoke')).toBeTruthy()
expect(step.Parameters?.['Payload.$']).toEqual('States.JsonMerge($$, $, false)')
})

test('default payload field of $', () => {
Expand All @@ -65,7 +66,8 @@ describe('stepfunctions command helpers tests', () => {
},
End: true,
}
expect(shouldUpdateStepForTracesMerging(step, context, 'Lambda Invoke')).toBeTruthy()
expect(injectContextForLambdaFunctions(step, context, 'Lambda Invoke')).toBeTruthy()
expect(step.Parameters?.['Payload.$']).toEqual('States.JsonMerge($$, $, false)')
})

test('custom payload field not using JsonPath expression', () => {
Expand All @@ -78,7 +80,7 @@ describe('stepfunctions command helpers tests', () => {
},
End: true,
}
expect(shouldUpdateStepForTracesMerging(step, context, 'Lambda Invoke')).toBeFalsy()
expect(injectContextForLambdaFunctions(step, context, 'Lambda Invoke')).toBeFalsy()
})

test('custom payload field using JsonPath expression', () => {
Expand All @@ -91,7 +93,7 @@ describe('stepfunctions command helpers tests', () => {
},
End: true,
}
expect(shouldUpdateStepForTracesMerging(step, context, 'Lambda Invoke')).toBeFalsy()
expect(injectContextForLambdaFunctions(step, context, 'Lambda Invoke')).toBeFalsy()
})

test('none-lambda step should not be updated', () => {
Expand All @@ -103,7 +105,7 @@ describe('stepfunctions command helpers tests', () => {
},
End: true,
}
expect(shouldUpdateStepForTracesMerging(step, context, 'DynamoDB Update')).toBeFalsy()
expect(injectContextForLambdaFunctions(step, context, 'DynamoDB Update')).toBeFalsy()
})

test('legacy lambda api should not be updated', () => {
Expand All @@ -112,7 +114,7 @@ describe('stepfunctions command helpers tests', () => {
Resource: 'arn:aws:lambda:sa-east-1:601427271234:function:hello-function',
End: true,
}
expect(shouldUpdateStepForTracesMerging(step, context, 'Legacy Lambda')).toBeFalsy()
expect(injectContextForLambdaFunctions(step, context, 'Legacy Lambda')).toBeFalsy()
})
})

Expand Down
98 changes: 47 additions & 51 deletions src/commands/stepfunctions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,8 @@ export const injectContextIntoTasks = async (
}
}

export const addTraceContextToLambdaParameters = ({Parameters}: StepType): void => {
if (Parameters) {
Parameters[`Payload.$`] = 'States.JsonMerge($$, $, false)'
}
export const addTraceContextToLambdaParameters = (Parameters: ParametersType): void => {
Parameters[`Payload.$`] = 'States.JsonMerge($$, $, false)'
}

export const addTraceContextToStepFunctionParameters = ({Parameters}: StepType): void => {
Expand All @@ -125,47 +123,6 @@ export const addTraceContextToStepFunctionParameters = ({Parameters}: StepType):
}
}

export const shouldUpdateStepForTracesMerging = (step: StepType, context: BaseContext, stepName: string): boolean => {
// not default lambda api
if (step.Resource !== 'arn:aws:states:::lambda:invoke') {
return false
}

if (!step.Parameters) {
context.stdout
.write(`[Warn] Step ${stepName} does not have a Parameters field. Step Functions Context Object injection \
skipped. Your Step Functions trace will not be merged with downstream Lambda traces. To manually merge these traces, \
check out https://docs.datadoghq.com/serverless/step_functions/troubleshooting/\n`)

return false
}

// payload field not set
if (!step.Parameters.hasOwnProperty('Payload.$') && !step.Parameters.hasOwnProperty('Payload')) {
return true
}

// default payload
if (step.Parameters['Payload.$'] === '$') {
return true
}

// context injection is already set up
if (step.Parameters['Payload.$'] === 'States.JsonMerge($$, $, false)') {
context.stdout.write(` Step ${stepName}: Context injection is already set up. Skipping context injection.\n`)

return false
}

// custom payload
context.stdout
.write(`[Warn] Step ${stepName} has a custom Payload field. Step Functions Context Object injection skipped. \
Your Step Functions trace will not be merged with downstream Lambda traces. To manually merge these traces, \
check out https://docs.datadoghq.com/serverless/step_functions/troubleshooting/\n`)

return false
}

// Truth table
// Input | Expected
// -------------------------|---------
Expand Down Expand Up @@ -252,19 +209,58 @@ export type ParametersType = {
}
}

const injectContextForLambdaFunctions = (step: StepType, context: BaseContext, stepName: string): boolean => {
if (shouldUpdateStepForTracesMerging(step, context, stepName)) {
addTraceContextToLambdaParameters(step)

return true
} else if (step.Resource?.startsWith('arn:aws:lambda')) {
export const injectContextForLambdaFunctions = (step: StepType, context: BaseContext, stepName: string): boolean => {
if (step.Resource?.startsWith('arn:aws:lambda')) {
context.stdout.write(
`[Warn] Step ${stepName} may be using the basic legacy integration, which does not support merging lambda trace(s) with Step Functions trace.
To merge lambda trace(s) with Step Functions trace, please consider using the latest integration.
More details can be found on https://docs.aws.amazon.com/step-functions/latest/dg/connect-lambda.html \n`
)

return false
}

// not default lambda api
if (step.Resource !== 'arn:aws:states:::lambda:invoke') {
return false
}

if (!step.Parameters) {
context.stdout
.write(`[Warn] Step ${stepName} does not have a Parameters field. Step Functions Context Object injection \
skipped. Your Step Functions trace will not be merged with downstream Lambda traces. To manually merge these traces, \
check out https://docs.datadoghq.com/serverless/step_functions/troubleshooting/\n`)

return false
}

// payload field not set
if (!step.Parameters.hasOwnProperty('Payload.$') && !step.Parameters.hasOwnProperty('Payload')) {
addTraceContextToLambdaParameters(step.Parameters)

return true
}

// default payload
if (step.Parameters['Payload.$'] === '$') {
addTraceContextToLambdaParameters(step.Parameters)

return true
}

// context injection is already set up
if (step.Parameters['Payload.$'] === 'States.JsonMerge($$, $, false)') {
context.stdout.write(` Step ${stepName}: Context injection is already set up. Skipping context injection.\n`)

return false
}

// custom payload
context.stdout
.write(`[Warn] Step ${stepName} has a custom Payload field. Step Functions Context Object injection skipped. \
Your Step Functions trace will not be merged with downstream Lambda traces. To manually merge these traces, \
check out https://docs.datadoghq.com/serverless/step_functions/troubleshooting/\n`)

return false
}

Expand Down

0 comments on commit 3566b41

Please sign in to comment.