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
14 changes: 0 additions & 14 deletions packages/metrics/src/DimensionsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,6 @@ class DimensionsStore {
this.#defaultDimensions = {};
}

public getDimensionCount(): number {
const dimensions = this.#getDimensions();
const dimensionSets = this.#getDimensionSets();
const dimensionSetsCount = dimensionSets.reduce(
(total, dimensionSet) => total + Object.keys(dimensionSet).length,
0
);
return (
Object.keys(dimensions).length +
Object.keys(this.#defaultDimensions).length +
dimensionSetsCount
);
}

public setDefaultDimensions(dimensions: Dimensions): void {
this.#defaultDimensions = { ...dimensions };
}
Expand Down
58 changes: 45 additions & 13 deletions packages/metrics/src/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,22 @@ class Metrics extends Utility implements MetricsInterface {
);
return this;
}
if (MAX_DIMENSION_COUNT <= this.#dimensionsStore.getDimensionCount()) {
const dimensions = this.#dimensionsStore.getDimensions();
const defaultDimensions = this.#dimensionsStore.getDefaultDimensions();
// addDimension only mutates the regular dimensions array, so we don't need to project against dimensionSets
// (each set is an independent published array).
const projectedSize = new Set([
...Object.keys(defaultDimensions),
...Object.keys(dimensions),
name,
]).size;
// MAX_DIMENSION_COUNT is 29 (EMF 30 dimension cap - 1 reserved for service).
// Thus exactly 29 custom dimensions are allowed, and we throw only if strictly greater.
if (projectedSize > MAX_DIMENSION_COUNT) {
throw new RangeError(
Comment thread
svozza marked this conversation as resolved.
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
}
const dimensions = this.#dimensionsStore.getDimensions();
const defaultDimensions = this.#dimensionsStore.getDefaultDimensions();
if (
Object.hasOwn(dimensions, name) ||
Object.hasOwn(defaultDimensions, name)
Expand All @@ -280,9 +289,13 @@ class Metrics extends Utility implements MetricsInterface {
*/
public addDimensions(dimensions: Dimensions): this {
const newDimensions = this.#sanitizeDimensions(dimensions);
const currentCount = this.#dimensionsStore.getDimensionCount();
const newSetCount = Object.keys(newDimensions).length;
if (currentCount + newSetCount >= MAX_DIMENSION_COUNT) {
const defaultDimensions = this.#dimensionsStore.getDefaultDimensions();
// addDimensions creates a new independent dimensionSet array, so we only project against defaultDimensions.
const projectedSize = new Set([
...Object.keys(defaultDimensions),
...Object.keys(newDimensions),
]).size;
if (projectedSize > MAX_DIMENSION_COUNT) {
Comment thread
svozza marked this conversation as resolved.
throw new RangeError(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
Expand Down Expand Up @@ -798,13 +811,32 @@ class Metrics extends Utility implements MetricsInterface {
const newDimensions = this.#sanitizeDimensions(dimensions);
const currentDefaultDimensions =
this.#dimensionsStore.getDefaultDimensions();
const newKeysCount = Object.keys(newDimensions).filter(
(key) => !Object.hasOwn(currentDefaultDimensions, key)
).length;
if (
this.#dimensionsStore.getDimensionCount() + newKeysCount >=
MAX_DIMENSION_COUNT
) {
const currentDimensions = this.#dimensionsStore.getDimensions();
const dimensionSets = this.#dimensionsStore.getDimensionSets();

const combinedDefaultKeys = [
...Object.keys(currentDefaultDimensions),
...Object.keys(newDimensions),
];
const currentDimensionsKeys = Object.keys(currentDimensions);
// The main array is only emitted if currentDimensions has keys.
// When empty, maxProjectedSize safely defaults to just the combinedDefaultKeys length to guard against phantom arrays.
let maxProjectedSize =
currentDimensionsKeys.length > 0
? new Set([...combinedDefaultKeys, ...currentDimensionsKeys]).size
: new Set(combinedDefaultKeys).size;

for (const dimensionSet of dimensionSets) {
const setSize = new Set([
...combinedDefaultKeys,
...Object.keys(dimensionSet),
]).size;
if (setSize > maxProjectedSize) {
maxProjectedSize = setSize;
}
}

if (maxProjectedSize > MAX_DIMENSION_COUNT) {
Comment thread
svozza marked this conversation as resolved.
throw new RangeError(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
Expand Down
113 changes: 97 additions & 16 deletions packages/metrics/tests/unit/dimensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ describe('Working with dimensions', () => {
}

// Assess
expect(() => metrics.addDimension('extra', 'test')).toThrowError(
expect(() => metrics.addDimension('extra', 'test')).toThrow(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
});
Expand All @@ -351,12 +351,12 @@ 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
expect(() => metrics.setDefaultDimensions({ extra: 'test' })).toThrowError(
expect(() => metrics.setDefaultDimensions({ extra: 'test' })).toThrow(
'The number of metric dimensions must be lower than 29'
);
});
Expand All @@ -368,7 +368,7 @@ describe('Working with dimensions', () => {
});

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

Expand All @@ -380,14 +380,35 @@ describe('Working with dimensions', () => {
);
});

it('throws when setDefaultDimensions would exceed the limit with existing dimension sets', () => {
// Prepare
const metrics = new Metrics({
singleMetric: true,
});

// Act
const newDimensionSet: Record<string, string> = {};
for (let i = 0; i < 28; i++) {
newDimensionSet[`dimension-extra-${i}`] = 'test';
}
metrics.addDimensions(newDimensionSet);

// Assess
expect(() =>
metrics.setDefaultDimensions({ 'new-default': 'test' })
).toThrow(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
});

it('allows overriding existing default dimension keys without triggering the limit', () => {
// Prepare
const metrics = new Metrics({
singleMetric: true,
});

// Act
for (let i = 1; i < MAX_DIMENSION_COUNT - 1; i++) {
for (let i = 1; i < MAX_DIMENSION_COUNT; i++) {
metrics.setDefaultDimensions({ [`dimension-${i}`]: 'test' });
}

Expand All @@ -397,34 +418,79 @@ describe('Working with dimensions', () => {
).not.toThrow();
});

it('throws when adding dimension sets would exceed the limit', () => {
it('allows overriding existing regular dimensions via addDimension without triggering the limit', () => {
// Prepare
const metrics = new Metrics({
singleMetric: true,
defaultDimensions: {
environment: 'test',
},
});

// Act
// We start with 2 dimensions because the default dimension & service name are already added
for (let i = 2; i < MAX_DIMENSION_COUNT; i++) {
for (let i = 1; i < MAX_DIMENSION_COUNT; i++) {
metrics.addDimension(`dimension-${i}`, 'test');
}

// Assess
// Adding a dimension set with 3 dimensions would exceed the limit
expect(() => metrics.addDimension('dimension-1', 'updated')).not.toThrow();
});

it('allows addDimensions to override existing default dimension keys without triggering the limit', () => {
// Prepare
const metrics = new Metrics({
singleMetric: true,
});

// Act
for (let i = 1; i < MAX_DIMENSION_COUNT; i++) {
metrics.setDefaultDimensions({ [`dimension-${i}`]: 'test' });
}

// Assess
expect(() =>
metrics.addDimensions({
'dimension-extra-1': 'test',
'dimension-extra-2': 'test',
'dimension-extra-3': 'test',
'dimension-1': 'updated',
'dimension-2': 'updated',
})
).toThrowError(
).not.toThrow();
});

it('throws when adding dimension sets would exceed the limit', () => {
// Prepare
const metrics = new Metrics({
Comment thread
svozza marked this conversation as resolved.
singleMetric: true,
defaultDimensions: {
environment: 'test',
},
});

// Act
const newDimensionSet: Record<string, string> = {};
for (let i = 0; i < 28; i++) {
newDimensionSet[`dimension-extra-${i}`] = 'test';
}

// Assess
// Adding a dimension set that results in > 29 dimensions should exceed the limit
expect(() => metrics.addDimensions(newDimensionSet)).toThrow(
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
);
});

it('allows setDefaultDimensions to evaluate maxProjectedSize correctly when dimensionSets are smaller', () => {
// Prepare
const metrics = new Metrics({ singleMetric: true });

// Act
metrics.addDimension('dim1', 'val1');
metrics.addDimension('dim2', 'val2');
metrics.addDimensions({ setDim1: 'val3' });

// Assess
// This triggers setDefaultDimensions logic where setSize (3) < maxProjectedSize (4), covering the false branch.
expect(() =>
metrics.setDefaultDimensions({ newDefault: 'val4' })
).not.toThrow();
});

it('handles dimension overrides across multiple dimension sets', () => {
// Prepare
const metrics = new Metrics({
Expand Down Expand Up @@ -647,4 +713,19 @@ describe('Working with dimensions', () => {
expect.not.objectContaining({ [name]: value })
);
});
it('allows adding multiple dimension sets as long as no single set exceeds MAX_DIMENSION_COUNT', () => {
// Prepare
const metrics = new Metrics({ namespace: 'test' });

// Act
// Add 30 dimension sets, each with 1 dimension
for (let i = 0; i < 30; i++) {
metrics.addDimensions({ [`dim-${i}`]: 'value' });
}

metrics.addMetric('test', MetricUnit.Count, 1);

// Assess
expect(() => metrics.publishStoredMetrics()).not.toThrow();
});
});