Skip to content

Bug: @metrics.log_metrics decorator does not accept additional arguments passed to wrapped function #3119

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

Closed
thegeorgeliu opened this issue Sep 21, 2023 · 6 comments · Fixed by #3120
Labels
bug Something isn't working metrics

Comments

@thegeorgeliu
Copy link
Contributor

Expected Behaviour

The function wrapped by @metrics.log_metrics decorator (e.g. lambda handler) should be able to accept arguments other than the event and lambda context.

Current Behaviour

If an additional positional argument is passed to the wrapped function, the following error is encountered:
TypeError: lambda_handler() takes 2 positional arguments but 3 were given

If an additional keyword argument is passed, this error is raised:
TypeError: lambda_handler() got an unexpected keyword argument 'additional_arg_name'

Code snippet

@metrics.log_metrics
def lambda_handler(event: dict[str, Any], context: LambdaContext, additional_arg_name: Any):
    ...

# This causes a wrong number of positional arguments TypeError
lambda_handler({}, {}, "additional arg value")

# This causes an unexpected keyword argument TypeError
lambda_handler({}, {}, additional_arg_name="additional arg value")

Possible Solution

The decorate() function for log_metrics should take in *args and **kwargs as arguments and then pass them to the lambda_handler() call.

@functools.wraps(lambda_handler)
def decorate(event, context, *args, **kwargs):
    try:
        response = lambda_handler(event, context, *args, **kwargs)
        ...

A similar issue with the @logger.inject_lambda_context decorator was fixed in #1276

Steps to Reproduce

This can be consistently reproduced using the code snippet above.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.10

Packaging format used

PyPi

Debugging logs

No response

@thegeorgeliu thegeorgeliu added bug Something isn't working triage Pending triage from maintainers labels Sep 21, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Sep 21, 2023

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@heitorlessa
Copy link
Contributor

@thegeorgeliu merged 🎉

Let us know if this can't wait until tomorrow for a bug fix release.

Thank you for your first contribution and huge thanks for making both bug report and PR so easy to review ;)

@thegeorgeliu
Copy link
Contributor Author

Thanks @heitorlessa! No worries, there's no issues with waiting till tomorrow. Glad the PR made the cutoff for the release!

@heitorlessa
Copy link
Contributor

Great!! FYI - for bugs that have no workaround and block multiple customers, we make a bug fix straightaway (typically ~30m after merge).

Hence the ask ;)

Will ping back upon release.

Have a great evening

@leandrodamascena
Copy link
Contributor

Closing as complete #3119

@github-actions
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@leandrodamascena leandrodamascena moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working metrics
Projects
Status: Shipped
3 participants