Skip to content

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

Closed
@thegeorgeliu

Description

@thegeorgeliu

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmetrics

    Type

    No type

    Projects

    Status

    Shipped

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions