Monitor AWS Lambda OpenTelemetry Traces With Datadog | Datadog

Monitor AWS Lambda OpenTelemetry traces with Datadog

Author Alex Cuoci
Author Kai Xin Tai

Published: April 29, 2021

Datadog’s support of OpenTelemetry—a vendor-agnostic, open source set of APIs and libraries for collecting system and application telemetry data—has helped thousands of organizations implement monitoring strategies that complement their existing workflows. Many of our customers leverage OpenTelemetry for their server- and container-based deployments, but also need visibility into the health and performance of their serverless applications running on AWS Lambda.

Today, we’re excited to partner with AWS for the launch of their managed Lambda Layer for OpenTelemetry, which extends OpenTelemetry tracing to Lambda. Developed as a Lambda Extension, the Layer contains the OpenTelemetry SDK (for Python, JavaScript, or Java) as well as the OpenTelemetry Collector—and lets you easily send traces to Datadog for monitoring and analysis.

AWS managed Lambda Layer for OpenTelemetry as a Lambda Extension

Lambda Extensions, released as a preview late last year, offers a new and seamless way for monitoring and security tools to plug into the Lambda execution environment. Before Lambda Extensions, third-party tools could not integrate easily with Lambda without adding latency or cost.

The AWS managed Lambda Layer for OpenTelemetry (built as a Lambda Extension) lets you collect OpenTelemetry traces from your Python, Node.js, and Java Lambda functions in the background, when they are not actively responding to events. The Layer contains the OpenTelemetry SDK, which generates traces, and the OpenTelemetry Collector, which sends those traces to AWS X-Ray via the OpenTelemetry Collector X-Ray Exporter. AWS has carefully developed and tested the Layer so your teams don’t have to worry about maintaining it or optimizing its performance—and can focus on building out your application.

In the next section, we’ll walk through how to instrument your Python functions with the managed Lambda Layer. If you’re looking to instrument your Node.js or Java functions, see the official AWS documentation.

Trace functions in Datadog with the AWS managed Lambda Layer for OpenTelemetry

First, add the managed Lambda Layer for OpenTelemetry to your Python function (arn:aws:lambda:<AWS_REGION>:901920570463:layer:aws-otel-python38-ver-1-1-0:1) via the AWS Management Console, the AWS Command Line Interface (AWS CLI), or an infrastructure-as-code tool such as the Serverless Framework or AWS Serverless Application Model (SAM). You can use this sample function, if you don’t already have one set up.

Next, add the environment variable AWS_LAMBDA_EXEC_WRAPPER: /opt/otel-instrument to trace your Lambda function without any application code changes, and enable X-Ray Active Tracing on the function. Then, install our AWS 1-click integration to forward traces to Datadog.

Once you’ve enabled our integration, you should begin to see OpenTelemetry traces from your Lambda function flowing into your Datadog account. X-Ray automatically stitches together each OpenTelemetry trace with X-Ray segments from the other AWS components that were involved in the request (e.g., Amazon API Gateway), so you can easily pinpoint bottlenecks.

View OpenTelemetry traces of your Lambda applications in Datadog

View traces in context with enhanced metrics and logs

Traces are useful for troubleshooting an issue, but viewing them alongside Lambda metrics and logs gives you the context you need to identify the root cause as quickly as possible.

To generate and forward enhanced Lambda metrics and logs to Datadog, you’ll need to install and configure two additional Lambda Layers:

Next, add the following environment variables:

  • DD_LAMBDA_HANDLER: lambda_function.lambda_handler

You can find your Datadog API key here. If you need to create one, see our documentation for instructions.

Lastly, modify your Lambda function handler to datadog_lambda.handler.handler.

Once you’ve completed these steps, you’ll be able to visualize key Lambda metrics (e.g., errors, timeouts, memory usage) together with OpenTelemetry traces and Lambda logs in our Serverless View. In addition, our enhanced metrics contain additional function metadata—and are collected at a higher granularity and lower latency than standard CloudWatch metrics—so you can investigate issues as soon as they crop up.

Datadog’s Serverless view unifies your Lambda metrics, traces, and logs, giving you all the context you need to troubleshoot issues

Native tracing with Datadog APM

Datadog customers can choose to trace their Lambda functions with either OpenTelemetry and X-Ray or our native tracing libraries (dd-trace). Datadog’s tracing libraries automatically propagate trace context across service boundaries, so you get end-to-end visibility into every request, regardless of whether it flows through functions, containers, or other infrastructure components. These traces appear in your account in real time, and no Lambda function code changes are required to begin collecting them.

If you’d like to learn more about Datadog Serverless monitoring and our native tracing capabilities, see our dedicated blog post.

Start monitoring AWS Lambda OpenTelemetry traces today

At Datadog, we’re constantly working to deliver comprehensive visibility into your serverless environment in a way that minimizes cost, operational complexity, and performance impact. The AWS managed Lambda Layer for OpenTelemetry, together with our AWS X-Ray integration, enables you to collect traces from your Lambda functions without any changes to your function code. To learn more, visit the AWS Distro for OpenTelemetry developer portal. And if you’re new to Datadog, sign up for a 14-day today.