Send custom AWS Lambda metrics to Amazon CloudWatch over OTLP

AWS Lambda with ADOT layer → Amazon CloudWatch OTLP endpoint

Record custom metrics in AWS Lambda with the OpenTelemetry metrics API and send them to the Amazon CloudWatch OTLP endpoint via the ADOT layer.

This pattern deploys a Python AWS Lambda function with the AWS Distro for OpenTelemetry (ADOT) Lambda layer. The function records custom metrics with the OpenTelemetry metrics API. A collector in the layer receives them over local OTLP, signs each request with SigV4 and sends them to the Amazon CloudWatch OTLP endpoint. The function makes no AWS SDK calls for metrics and needs only cloudwatch:PutMetricData beyond basic logging permissions.
Compared with PutMetricData or embedded metric format logs, OTLP has no per-series charge and allows up to 150 labels per data point instead of 30 dimensions, so it suits high-cardinality metrics such as per-tenant or business metrics. Keep label values bounded, and don't use request IDs or UUIDs as labels.
Three details are easy to get wrong. The collector in the ADOT layer has no processors, so naming one such as batch stops it from starting. The function flushes the meter provider before it returns, because Lambda freezes the environment straight after. The metrics are queried with PromQL in Query Studio, not in the classic CloudWatch metrics console.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/lambda-cloudwatch-otlp-metrics/python/sam

Deploy

sam deploy --guided


Testing

Invoke the function a few times with the test event in events/order.json, wait one to two minutes, then query the counter with PromQL using the signed request script in the pattern folder, or in Query Studio in the CloudWatch console. See the README for detailed instructions.

Cleanup

Delete the stack with sam delete --stack-name STACK_NAME --region $REGION and enter y at each confirmation prompt, confirm it with aws cloudformation list-stacks, then delete the local out.json response file. The shared AWS SAM CLI managed artifacts bucket and the optional account and Region wide enrichment settings are not created by the stack and are left in place. Metric data points that were already ingested cannot be deleted and age out under the included retention.

Created by:

Manish S

Manish S

AWS Support Engineer, trying to build things