Summary
While adding the runtime validations for the Typescript implementation of AWS Powertools Lambda Metrics, I noticed an issue when a metric name with either an empty string or with a string longer than 255 characters was used, the metrics were not being written. If this validation is not done in the runtime, the metrics will get dropped by Cloudwatch.
Why is this needed?
As per the MetricDatum API Reference, the Metric Name has a length constraint of a minimum of length 1 and a maximum of length 255. Since, the runtime validation for the metric name is missing on the add_metric function in the cloudwatch_emf provider of the Metric utility. If we pass in an empty string or a string with more than 255 characters currently as the metric name, those metrics are being dropped by Cloudwatch silently.
Which area does this relate to?
Metrics
Solution
- Runtime Metric Name Type Validation
The Typescript implementation includes a runtime check to ensure that metric name is a string and the length constraint is satisfied. If an invalid value is passed, it raises a RangeError.
Typescript code:
if (!isString(name)) throw new Error(`${name} is not a valid string`);
if (
name.length < MIN_METRIC_NAME_LENGTH ||
name.length > MAX_METRIC_NAME_LENGTH
)
throw new RangeError(
`The metric name should be between ${MIN_METRIC_NAME_LENGTH} and ${MAX_METRIC_NAME_LENGTH} characters`
);
Typescript Source Code
Python Source - add_metric method
Acknowledgment
Summary
While adding the runtime validations for the Typescript implementation of AWS Powertools Lambda Metrics, I noticed an issue when a metric name with either an empty string or with a string longer than 255 characters was used, the metrics were not being written. If this validation is not done in the runtime, the metrics will get dropped by Cloudwatch.
Why is this needed?
As per the MetricDatum API Reference, the Metric Name has a length constraint of a minimum of length 1 and a maximum of length 255. Since, the runtime validation for the metric name is missing on the
add_metricfunction in thecloudwatch_emfprovider of theMetricutility. If we pass in an empty string or a string with more than 255 characters currently as the metric name, those metrics are being dropped by Cloudwatch silently.Which area does this relate to?
Metrics
Solution
The Typescript implementation includes a runtime check to ensure that metric name is a string and the length constraint is satisfied. If an invalid value is passed, it raises a
RangeError.Typescript code:
Typescript Source Code
Python Source - add_metric method
Acknowledgment