Datadog's Lambda Layer: Monitor custom serverless metrics | Datadog

Datadog's Lambda Layer: Monitor custom serverless metrics

Author Daniel Langer

Last updated: August 22, 2019

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 Serverless 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.

Choosing a layer in the Lambda Layers UI

Scroll down, and click on “Provide a layer version ARN.” Enter an ARN in the format described in the Lambda integration documentation.

Selecting the Datadog Layer in the AWS console

Navigate to the “Environment Variables” section of your function, add DD_API_KEY to the “Key” column, and use your Datadog API key as the value.

You can find your API key within Datadog.

Specifying your Datadog API key as an environment variable

Use the Datadog Layer to send monitoring data

With the Datadog Layer 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_lambda.metric import lambda_metric
from datadog_lambda.wrapper import datadog_lambda_wrapper

@datadog_lambda_wrapper
<YOUR_FUNCTION_HANDLER>

Now, simply call lambda_metric(metric_name, metric_value, tags=[]) within your function handler to send metrics to Datadog. The Layer will emit your metrics asynchronously as logs using our Lambda-based log forwarder, and Datadog will ingest them as metrics automatically. This means that you can get visibility into your Lambda functions with minimal overhead for metrics collection.

Collect custom metrics and events from functions with Datadog's Lambda layer.

In the example below, we are sending a metric named coffee_house.order_value with a fixed value (12.45) and the tags product:latte and order:online.

from datadog_lambda.metric import lambda_metric
from datadog_lambda.wrapper import datadog_lambda_wrapper

@datadog_lambda_wrapper
def lambda_handler(event, context):
    lambda_metric(
        "coffee_house.order_value",             # Metric name
        12.45,                                  # Metric value
        tags=['product:latte', 'order:online']  # Associated tags
    )

If you’re working in Node.js or Ruby, you can import a similar wrapper to send custom metrics easily from your functions.

const { datadog, sendDistributionMetric } = require("datadog-lambda-js");

async function myHandler(event, context) {
  sendDistributionMetric(
    "coffee_house.order_value", // Metric name
    12.45,                      // Metric value
    "product:latte",            // Associated tag
    "order:online",             // Another tag
  );
  return {
    statusCode: 200,
    body: "hello, dog!",
  };
}
// Wrap your handler function:
module.exports.myHandler = datadog(myHandler);

Datadog also supports Lambda functions written in Golang and Java. To instrument functions written in Golang, import the datadog-lambda-go package (as well as the standard lambda package for AWS Lambda), and add the package’s wrapper function to your handler. In order to instrument Java Lambda functions, include the datadog-lambda-java client library in your project workspaces.

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 today to get deep visibility into your applications, infrastructure, and Lambda functions in one platform.