Description
Use case
Amazon CloudWatch announced support for high resolution metric extraction from structured logs (EMF). Customers can now provide an optional StorageResolution
parameter within the EMF specification with a value of 1 or 60 (default) to indicate the desired resolution (in seconds) of the metric.
We should consider adding support for this new optional parameter to Metrics.
The EMF log should have this format:
{
"_aws": {
"CloudWatchMetrics": [
{
"Metrics": [
{
"Name": "Time",
"Unit": "Milliseconds",
"StorageResolution": 60 // <- new key
}
],
...
}
]
},
"Time": 1
}
As part of this issue we should also update the API docs, documentation, and unit/integration tests.
Solution/User Experience
Proposed DX:
import { Metrics, MetricUnits, MetricResolutions } from '@aws-lambda-powertools/metrics';
const metrics = new Metrics();
export const handler = async (_event: any, _context: any): Promise<void> => {
// Publish a metric with standard resolution i.e. StorageResolution = 60
metrics.addMetric('successfulBooking', MetricUnits.Count, 1, MetricResolutions.Standard);
// Publish a metric with high resolution i.e. StorageResolution = 1
metrics.addMetric('failedBooking', MetricUnits.Count, 1, MetricResolutions.High);
// The last parameter (storage resolution) is optional
metrics.addMetric('successfulUpgrade', MetricUnits.Count, 1);
};
To support the proposal, we should add a new MetricResolutions
enum that looks similar to this:
enum MetricResolutions {
Standard = 60,
High = 1,
}
supported by types like:
type MetricResolutionStandard = MetricResolutions.Standard;
type MetricResolutionHigh = MetricResolutions.High;
type MetricResolution = MetricResolutionStandard | MetricResolutionHigh;
export {
MetricResolution,
MetricResolutions,
};
as well as add a new optional parameter to the Metrics.addMetric()
method, which would change its signature to:
public addMetric(name: string, unit: MetricUnit, value: number, storageResolution?: MetricResolution): void
Note
The EMF specification states that if a value is not provided, then a default value of 60 is assumed (aka standard resolution). For this reason, if thestorageResolution
parameter is not specified we won't add the resolution.
The change will likely also require modifying the logic of the Metrics.storeMetric
private method to allow the new optional field.
Alternative solutions
No response
Acknowledgment
- This feature request meets Lambda Powertools Tenets
- Should this be considered in other Lambda Powertools languages? i.e. Python, Java