Repository navigation
fix(metrics): lambda_handler typing, and **kwargs preservation all middlewares - #3460
Conversation
Signed-off-by: heitorlessa <lessa@amazon.co.uk>
Signed-off-by: heitorlessa <lessa@amazon.co.uk>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #3460 +/- ##
========================================
Coverage 95.42% 95.43%
========================================
Files 209 209
Lines 9664 9675 +11
Branches 1773 1774 +1
========================================
+ Hits 9222 9233 +11
Misses 329 329
Partials 113 113 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
wait, |
Signed-off-by: heitorlessa <lessa@amazon.co.uk>
|
Found the same issue in the Middleware Factory, subsequently having the same issue in Validation, Idempotency, and Parser. Fixed in all of them. Besides 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") |
|
Kudos, SonarCloud Quality Gate passed!
|








Issue number: #3447
Summary
This PR allows
@metrics.log_metricsdecorator to support custom Lambda Handlers that are either extended with dependency injection, or Event type overridden.Changes
User experience
The following Lambda handler signature now passes Pylance checks, and extra keyword arguments are preserved when stacking decorators.
Checklist
If your change doesn't seem to apply, please leave them unchecked.
Is this a breaking change?
RFC issue number:
Checklist:
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.