Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/metrics/src/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class Metrics extends Utility implements MetricsInterface {
);
return this;
}
if (MAX_DIMENSION_COUNT <= this.#dimensionsStore.getDimensionCount()) {
if (this.#dimensionsStore.getDimensionCount() >= MAX_DIMENSION_COUNT) {
throw new RangeError(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
Expand Down Expand Up @@ -282,7 +282,7 @@ class Metrics extends Utility implements MetricsInterface {
const newDimensions = this.#sanitizeDimensions(dimensions);
const currentCount = this.#dimensionsStore.getDimensionCount();
const newSetCount = Object.keys(newDimensions).length;
if (currentCount + newSetCount >= MAX_DIMENSION_COUNT) {
if (currentCount + newSetCount > MAX_DIMENSION_COUNT) {
throw new RangeError(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
Expand Down Expand Up @@ -802,7 +802,7 @@ class Metrics extends Utility implements MetricsInterface {
(key) => !Object.hasOwn(currentDefaultDimensions, key)
).length;
if (
this.#dimensionsStore.getDimensionCount() + newKeysCount >=
this.#dimensionsStore.getDimensionCount() + newKeysCount >
MAX_DIMENSION_COUNT
) {
throw new RangeError(
Expand Down
96 changes: 90 additions & 6 deletions packages/metrics/tests/unit/dimensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,96 @@ describe('Working with dimensions', () => {
metrics.addDimension(`dimension-${i}`, 'test');
}

// Assess
// Assess — the 29th dimension (at exactly MAX_DIMENSION_COUNT) is allowed,
// but the 30th should throw
expect(() => metrics.addDimension('extra', 'test')).toThrowError(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
});

it('allows adding up to MAX_DIMENSION_COUNT dimensions with addDimension', () => {
// Prepare
const metrics = new Metrics({
singleMetric: true,
defaultDimensions: {
environment: 'test',
},
});

// Act — fill up to 28 dimensions (2 default + 26 regular)
// i=2..27 gives 26 iterations, total = 2 + 26 = 28
for (let i = 2; i < MAX_DIMENSION_COUNT - 1; i++) {
metrics.addDimension(`dimension-${i}`, 'test');
}
// This is the 29th dimension — should NOT throw
metrics.addDimension('final', 'test');

// Assess — next one should throw (would be 30th)
expect(() => metrics.addDimension('extra', 'test')).toThrowError(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
});

it('allows adding up to MAX_DIMENSION_COUNT dimensions with addDimensions', () => {
// Prepare
const metrics = new Metrics({
singleMetric: true,
defaultDimensions: {
environment: 'test',
},
});

// Act — fill up to 28 dimensions (2 default + 26 regular)
for (let i = 2; i < MAX_DIMENSION_COUNT - 1; i++) {
metrics.addDimension(`dimension-${i}`, 'test');
}
// Adding 1 more dimension via addDimensions to reach 29 — should NOT throw
expect(() =>
metrics.addDimensions({ final: 'test' })
).not.toThrow();

// Assess — adding one more should throw
expect(() =>
metrics.addDimensions({ extra: 'test' })
).toThrowError(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
});

it('addDimension and addDimensions enforce the same boundary consistently', () => {
// Prepare — two identical metric instances
const metricsA = new Metrics({
singleMetric: true,
defaultDimensions: {
environment: 'test',
},
});
const metricsB = new Metrics({
singleMetric: true,
defaultDimensions: {
environment: 'test',
},
});

// Act — fill both to 28 dimensions (2 default + 26 regular)
for (let i = 2; i < MAX_DIMENSION_COUNT - 1; i++) {
metricsA.addDimension(`dimension-${i}`, 'test');
metricsB.addDimension(`dimension-${i}`, 'test');
}

// Both should allow the 29th dimension
expect(() => metricsA.addDimension('final', 'test')).not.toThrow();
expect(() => metricsB.addDimensions({ final: 'test' })).not.toThrow();

// Both should reject the 30th dimension
expect(() => metricsA.addDimension('extra', 'test')).toThrowError(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
expect(() => metricsB.addDimensions({ extra: 'test' })).toThrowError(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
});

it('throws when the number of dimensions exceeds the limit after adding default dimensions', () => {
// Prepare
const metrics = new Metrics({
Expand All @@ -351,11 +435,11 @@ describe('Working with dimensions', () => {

// Act
// We start with 1 dimension because service name is already added
for (let i = 1; i < MAX_DIMENSION_COUNT - 1; i++) {
for (let i = 1; i < MAX_DIMENSION_COUNT; i++) {
metrics.setDefaultDimensions({ [`dimension-${i}`]: 'test' });
}

// Assess
// Assess — at 29 dimensions, adding one more should throw
expect(() => metrics.setDefaultDimensions({ extra: 'test' })).toThrowError(
'The number of metric dimensions must be lower than 29'
);
Expand All @@ -367,12 +451,12 @@ describe('Working with dimensions', () => {
singleMetric: true,
});

// Act
for (let i = 1; i < MAX_DIMENSION_COUNT - 1; i++) {
// Act — fill up to MAX_DIMENSION_COUNT (29) dimensions
for (let i = 1; i < MAX_DIMENSION_COUNT; i++) {
metrics.addDimension(`regular-${i}`, 'test');
}

// Assess
// Assess — at 29 dimensions, adding one more should throw
expect(() =>
metrics.setDefaultDimensions({ 'new-default': 'test' })
).toThrow(
Expand Down