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
- Create a
Metrics instance (1 default service dimension)
- Add 25 regular dimensions via
addDimension() (total = 26)
- Call
setDefaultDimensions() with 20 new keys
- 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.
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()atMetrics.ts:796-813only checks the count of existing default dimensions against the limit, completely ignoring regular dimensions added viaaddDimension():You can exceed CloudWatch's limit by adding 25 regular dimensions, then calling
setDefaultDimensions()with 20 new keys — the check only sees1 (service) + 20 = 21 < 29and allows it, even though the total is now 46.CloudWatch will silently drop the metric.
Code snippet
Steps to Reproduce
Metricsinstance (1 defaultservicedimension)addDimension()(total = 26)setDefaultDimensions()with 20 new keysPossible Solution
Count only the genuinely new keys that don't overlap with existing defaults, then check against the full 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.