Skip to content

Maintenance: Add runtime validation for the metric name #931

@sdangol

Description

@sdangol

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. There is a validation for the metric name not being empty which satisfies the minimun length constraint. But, there validation for the metric name when it is longer than 255 characters is missing on the AddMetric function in the Metrics utility. If we pass ina 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

  1. 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

.NET Source

Acknowledgment

Metadata

Metadata

Assignees

Labels

area/metricsCore metrics utilitygood first issueGood for newcomersinternalMaintenance changespending-releaseFix or implementation already in dev waiting to be released

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions