fix(metrics): enforce dimension limits per-array#5229
Conversation
2e0cfa3 to
704fcf3
Compare
This resolves double counting and limit discrepancies. Closes aws-powertools#5205
704fcf3 to
7f91fb0
Compare
dreamorosi
left a comment
There was a problem hiding this comment.
Validated the approach against the EMF specification: the spec defines the 30-dimension cap per DimensionSet, not as an aggregate across all sets, and the outer Dimensions array has no upper bound in the schema. The new Set-based projected-size check correctly mirrors what serializeMetrics actually emits (each set merged with defaultDimensions as its own array, see Metrics.ts:733-738), which makes the validation match runtime behaviour. The off-by-one unification on projectedSize > MAX_DIMENSION_COUNT is also a nice cleanup. Left a few minor observations inline — none are blockers.
Adds explanatory comments, guards against phantom dimension arrays, and clarifies test names.
|
@vishwakt looks like we're getting a CI failure due to a drop in code coverage. My guess is probably because the new guard you added doesn't have a test. |
…refactor Removes unused getDimensionCount method and adds coverage for edge-case boundary checks in setDefaultDimensions
|
|
@svozza the drop in coverage was actually a direct result of the recent refactor the |



Summary
Changes
This PR fundamentally fixes how dimension limits (
MAX_DIMENSION_COUNT) are enforced across theMetricsclass. It resolves the issue where valid dimension updates were incorrectly rejected near the limit due to overlapping keys being double-counted.What is this PR solving?
Previously, limit checks relied on
this.#dimensionsStore.getDimensionCount(), which naively summed the sizes ofdimensions,defaultDimensions, and alldimensionSets. This approach was flawed for a few reasons:dimensionSetas a completely separate array of dimensions. The CloudWatch limit of 30 dimensions applies to the maximum size of any individual array, not the sum of all dimensions defined across all sets.currentCount + newCount) meant that updating an existing key would incorrectly inflate the dimension count, causing aRangeError(issue Bug: setDefaultDimensions() double-counts overlapping keys, rejecting valid updates near the limit #5205).addDimensionallowed 29 dimensions, whereasaddDimensionsandsetDefaultDimensionsonly allowed 28 due to a>=vs>discrepancy.Fix Details:
Setto calculate the exact maximum size of the specific dimension arrays that will actually be published to CloudWatch EMF.Setautomatically deduplicates overlapping keys, updating existing dimensions inherently evaluates to a size of0new dimensions—fixing Bug: setDefaultDimensions() double-counts overlapping keys, rejecting valid updates near the limit #5205 automatically.projectedSize > MAX_DIMENSION_COUNTso exactly 29 custom dimensions are permitted across all methods (accommodating the 30th reservedservicedimension).Issue number: closes #5205
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.