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
- Create a
Metrics instance with 20 default dimensions (+ service = 21 total)
- Call
setDefaultDimensions() with the same 20 keys but different values
- 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.
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()atMetrics.ts:796-813sums the current default dimension count and the new dimension count without deducting overlapping keys:If you have 21 default dimensions (including
service) and callsetDefaultDimensions()with the same 20 keys to update their values, it calculates21 + 20 = 41 >= 29and throws — even though the final count would still be 21.Code snippet
Steps to Reproduce
Metricsinstance with 20 default dimensions (+service= 21 total)setDefaultDimensions()with the same 20 keys but different valuesRangeErroreven though the final merged count would still be 21Possible Solution
Calculate the final merged count instead of naively summing:
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.