Skip to content

Bug: dimension value not being validated for empty strings #3302

Closed
@dreamorosi

Description

@dreamorosi

Expected Behavior

When adding a dimension to my metrics, I should get a warning if the dimension value is invalid aka an empty string ("") or undefined/null, and the dimension should not be added to the EMF blobs emitted by the utility.

Current Behavior

Currently the utility doesn't validate the input but only enforces types. In practice this means it's unlikely customers can pass undefined or null unless they're suppressing the warning, but can pass an empty string and generate an EMF blob that will have the invalid dimension.

Code snippet

import { Metrics } from '@aws-lambda-powertools/metrics';

const metrics = new Metrics();

export const handler = async () => {
  metrics.addDimension('FunctionName', '');
  metrics.addMetric('CustomMetric', 'Count', 1);
  metrics.publishStoredMetrics();
};

Steps to Reproduce

  1. Run the code above
  2. Observe that the EMF blob emitted has an empty dimension
  3. Verify that the processed metric in CloudWatch does not include any dimensions

Possible Solution

We should: 1/ validate the input when adding a dimension, 2/ emit a warning if invalid, and 3/ don't add the dimension to the EMF blob

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

20.x

Packaging format used

npm

Execution logs

{
  "_aws": {
    "Timestamp": 1731086204245,
    "CloudWatchMetrics": [
      {
        "Namespace": "default_namespace",
        "Dimensions": [
          [
            "service",
            "FunctionName"
          ]
        ],
        "Metrics": [
          {
            "Name": "CustomMetric",
            "Unit": "Count"
          }
        ]
      }
    ]
  },
  "service": "service_undefined",
  "FunctionName": "",
  "CustomMetric": 1
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingconfirmedThe scope is clear, ready for implementationgood-first-issueSomething that is suitable for those who want to start contributinghelp-wantedWe would really appreciate some support from community for this onemetricsThis item relates to the Metrics Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions