Skip to content

Bug: addDimensions() is one dimension more restrictive than addDimension() at the boundary #5204

@svozza

Description

@svozza

Expected Behavior

addDimension() and addDimensions() should enforce the same maximum dimension limit. When there are 28 dimensions and a user adds 1 more, both methods should either allow or reject it consistently.

Current Behavior

addDimension() uses <= (Metrics.ts:250):

if (MAX_DIMENSION_COUNT <= this.#dimensionsStore.getDimensionCount()) {

addDimensions() uses >= (Metrics.ts:285):

if (currentCount + newSetCount >= MAX_DIMENSION_COUNT) {

At the boundary (28 existing dimensions, adding 1 more to reach the MAX_DIMENSION_COUNT of 29), addDimension() allows it but addDimensions() throws a RangeError.

Code snippet

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

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

// Fill both to 28 dimensions (1 default "service" + 27 regular)
for (let i = 1; i < 28; i++) {
  metricsA.addDimension(`dimension-${i}`, 'test');
  metricsB.addDimension(`dimension-${i}`, 'test');
}

// ✅ addDimension allows the 29th dimension
metricsA.addDimension('final', 'test');

// ❌ addDimensions rejects the 29th dimension
metricsB.addDimensions({ final: 'test' });
// RangeError: The number of metric dimensions must be lower than 29

Steps to Reproduce

  1. Create a Metrics instance (which auto-adds service as a default dimension)
  2. Add 27 dimensions via addDimension() to reach 28 total
  3. Call addDimensions({ final: 'test' }) to add the 29th
  4. Observe RangeError — while addDimension('final', 'test') would have succeeded

Possible Solution

Change the >= to > in addDimensions() at Metrics.ts:285 to match the behavior of addDimension():

if (currentCount + newSetCount > MAX_DIMENSION_COUNT) {

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