iTranslated by AI
Storing Metrics with AWS Embedded Metrics
Introduction
I recently checked out the AWS Lambda Performance Tuning Deep Dive ~ This is what I really wanted to know ~ (AWS-46) session from the recent AWS Summit, and it was excellent. So, I tried using AWS Embedded Metrics, which was introduced in the session.
Summary
- You can emit metrics via CloudWatch Logs using the aws-embedded-metrics-node library.
- Since there is library support, it's easier to write than using the AWS SDK to put metrics directly into CloudWatch Metrics.
- Because you can freely add information as logs, you can use CloudWatch Logs Insights to analyze metrics under more detailed conditions.
What is Embedded Metric Format?
Embedded Metric Format is a JSON specification that allows you to instruct CloudWatch Logs to automatically emit metrics embedded in logs to CloudWatch Metrics.
Specification: Embedded metric format
{
"_aws": {
"CloudWatchMetrics": [
{
"Metrics": [
{
"Name": "Time",
"Unit": "Milliseconds"
}
],
...
}
]
},
"Time": 1
}
The aws-embedded-metrics-node library indirectly emits metrics by generating logs that comply with this specification.
aws-embedded-metrics library
The aws-embedded-metrics library supports four languages: Node.js (with type definitions), Python, Java, and C#.
Using the client libraries to generate embedded metric format logs - Amazon CloudWatch
Sample
I created a Web API that returns empty data using API Gateway and Lambda. I am using TypeScript for the language.
Dummy data is used for the metrics.
import {APIGatewayProxyHandler} from 'aws-lambda';
import {metricScope, Unit} from 'aws-embedded-metrics';
const getRandom = (min: number, max: number): number =>
Math.floor(Math.random() * (max + 1 - min)) + min;
const getFruit = () => {
const fruits = ['apple', 'banana', 'orange'];
return fruits[getRandom(0, fruits.length - 1)];
};
export const handler: APIGatewayProxyHandler = metricScope(
metrics => async () => {
metrics.setNamespace('DemoAwsEmbeddedMetrics');
metrics.putDimensions({Service: 'MyService'});
metrics.putMetric(
'ProcessingLatency',
getRandom(50, 150),
Unit.Milliseconds
);
metrics.putMetric('Warnings', getRandom(0, 10), Unit.Count);
metrics.setProperty('Fruit', getFruit());
return {
statusCode: 200,
body: '',
};
}
);
Recorded Metrics and Logs
I continuously accessed the created API to generate logs. I used watch and curl for this.
watch -n1 curl https://${APIGW_ENDPOINT_URL}
The logs are stored in CloudWatch Logs. Even if putMetric is called multiple times, it seems they are consolidated into a single log entry. Since this is for recording metrics, it might be designed this way; it's probably better to think of this library as something separate from a standard logger.

Metrics have been generated in CloudWatch Metrics.

Furthermore, I'll use CloudWatch Logs Insights to analyze the logs according to the following conditions:
- Target only Fruit=orange
- Aggregate the average of Warnings, average of ProcessingLatency, and access count in 5-second intervals.
fields @timestamp, @message
| filter Fruit="orange"
| stats avg(Warnings), avg(ProcessingLatency), count(*) by bin(5s)

While this example uses a hypothetical classification like "fruit", it seems very useful for filtering by paths or other real-world attributes in actual environments.
Afterword
I had seen CloudWatch Metrics and Dashboards mentioned in the context of observability, but I had never actually used them myself, so I'm glad I could finally try them out. It really made me want to record business metrics more and more.
That's it!
Discussion