- AWS Embedded Metrics are an ideal solution for generating metrics for Lambda functions that will track historical data.
- They are a method for capturing Cloudwatch metrics as part of a logging request.
- This is good because it avoids the financial and performance cost of making a putMetricData() request.
- It also makes it easier to find the point at which the metric is updated in both the logs and in the code itself.
- This does not work at all for our EC2 apps as their logs do not pass through Cloudwatch.
- This pull request gives a working example of how to embed metrics in your logging request
- This document gives a good summary of why AWS embedded metrics are so useful
- Full details can be found in the AWS Documentation, but here are the highlights:
- To use AWS Embedded metrics, logs must be in JSON format.
- A metric is embedded in a JSON logging request by adding a root node named “_aws” to the start of the log request.
- The metric details are defined within this "_aws" node.
- The following code snippet shows a logging request updating a single metric:
{"_aws": {
"Timestamp": 1574109732004,
"CloudWatchMetrics": [
{
"Namespace": "lambda-function-metrics",
"Dimensions": [["functionVersion"]],
"Metrics": [
{
"Name": "time",
"Unit": "Milliseconds"
}
]
}
]
},
"functionVersion": "$LATEST",
"time": 100,
"requestId": "989ffbf8-9ace-4817-a57c-e4dd734019ee"
}
- Within the "_aws" node there is a "timestamp" field and a "CloudwatchMetrics" field
- The "timestamp" field is in epoch milliseconds format
- The "CloudwatchMetrics" field contains an array of CloudwatchMetric objects
- Each CloudwatchMetrics object contains the "Namespace", "Dimensions" and "Metrics" fields
- "Namespace" is a string matching the namespace all metrics in the request are listed under.
- "Dimensions" is an array of DimensionSet objects.
- If you do not have any Dimensions you must still define the field and pass an empty nested array "[[]]".
- The metrics field contains an array of MetricDefinition objects. These contain the "Name" and "Unit" for the metric.
- The "Name" field of the metric must correspond with the name of a field in the body of the logging request.
This field will hold the value for the metric.
- Metrics sent by this request must correspond with the logging objects contained in the request.
- The value of the metric is not specified in the metric definition,
instead the metric name is used as a reference to look up the value of the corresponding field in the logging request.
- Further reading: