Skip to content

Bug: setDefaultDimensions() double-counts overlapping keys, rejecting valid updates near the limit #5205

@svozza

Description

@svozza

Expected Behavior

Calling setDefaultDimensions() with keys that already exist in the current default dimensions should be allowed, since it's updating values — not increasing the total dimension count. The limit check should be based on the final merged count, not the naive sum of current + new.

Current Behavior

setDefaultDimensions() at Metrics.ts:796-813 sums the current default dimension count and the new dimension count without deducting overlapping keys:

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

If you have 21 default dimensions (including service) and call setDefaultDimensions() with the same 20 keys to update their values, it calculates 21 + 20 = 41 >= 29 and throws — even though the final count would still be 21.

Code snippet

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

const defaultDimensions: Record<string, string> = {};
for (let i = 0; i < 20; i++) {
  defaultDimensions[`dim-${i}`] = 'value';
}

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

// ❌ Throws RangeError even though we're just updating existing keys
const updatedDimensions: Record<string, string> = {};
for (let i = 0; i < 20; i++) {
  updatedDimensions[`dim-${i}`] = 'new-value';
}
metrics.setDefaultDimensions(updatedDimensions);
// RangeError: The number of metric dimensions must be lower than 29

Steps to Reproduce

  1. Create a Metrics instance with 20 default dimensions (+ service = 21 total)
  2. Call setDefaultDimensions() with the same 20 keys but different values
  3. Observe RangeError even though the final merged count would still be 21

Possible Solution

Calculate the final merged count instead of naively summing:

const merged = { ...currentDefaultDimensions, ...newDimensions };
if (Object.keys(merged).length >= 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