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

Bug: log_uncaught_exceptions only works locally and not on lambda #1798

Open
bellis-ai opened this issue Dec 27, 2022 · 21 comments
Open

Bug: log_uncaught_exceptions only works locally and not on lambda #1798

bellis-ai opened this issue Dec 27, 2022 · 21 comments
Assignees
Labels
bug-upstream Something isn't working in an upstream dependency logger

Comments

@bellis-ai
Copy link

Expected Behaviour

When log_uncaught_exceptions=True, uncaught exceptions in a lambda are handled and transformed to a structured log.

Current Behaviour

Uncaught exceptions are printed unstructured.

Code snippet

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger(log_uncaught_exceptions=True)

def lambda_handler(event: dict, context: LambdaContext) -> str:
    raise(Exception("asdf"))

Possible Solution

Probably none. Document this behavior.

I don't think sys.excepthook can be overridden in a lambda container, but I have no proof because the original forum thread that details this has been deleted by AWS with no replacement.

Steps to Reproduce

Set log_uncaught_exceptions=True in an actual running lambda. If log_uncaught_exceptions=True on a local machine, the behavior runs just as intended.

AWS Lambda Powertools for Python version

latest

AWS Lambda function runtime

3.9

Packaging format used

Lambda Layers

Debugging logs

No response

@bellis-ai bellis-ai added bug Something isn't working triage Pending triage from maintainers labels Dec 27, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented Dec 27, 2022

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our AWS Lambda Powertools Discord: Invite link

@bellis-ai
Copy link
Author

@leandrodamascena
Copy link
Contributor

Hello @bellis-ai. Thanks for reporting this bug and posting the helpful links for review. We were able to reproduce the error in our local tests and are looking into how to resolve it. This failure was most likely because we made this update and we didn't have E2E tests for it.

We'll be back with an update here as soon as possible.

@leandrodamascena leandrodamascena self-assigned this Dec 28, 2022
@leandrodamascena leandrodamascena added logger and removed triage Pending triage from maintainers labels Dec 28, 2022
@leandrodamascena
Copy link
Contributor

Coming back here with feedback. We tried to find a solution and ran into the same problem: we couldn't get the excepthook to work inside the Lambda runtime and we couldn't catch exceptions using log_uncaught_exceptions=True.
We are reaching out internally to the Lambda team to understand if there is any workaround to do in this case or if we are missing something.

Not sure if this is suitable for you, but for now you can use Middleware Factory to catch all exceptions and log them centrally. We had a discussion about this on our Discord Server and it might be helpful in your case.

image

Thank you for your patience @bellis-ai and we are working to resolve this as soon as possible.

@leandrodamascena leandrodamascena added the help wanted Could use a second pair of eyes/hands label Jan 2, 2023
@leandrodamascena leandrodamascena added bug-upstream Something isn't working in an upstream dependency and removed bug Something isn't working labels Jan 10, 2023
@leandrodamascena
Copy link
Contributor

Updating this issue...
We're working closely with the Lambda team to investigate the issue and determine if we have a workaround to resolve the issue.

I'll come back here as soon as I have an update. In the meantime, I'll update the documentation to suggest not using this code in production and putting this issue in a blocking state.

@leandrodamascena leandrodamascena removed the help wanted Could use a second pair of eyes/hands label Jan 10, 2023
@heitorlessa
Copy link
Contributor

Update: @leandrodamascena updated the docs!

Adding this to our board of activities so we can do bi-weekly follow ups with the Lambda team - (no update beyond acknowledge yet).

@matteocontrini
Copy link

Are there updates on this issue?

@heitorlessa
Copy link
Contributor

Not yet @matteocontrini, meeting the team early next week for a follow up and post here if anything changes. Appreciate the patience

@jth08527
Copy link

Not yet @matteocontrini, meeting the team early next week for a follow up and post here if anything changes. Appreciate the patience

