Datadog's Lambda Layer: Monitor custom serverless metrics
To build applications in AWS Lambda, you often need to use third party libraries and packages in your function code. Previously, these packages had to be included in a function’s deployment package. Today, Amazon Web Services released a new feature called Layers to simplify this process for Lambda developers. Layers allow you to deploy common components that you can reuse across functions, such as machine learning models, SDKs, or instrumentation libraries. The Lambda service merges your layers with the function code automatically whenever the function is created or updated, reducing the complexity of keeping libraries in sync across all your functions.
We are pleased to announce that a Datadog instrumentation Layer is available at launch. The new Datadog Layer for Python (version 2.7, 3.6, and 3.7) allows you to send custom metrics and events from your Lambda functions to Datadog more easily and efficiently than ever.
Custom metrics for serverless monitoring
While Datadog collects standard Lambda metrics emitted from CloudWatch, tracking custom metrics gives you insight into actions or performance indicators that are unique to your use case and application. For example, you can instrument your functions to report metrics for actions such as customer logins, video plays, or completed purchases. These custom metrics allow you to rapidly identify any business-impacting issues in your serverless applications, which you can then troubleshoot using Lambda logs and distributed tracing in Datadog’s new Cloud Functions view.
Set up the Datadog Layer
To add the Datadog Layer, navigate to the Lambda function that you want to instrument in the AWS console.
Click on “Layers” on the main page of your function.
Scroll down, and click on “Provide a layer version ARN.” Enter an ARN of the following form, providing your desired region and Python version (e.g.
Python36 for 3.6):
Navigate to the “Environment Variables” section of your function and set two environment variables:
You can find these values on the API page within the Datadog application.
Use the Datadog Layer to send monitoring data
With the Datadog Layer now added to the function, you can send custom metrics to Datadog. In your function code, import the necessary methods from the Layer and add a wrapper around your function handler:
from datadog import datadog_lambda_wrapper, lambda_metric @datadog_lambda_wrapper
Now, simply call
lambda_metric(metric_name, metric_value, tags=) to send metrics to Datadog. In the example below, we are sending a metric named
lambda_custom_metric with 10 different values (0–9) and the tags
env:demo. Behind the scenes, the Datadog Layer will collect these points and send them to Datadog once using ThreadStats. This technique has minimal impact on the overall performance of your Lambda function.
from datadog import datadog_lambda_wrapper, lambda_metric @datadog_lambda_wrapper def lambda_handler(event, context): for i in range(0, 10): lambda_metric("lambda_custom_metric",i, tags=['owner:daniel', 'env:demo']) return "metrics sent to Datadog!"
Within seconds, these metrics will appear in Datadog, where you can use them in dashboards, notebooks, alerts, and more. Metrics sent from the Datadog Layer will automatically be aggregated into distributions, so you can graph the average, sum, max, min, and count, as well as 50th, 75th, 95th, and 99th percentile values. Detailed information on sending custom metrics from Lambda functions can be found in our documentation.
Start monitoring Lambda with Datadog
If you don’t yet have a Datadog account, you can start a free, full-featured trial today to get deep visibility into your applications, infrastructure, and Lambda functions in one platform.