Skip to content

Log warning on unhandled exception. #120

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

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions awslambdaric/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def replace_line_indentation(line, indent_char, new_indent_char):

if _AWS_LAMBDA_LOG_FORMAT == LogFormat.JSON:
_ERROR_FRAME_TYPE = _JSON_FRAME_TYPES[logging.ERROR]
_WARNING_FRAME_TYPE = _JSON_FRAME_TYPES[logging.WARNING]

def log_error(error_result, log_sink):
error_result = {
Expand All @@ -128,6 +129,7 @@ def log_error(error_result, log_sink):

else:
_ERROR_FRAME_TYPE = _TEXT_FRAME_TYPES[logging.ERROR]
_WARNING_FRAME_TYPE = _TEXT_FRAME_TYPES[logging.WARNING]

def log_error(error_result, log_sink):
error_description = "[ERROR]"
Expand Down Expand Up @@ -210,6 +212,9 @@ def handle_event_request(
)

if error_result is not None:
from .lambda_literals import lambda_unhandled_exception_warning_message

log_sink.log(lambda_unhandled_exception_warning_message, _WARNING_FRAME_TYPE)
log_error(error_result, log_sink)
lambda_runtime_client.post_invocation_error(
invoke_id, to_json(error_result), to_json(xray_fault)
Expand Down
17 changes: 17 additions & 0 deletions awslambdaric/lambda_literals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
"""

lambda_warning = "LAMBDA_WARNING"

# Holds warning message that is emitted when an unhandled exception is raised during function invocation.
lambda_unhandled_exception_warning_message = str(
f"{lambda_warning}: "
"Unhandled exception. "
"The most likely cause is an issue in the function code. "
"However, in rare cases, a Lambda runtime update can cause unexpected function behavior. "
"For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. "
"To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. "
"If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. "
"For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html"
)
8 changes: 5 additions & 3 deletions awslambdaric/lambda_runtime_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ def post_invocation_result(
):
runtime_client.post_invocation_result(
invoke_id,
result_data
if isinstance(result_data, bytes)
else result_data.encode("utf-8"),
(
result_data
if isinstance(result_data, bytes)
else result_data.encode("utf-8")
),
content_type,
)

Expand Down
32 changes: 24 additions & 8 deletions tests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
from awslambdaric.lambda_runtime_exception import FaultException
from awslambdaric.lambda_runtime_log_utils import LogFormat, _get_log_level_from_env_var
from awslambdaric.lambda_runtime_marshaller import LambdaMarshaller
from awslambdaric.lambda_literals import (
lambda_unhandled_exception_warning_message,
)


class TestUpdateXrayEnv(unittest.TestCase):
Expand Down Expand Up @@ -461,7 +464,10 @@ def raise_exception_handler(json_input, lambda_context):
)

# NOTE: Indentation characters are NO-BREAK SPACE (U+00A0) not SPACE (U+0020)
error_logs = "[ERROR] FaultExceptionType: Fault exception msg\r"
error_logs = (
lambda_unhandled_exception_warning_message
+ "[ERROR] FaultExceptionType: Fault exception msg\r"
)
error_logs += "Traceback (most recent call last):\r"
error_logs += '  File "spam.py", line 3, in <module>\r'
error_logs += "    spam.eggs()\r"
Expand Down Expand Up @@ -492,7 +498,10 @@ def raise_exception_handler(json_input, lambda_context):
0,
bootstrap.StandardLogSink(),
)
error_logs = "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n"
error_logs = (
lambda_unhandled_exception_warning_message
+ "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n"
)

self.assertEqual(mock_stdout.getvalue(), error_logs)

Expand All @@ -518,7 +527,10 @@ def raise_exception_handler(json_input, lambda_context):
0,
bootstrap.StandardLogSink(),
)
error_logs = "[ERROR] FaultExceptionType\rTraceback (most recent call last):\n"
error_logs = (
lambda_unhandled_exception_warning_message
+ "[ERROR] FaultExceptionType\rTraceback (most recent call last):\n"
)

self.assertEqual(mock_stdout.getvalue(), error_logs)

Expand All @@ -544,7 +556,10 @@ def raise_exception_handler(json_input, lambda_context):
0,
bootstrap.StandardLogSink(),
)
error_logs = "[ERROR] Fault exception msg\rTraceback (most recent call last):\n"
error_logs = (
lambda_unhandled_exception_warning_message
+ "[ERROR] Fault exception msg\rTraceback (most recent call last):\n"
)

self.assertEqual(mock_stdout.getvalue(), error_logs)

Expand Down Expand Up @@ -579,8 +594,7 @@ def raise_exception_handler(json_input, lambda_context):
0,
bootstrap.StandardLogSink(),
)

error_logs = "[ERROR]\r"
error_logs = lambda_unhandled_exception_warning_message + "[ERROR]\r"
error_logs += "Traceback (most recent call last):\r"
error_logs += '  File "spam.py", line 3, in <module>\r'
error_logs += "    spam.eggs()\r"
Expand Down Expand Up @@ -616,8 +630,10 @@ def test_handle_event_request_fault_exception_logging_syntax_error(
0,
bootstrap.StandardLogSink(),
)

error_logs = f"[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'a': {syntax_error}\r"
error_logs = (
lambda_unhandled_exception_warning_message
+ f"[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'a': {syntax_error}\r"
)
error_logs += "Traceback (most recent call last):\r"
error_logs += '  File "<string>" Line 1\r'
error_logs += "    -\n"
Expand Down