I was hoping to use this feature on a new project, then saw the note cautioning on using it, and found this open issue.

Did the meeting happen? Is there a plan to address this?

@heitorlessa
Copy link
Contributor

heitorlessa commented May 17, 2023 via email

@heitorlessa
Copy link
Contributor

Good news! Met with Lambda engineering to rule out options, and we have a potential solution for all runtimes.

I say potential because security review might take a while to ensure there's no angle for abuse.

Setting expectations

One caveat tho:

It might be applied to newer runtimes only (pending investigation). This means, if we can't backport this will only
work for Python 3.11+.

Lambda will literally change the component responsible for handling every customer's request to ensure each programming language hook for unhandled exception works as expected without side effects or abuse - we'll take due diligence as expected for any component in the critical path.

We'll keep you posted; progress nevertheless.

I appreciate everyone's patience on this.

@kbakk
Copy link
Contributor

kbakk commented Nov 20, 2023

@heitorlessa Hi, seeing that the Python 3.11 Lambda runtime has been released, are there any updates here? Thanks!

@heitorlessa
Copy link
Contributor

heitorlessa commented Nov 21, 2023 via email

@tlinhart
Copy link

I'm aware that under the hood the things differ, but if the only motivation for this issue is being able to see the unhandled exceptions logged in a JSON format, the introduction of advanced logging controls might make it superfluous. If I specify JSON log format in the Lambda function's logging configuration, all Lambda system events (including the unhandled exceptions) get logged in a JSON format.

image

@heitorlessa
Copy link
Contributor

That unfortunately causes another unwanted side effect @tlinhart -- any warnings coming from libraries or your own emitted warnings will be dropped with this configuration.

The new ALC (Advanced Lambda Control) will look at the message, it doesn't have the required fields and it will convert to INFO. However your ALC log level is set to ERROR, so this will effectively drop any INFO (thus that warning) too.

@tlinhart
Copy link

I usually capture the warnings using logging.captureWarnings(True) which I thought would suffice in this case. Also, I set the ALC to the most granular level and manage the log level solely in Powertools via the POWERTOOLS_LOG_LEVEL variable.

@heitorlessa
Copy link
Contributor

I haven't tried but I suspect if you explicitly use captureWarnings(True) it might work because ALC feature changes the Root Logger.

If my memory serves, captureWarnings(True) configures a logger named py.warnings, which by itself isn't configured to the same log level. Since the root is, it should propagate, just like NullHandler from 3rd party libraries will propagate all debug logs once ALC is enabled to DEBUG (e.g., botocore, requests, etc.)

@heitorlessa
Copy link
Contributor

heitorlessa commented Nov 21, 2023

TL;DR - we're still working with the AWS Lambda team on this, and related issues with the newly launched Advanced Logging Control like the warning above.

@jmehnle
Copy link

jmehnle commented May 21, 2024

@heitorlessa, any updates on how to get Python exceptions logged? I understand that the log_uncaught_exceptions feature is slated for removal, but we need some alternative method.

@heitorlessa
Copy link
Contributor

heitorlessa commented May 21, 2024 via email

@heitorlessa
Copy link
Contributor

heitorlessa commented Jun 4, 2024

HELP WANTED -- here's a backup plan that wouldn't involve a no-op Lambda internal extension.

We know nearly all customers use the decorator @logger.inject_lambda_context to enrich the Lambda context, opportunistically log incoming events, set correlation ID dynamically etc.

We could transparently update that decorator to catch all exceptions, log the exception raised if log_uncaught_exceptions=True, and re-raise them to mimic as closely as possible Python's native uncaught exception capability.

What do you think?

That way, if Lambda ever fixes it, we can transparently remove the try/catch from within the decorator code. Either way this will start working for you in the next upgrade.


Related discussion about buffer debug logs where this could come in handy too.

#4432 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-upstream Something isn't working in an upstream dependency logger
Projects
Status: Pending customer
Development

No branches or pull requests

8 participants