When building serverless applications on AWS Lambda, Amazon CloudWatch provides out-of-the-box metrics that measure the performance, errors, and duration of your functions. Although these standard Lambda metrics provide visibility into your serverless applications, it can also be invaluable to monitor custom metrics that are unique to your use case and application. Custom metrics enable you to report on actions such as customer logins, video plays, completed purchases, or any other information that’s important to your business. In this blog post, we’ll cover how to:
- Collect custom business metrics with the Lambda extension
- Generate custom metrics from logs and traces
- Use custom metrics to rapidly identify and troubleshoot any business-impacting issues
Collect custom business metrics
The Datadog Lambda extension runs within your Lambda execution environment and enables you to send custom and enhanced metrics, traces, and logs directly to Datadog. The extension supports Node.js, Python, Ruby, Go, Java, and .NET runtimes. Follow these instructions to set up the extension to work in your serverless environment.
Once you’ve installed the extension, you can begin configuring and forwarding custom metrics to Datadog. The Python example below shows how a coffee shop could instrument a function to send a custom metric, coffee_house.order_value
, to track the value of each order and tag it with relevant information such as product and order type.
from datadog_lambda.metric import lambda_metric
def lambda_handler(event, context):
lambda_metric(
"coffee_house.order_value", # Metric name
12.45, # Metric value
tags=['product:latte', 'order:online'] # Associated tags
)
Within seconds, your custom metric will appear in Datadog, where you can use it in dashboards, notebooks, monitors, and more. Metrics sent from the Datadog extension 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. You can learn more about distribution metrics and sending custom metrics from Lambda functions in our documentation.
Generate custom metrics from logs and traces
If you’re using the Datadog Lambda extension to collect traces and logs from your Lambda functions, you can also generate custom metrics from that data without redeploying or rewriting your application code. For example, you can search for plain text messages within your logs, such as a “FREESHIPPING” coupon code, and generate a new metric from this query to track its popularity, as shown below. You can also generate metrics around log attributes, such as merchant ID and customer location, for insight into which sellers and cities generate the most customer traffic for your web store.
For additional insights into your serverless application, you can generate custom metrics from your traces to track metrics such as error rates and latency of customer checkouts. Span-based metrics are retained for 15 months, enabling you to get long-term insights into your business.
Troubleshoot business-impacting issues in your serverless application
Once you’re collecting custom metrics from your AWS Lambda applications, you can use them just like any other metric to create useful dashboards, monitors, and SLOs. Configuring monitors for custom metrics and other data from your serverless applications can help you swiftly detect and troubleshoot issues before they negatively affect your business. For example, you can configure a monitor to automatically notify you about anomalous trends in checkout errors.
Once you receive an alert, you can navigate to the Serverless view to identify and troubleshoot potential root causes. You can filter your Lambda functions by name, AWS account, region, runtime, and other metadata, or search for functions tagged with errors, cold starts, and timeouts. For example, if you’re receiving the usual volume of API requests, but the number of successful checkouts is unusually low, you can filter for errors and then click to inspect a flame graph for a request, as shown below. Datadog provides full visibility into AWS Lambda request and response payloads so you can get deep insight for troubleshooting.
Start monitoring Lambda with Datadog
Instrumenting your serverless functions to send custom metrics enables you to leverage Datadog to visualize, alert on, and troubleshoot data specific to your business. You can get started by installing the Datadog Lambda extension to begin collecting custom metrics. If you don’t yet have a Datadog account, you can start a free, full-featured trial today to get deep visibility into your serverless applications and infrastructure in one platform.