Skip to content

Bug: setDefaultDimensions() ignores regular dimensions in limit check #5206

@svozza

Description

@svozza

Expected Behavior

setDefaultDimensions() should account for all dimensions (default + regular + dimension sets) when enforcing the maximum dimension count, ensuring the total never exceeds CloudWatch's limit.

Current Behavior

setDefaultDimensions() at Metrics.ts:796-813 only checks the count of existing default dimensions against the limit, completely ignoring regular dimensions added via addDimension():

const currentCount = Object.keys(currentDefaultDimensions).length;
const newSetCount = Object.keys(newDimensions).length;
if (currentCount + newSetCount >= MAX_DIMENSION_COUNT) {
  throw new RangeError(...);
}

You can exceed CloudWatch's limit by adding 25 regular dimensions, then calling setDefaultDimensions() with 20 new keys — the check only sees 1 (service) + 20 = 21 < 29 and allows it, even though the total is now 46.

CloudWatch will silently drop the metric.

Code snippet

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

const metrics = new Metrics({ namespace: 'test' });

// Add 25 regular dimensions (plus 'service' default = 26 total)
for (let i = 0; i < 25; i++) {
  metrics.addDimension(`regular-${i}`, 'test');
}

// ❌ Should throw but doesn't — only checks default count (1) + new (20) = 21 < 29
const newDefaults: Record<string, string> = {};
for (let i = 0; i < 20; i++) {
  newDefaults[`new-default-${i}`] = 'test';
}
metrics.setDefaultDimensions(newDefaults);
// No error — but total dimensions is now 46, well above CloudWatch's limit

Steps to Reproduce

  1. Create a Metrics instance (1 default service dimension)
  2. Add 25 regular dimensions via addDimension() (total = 26)
  3. Call setDefaultDimensions() with 20 new keys
  4. Observe no error, even though total dimensions = 46

Possible Solution

Count only the genuinely new keys that don't overlap with existing defaults, then check against the full dimension count:

const newKeysCount = Object.keys(newDimensions)
  .filter((key) => !Object.hasOwn(currentDefaultDimensions, key)).length;
const totalAfterMerge = this.#dimensionsStore.getDimensionCount() + newKeysCount;
if (totalAfterMerge >= MAX_DIMENSION_COUNT) {
  throw new RangeError(...);
}

Powertools for AWS Lambda (TypeScript) version

2.33.0

AWS Lambda function runtime

22.x

Packaging format used

npm


Disclaimer: After creating an issue, please wait until it is triaged and confirmed by a maintainer before implementing it. This will reduce amount of rework and the chance that a pull request gets rejected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingconfirmedThe scope is clear, ready for implementationgood-first-issueSomething that is suitable for those who want to start contributing

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions