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(metrics): lambda_handler typing, and **kwargs preservation all middlewares #3460

Merged

Conversation

heitorlessa
Copy link
Contributor

Issue number: #3447

Summary

This PR allows @metrics.log_metrics decorator to support custom Lambda Handlers that are either extended with dependency injection, or Event type overridden.

Changes

Please provide a summary of what's being changed

User experience

Please share what the user experience looks like before and after this change

The following Lambda handler signature now passes Pylance checks, and extra keyword arguments are preserved when stacking decorators.

import json
from typing import List

from aws_lambda_powertools import Logger, Metrics
from aws_lambda_powertools.utilities.parser import BaseModel, Field, event_parser
from aws_lambda_powertools.utilities.typing import LambdaContext


logger = Logger()
metrics = Metrics()


class AgentQuery(BaseModel):
    username: str
    user_query: str


@logger.inject_lambda_context
@metrics.log_metrics(capture_cold_start_metric=True)
@event_parser(model=AgentQuery)
def lambda_handler(event: AgentQuery, context: LambdaContext, my_dependency = None):
    logger.info("hi")

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@boring-cyborg boring-cyborg bot added the metrics label Dec 6, 2023
@pull-request-size pull-request-size bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Dec 6, 2023
@codecov-commenter
Copy link

codecov-commenter commented Dec 6, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (05dd82b) 95.42% compared to head (c64ba60) 95.42%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #3460   +/-   ##
========================================
  Coverage    95.42%   95.42%           
========================================
  Files          209      209           
  Lines         9664     9671    +7     
  Branches      1773     1774    +1     
========================================
+ Hits          9222     9229    +7     
  Misses         329      329           
  Partials       113      113           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@heitorlessa heitorlessa marked this pull request as ready for review December 6, 2023 10:07
@heitorlessa heitorlessa requested a review from a team as a code owner December 6, 2023 10:07
@heitorlessa
Copy link
Contributor Author

wait, event_parser also doesn't preserve kwargs..... 🤦🏻

Signed-off-by: heitorlessa <lessa@amazon.co.uk>
@boring-cyborg boring-cyborg bot added the middleware_factory Middleware factory utility label Dec 6, 2023
@pull-request-size pull-request-size bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Dec 6, 2023
@heitorlessa heitorlessa changed the title fix(metrics): lambda_handler typing, and **kwargs preservation fix(metrics): lambda_handler typing, and **kwargs preservation all middlewares Dec 6, 2023
@heitorlessa
Copy link
Contributor Author

Found the same issue in the Middleware Factory, subsequently having the same issue in Validation, Idempotency, and Parser.

Fixed in all of them. Besides make pr, created the following dummy script to test all of them:


from aws_lambda_powertools import Logger, Metrics
from aws_lambda_powertools.utilities.parser import BaseModel, event_parser
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.utilities.validation import validator
from aws_lambda_powertools.utilities.idempotency import idempotent_function, idempotent
from aws_lambda_powertools.utilities.idempotency.persistence.dynamodb import DynamoDBPersistenceLayer
from dataclasses import dataclass

logger = Logger()
metrics = Metrics(namespace="test")
persistence = DynamoDBPersistenceLayer(table_name="idem")

SCHEMA = {
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "Sample schema",
    "description": "The root schema comprises the entire JSON document.",
    "required": ["username", "user_query"],
    "properties": {
        "username": {
            "$id": "#/properties/username",
            "type": "string",
            "title": "The username",
            "maxLength": 50,
        },
        "user_query": {
            "$id": "#/properties/user_query",
            "type": "string",
            "title": "The user_query",
            "maxLength": 30,
        },
    },
}


class AgentQuery(BaseModel):
    username: str
    user_query: str


@idempotent_function(data_keyword_argument="my_dependency", persistence_store=persistence)
def use_dependency(my_dependency):
    return my_dependency


@validator(inbound_schema=SCHEMA)
@event_parser(model=AgentQuery)
@logger.inject_lambda_context
@metrics.log_metrics(capture_cold_start_metric=True)
@idempotent(persistence_store=persistence)
def lambda_handler(event: AgentQuery, context: LambdaContext, my_dependency: str | None = None) -> None:
    use_dependency(my_dependency=my_dependency)
    logger.info("hi", my_dependency=my_dependency)


@dataclass
class DummyLambdaContext:
    function_name: str = "test"
    memory_limit_in_mb: int = 128
    invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
    aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"

    def get_remaining_time_in_millis(self):
        return 10 * 1000


event = AgentQuery(username="lessa", user_query="kaodsk").dict()

lambda_handler(event, DummyLambdaContext(), my_dependency="test")

Copy link

sonarqubecloud bot commented Dec 6, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
3.1% 3.1% Duplication

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

APPROVED! 🚀

@heitorlessa heitorlessa merged commit 5fda86d into aws-powertools:develop Dec 6, 2023
15 checks passed
@heitorlessa heitorlessa deleted the chore/log-metrics-typing branch December 6, 2023 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metrics middleware_factory Middleware factory utility size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Using @metrics.log_metrics and @event_parser on the same handler causes a type error.
3 participants