-
Notifications
You must be signed in to change notification settings - Fork 429
Bug: Injecting Lambda context into logger causing exception #4362
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
Comments
Hey @Thomas-McKanna, can you please confirm the Powertools version you are using? Thanks |
Ah, I saw that you mentioned the version in the issue: 2.38.0. I'm using this code and this is working as expected in 2.38.0 version. Can you check my code and see if I'm missing something? CODEimport requests
from requests import Response
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext
tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver()
@app.get("/hello")
@tracer.capture_method
def get_todos():
todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
todos.raise_for_status()
# for brevity, we'll limit to the first 10 only
return {"todos": todos.json()[:10]}
# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_HTTP, log_event=True)
@tracer.capture_lambda_handler
def lambda_handler(event: dict, context: LambdaContext) -> dict:
return app.resolve(event, context) OUTPUT
|
@leandrodamascena The bug appears in 2.38.0. My code was working up to and including version 2.37.0. |
Thanks for sending this. I sent an example that is working for me. Can you validate this and see if I'm doing anything different than you? |
I am also seeing an issue that I think is related. I am getting the following error:
|
I'm doing a line by line comparison right now. It's stumping me. For some, my formatter is None: powertools-lambda-python/aws_lambda_powertools/logging/logger.py Lines 697 to 707 in d0293d0
I see there is a check that handler is None, but that that the formatter within it is None. I really am struggling to find a difference in my code. Here is a scrubbed version of what I'm working with, with the endpoints taken out: import traceback
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import (
APIGatewayRestResolver,
Response,
content_types,
)
from aws_lambda_powertools.event_handler.openapi.params import Path
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.shared.types import Annotated
from aws_lambda_powertools.utilities.typing import LambdaContext
tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(path="/swagger")
@logger.inject_lambda_context(
correlation_id_path=correlation_paths.API_GATEWAY_HTTP, log_event=True
)
@tracer.capture_lambda_handler
def lambda_handler(request: dict, context: LambdaContext) -> dict:
return app.resolve(request, context) |
I suspect we have some regression caused here: https://github.com/aws-powertools/powertools-lambda-python/pull/4295/files. I appreciate your patience and I'm sorry for some back and forth @Thomas-McKanna and @SimonBFrank, but I need to confirm some information before applying a patch or reverting this PR. Can you confirm the environment you are running this Lambda in? Is this using AWS? SAM CLI? Serverless framework? Local emulation? Thanks |
I am running in Python 3.10 Lambda environment in AWS, deployed with SAM CLI. I have not tried local emulation. |
If it helps, I am doing a |
I am seeing this when I am running locally. |
Thank you so much @Thomas-McKanna and @SimonBFrank, this is the missing piece. I was able to reproduce this and am debugging to see if I can find a quick solution or revert this PR https://github.com/aws-powertools/powertools-lambda-python/pull/4295/files validate_log.pyfrom collections import namedtuple
from aws_lambda_powertools.event_handler import (
APIGatewayRestResolver,)
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools import Logger
from event import EVENT
from validate_log1 import *
logger = Logger()
app = APIGatewayRestResolver(enable_validation=True)
@app.get("/hello")
def get_todos():
return {"todos": "OK"}
@logger.inject_lambda_context(
correlation_id_path=correlation_paths.API_GATEWAY_HTTP, log_event=True
)
def lambda_handler(request: dict, context: LambdaContext) -> dict:
return app.resolve(request, context)
def lambda_context():
lambda_context = {
"function_name": "test",
"memory_limit_in_mb": 128,
"invoked_function_arn": "arn:aws:lambda:eu-west-1:809313241:function:test",
"aws_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72",
}
return namedtuple("LambdaContext", lambda_context.keys())(*lambda_context.values())
lambda_handler(EVENT, lambda_context()) validate_log1.pyfrom aws_lambda_powertools import Logger
logger = Logger() output(venv) ➜ bug-log python validate_log.py
Traceback (most recent call last):
File "/Users//DEV/powertools/tmp-testes/bug-log/validate_log.py", line 37, in <module>
lambda_handler(EVENT, lambda_context())
File "/Users//DEV/powertools/tmp-testes/bug-log/venv/lib/python3.10/site-packages/aws_lambda_powertools/logging/logger.py", line 455, in decorate
self.append_keys(cold_start=cold_start, **lambda_context.__dict__)
File "/Users//DEV/powertools/tmp-testes/bug-log/venv/lib/python3.10/site-packages/aws_lambda_powertools/logging/logger.py", line 603, in append_keys
self.registered_formatter.append_keys(**additional_keys)
AttributeError: 'NoneType' object has no attribute 'append_keys' This same code works in version 2.37.0 |
+1. We saw this happening too. We had to rollback to 2.37 and it seems to work now. What's weird is that this code segment worked locally, but did not work when deployed to a Lambda. |
Hi @saravsak, just to understand your use case, how many Logger instances are you creating? Do you have any other library that creates a log instance? |
I'll rollback this PR to allow us to have more time to test as many scenarios as possible, it looks like we have some use cases that aren't clear and I don't know if the patch I'm going to apply has any side effects. I will make a patch release 2.38.1 with the code reverted. Thank you everyone for reporting this and I apologize for any inconveniences. |
Looks like the reason this was working locally was because we were still using 2.37 in our virtualenv whereas our pipeline was installing 2.38 which is why our deployments broke. Roling it back to 2.37 worked. |
I'm waiting for the CI to turn green to merge and release a patch release. |
I'm releasing v2.38.1 - https://github.com/aws-powertools/powertools-lambda-python/actions/runs/9133311541 |
Hello @Thomas-McKanna @SimonBFrank @saravsak @JacobAuthenic v2.38.1 is already available in PyPi, can you please try and give me a feedback? |
This is now released under 2.38.1 version! |
v2.38.1 works again for me! Thanks for the help |
Working for me now also. Thanks! |
|
thank you all for pitching in so quickly, can't thank you enough. We'll create new tests for this use case (functional & end-to-end) to catch regressions like this earlier now. and thank you @leandrodamascena for working late on Friday night to revert the other bug fix that caused this regression undetected by our tests suite <3 |
Expected Behaviour
Should be able to write a Lambda handler like this:
Current Behaviour
502 errors are being returned by API endpoint pointing to Lambda function. Error log is:
I suspect this error is due to a recent change, as things were working for me yesterday. Removing the
@logger.inject_lambda_context
decorator removed the error.The code does not work in version 2.38.0, but does work in version 2.37.0 and earlier.
Code snippet
Possible Solution
No response
Steps to Reproduce
Make minimalistic Lambda Powertools event handler.
Powertools for AWS Lambda (Python) version
latest
AWS Lambda function runtime
3.10
Packaging format used
Lambda Layers
Debugging logs
No response
The text was updated successfully, but these errors were encountered: