From 3887043ef9d2fd9ec2d47d14baaea9ef54ae6370 Mon Sep 17 00:00:00 2001 From: Edoardo Rosa <6991986+notdodo@users.noreply.github.com> Date: Sun, 22 Sep 2024 00:18:28 +0200 Subject: [PATCH] fix: lambda logging --- app/erfiume/logging.py | 2 +- app/erfiume/storage.py | 4 ++-- app/erfiume_bot.py | 5 +++-- app/erfiume_fetcher.py | 1 + 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/erfiume/logging.py b/app/erfiume/logging.py index aa9c48e..55a643d 100644 --- a/app/erfiume/logging.py +++ b/app/erfiume/logging.py @@ -4,4 +4,4 @@ from aws_lambda_powertools import Logger -logger = Logger(service="erfiume", level="INFO", child=True) +logger = Logger(service="erfiume") diff --git a/app/erfiume/storage.py b/app/erfiume/storage.py index 9991866..13d7498 100644 --- a/app/erfiume/storage.py +++ b/app/erfiume/storage.py @@ -63,7 +63,7 @@ async def check_and_update_stazioni(self, station: Stazione) -> None: # Get the latest timestamp from the DynamoDB response latest_timestamp = ( int(response["Item"].get("timestamp")) # type: ignore[arg-type] - if response["Item"] + if "Item" in response else 0 ) @@ -94,7 +94,7 @@ async def get_matching_station(self, station_name: str) -> Stazione | None: Key={"nomestaz": station_name}, ) - if stazione["Item"]: + if "Item" in stazione: return Stazione(**stazione["Item"]) # type: ignore[arg-type] logger.info("Station %s not found in DynamoDB.", station_name) except ClientError as e: diff --git a/app/erfiume_bot.py b/app/erfiume_bot.py index 817a930..3f117ed 100644 --- a/app/erfiume_bot.py +++ b/app/erfiume_bot.py @@ -14,14 +14,15 @@ from aws_lambda_powertools.utilities.typing import LambdaContext +@logger.inject_lambda_context def handler(event: dict[str, Any], _context: LambdaContext) -> dict[str, Any]: """Run entry point for the bot.""" logger.info("Received event: %s", event) try: asyncio.run(bot(event)) except Exception as e: # noqa: BLE001 - logger.error(f"An error occurred: {e!s}") - logger.error(traceback.format_exc()) + logger.exception("An error occurred: %s", e) + logger.exception(traceback.format_exc()) return {"statusCode": 501} logger.info("Successfully processed event") diff --git a/app/erfiume_fetcher.py b/app/erfiume_fetcher.py index fdda88b..cb00487 100644 --- a/app/erfiume_fetcher.py +++ b/app/erfiume_fetcher.py @@ -46,6 +46,7 @@ async def main() -> None: await update_task +@logger.inject_lambda_context def handler(_event: dict[str, Any], _context: LambdaContext) -> None: """ AWS Lambda starting method