Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add typecheck to lambda wrapper #533

Merged
merged 2 commits into from
Nov 5, 2024
Merged

Conversation

avedmala
Copy link
Contributor

@avedmala avedmala commented Nov 5, 2024

What does this PR do?

  • Checks if event is a dictionary before trying to call get() on it as it may be a list
  • The case where it is a list is breaking instrumentation for customers
  • Extended unit tests to catch this

Closes #532

Motivation

Testing Guidelines

Additional Notes

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@avedmala avedmala requested a review from a team as a code owner November 5, 2024 17:21
@avedmala avedmala changed the title Hotfix fix: add typecheck to lambda wrapper Nov 5, 2024
@@ -411,6 +411,9 @@ def is_legacy_lambda_step_function(event):
"""
Check if the event is a step function that called a legacy lambda
"""
if not isinstance(event, dict):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have other code that assumes that this is a dict in the wrapper flow that will trip over this problem?

also, should we look at the contents of the list if it is a list? or is that impossible for a legacy lambda step function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont' think we need to look through the whole list. If it's not a dict, we know right away that it's not a step function event.

As for the isinstance, we do have other checks for if the event is a dict. For example in determining trace propagation we skip altogether if it's not a dict. Best answer could even be surrounding the whole thing with a try/except.

Copy link
Contributor Author

@avedmala avedmala Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract_trigger_tags() which is called right after is_legacy_lambda_step_function() expects a dict, assuming there's more

it should be impossible for legacy lambda case

according to the issue

In this case the incoming event is not a dictionary but a list of dictionaries that's generated by a dynamodb stream in an aws eventbridge pipe with batching enabling

The customers use case might still break even with this quick fix

@@ -411,6 +411,9 @@ def is_legacy_lambda_step_function(event):
"""
Check if the event is a step function that called a legacy lambda
"""
if not isinstance(event, dict):
return False

event = event.get("Payload", {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't super related to this incident, but I think it's worth changing anyway. I'm wondering if we can exit early if "Payload" is not in the event. By then we already know that it's not gonna be a step function event. So, exiting early would prevent the allocation of the {} and all the searching on the next line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout 👍🏽 , will add an early exit

if not isinstance(event, dict) or "Payload" not in event:
return False

event = event.get("Payload")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

variable name is the same as a function parameter (...read more)

A function parameter should only be read and not be modified. If your intent is to modify the value of the parameter, return the value in the function and handle the new value in the caller of the function.

View in Datadog  Leave us feedback  Documentation

@avedmala
Copy link
Contributor Author

avedmala commented Nov 5, 2024

/merge

@dd-devflow
Copy link

dd-devflow bot commented Nov 5, 2024

🚂 MergeQueue: waiting for PR to be ready

This merge request is not mergeable yet, because of pending checks/missing approvals. It will be added to the queue as soon as checks pass and/or get approvals.
Note: if you pushed new commits since the last approval, you may need additional approval.
You can remove it from the waiting list with /remove command.

Use /merge -c to cancel this operation!

@dd-devflow
Copy link

dd-devflow bot commented Nov 5, 2024

🚂 MergeQueue: pull request added to the queue

The median merge time in main is 0s.

Use /merge -c to cancel this operation!

@dd-mergequeue dd-mergequeue bot merged commit e58b065 into main Nov 5, 2024
50 of 51 checks passed
@dd-mergequeue dd-mergequeue bot deleted the avedmala/quick-fix branch November 5, 2024 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'_LambdaDecorator' object has no attribute 'trigger_tags'
3 participants