Description
Expected Behavior
When a line of code raises outside of the handler function, then datadog
should be able to detect the error.
Actual Behavior
When a line of code raises outside of the handler function, then datadog
is not able to detect the error.
==> If an monitor (attached to a Slack alert) is set up when an exception is raised (and not catched) on this lambda, then the corresponding monitor is not triggered and no Slack message is sent.
Steps to Reproduce the Problem
Define the following Lambda function:
import json
# The following line will raise on purpose
0/0
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Set
- The AWS handler to
datadog_lambda.handler.handler
, and - The
DD_LAMBDA_HANDLER
environment variable to<your_file>.lambda_handler
Run the lambda.
==> Even if the line 0/0
raises, no trace will be visible in the Invocation Serverless part of Datadog.
Note we see invocations on top left chart (3 blue vertical bars), but there is nothing in the center panel (No traced invocation in the time window
), no way to see the traces, the Python stack trace ...
If we move the 0/0
in the handler, like below:
import json
def lambda_handler(event, context):
# The following line will raise on purpose
0/0
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
then Datadog behaves correctly (visible Traces, Monitor, Slack Message ...)
Specifications
Additional information
I understand we specify the handler to Datadog, and thus cannot be aware of things running out of the handler, but as indicated in AWS best practices, there is benefits to run some code out of the handler. If this code fails, it is very important that the developer team is notified.
Take advantage of execution environment reuse to improve the performance of your function. Initialize SDK clients and database connections outside of the function handler, and cache static assets locally in the /tmp directory. Subsequent invocations processed by the same instance of your function can reuse these resources. This saves cost by reducing function run time.