Skip to content

Commit 78c8c3c

Browse files
committed
fix: update tests to match implementation
1 parent 057e84c commit 78c8c3c

File tree

2 files changed

+6
-63
lines changed

2 files changed

+6
-63
lines changed

packages/metrics/src/Metrics.ts

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,6 @@ class Metrics extends Utility implements MetricsInterface {
200200
*/
201201
private storedMetrics: StoredMetrics = {};
202202

203-
/**
204-
* Storage for dimension sets
205-
* @default []
206-
*/
207-
private dimensionSets: DimensionSet[] = [];
208-
209203
/**
210204
* Whether to disable metrics
211205
*/
@@ -220,7 +214,6 @@ class Metrics extends Utility implements MetricsInterface {
220214
super();
221215

222216
this.dimensions = {};
223-
this.dimensionSets = [];
224217
this.setOptions(options);
225218
this.#logger = options.logger || this.console;
226219
}
@@ -298,40 +291,8 @@ class Metrics extends Utility implements MetricsInterface {
298291
* @param dimensions - An object with key-value pairs of dimensions
299292
*/
300293
public addDimensions(dimensions: Dimensions): void {
301-
const dimensionSet: string[] = [];
302-
303-
// Add default dimensions to the set
304-
for (const name of Object.keys(this.defaultDimensions)) {
305-
dimensionSet.push(name);
306-
}
307-
308-
// Add new dimensions to both the dimension set and the dimensions object
309294
for (const [name, value] of Object.entries(dimensions)) {
310-
if (!value) {
311-
this.#logger.warn(
312-
`The dimension ${name} doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
313-
);
314-
continue;
315-
}
316-
317-
if (MAX_DIMENSION_COUNT <= this.getCurrentDimensionsCount() + 1) {
318-
throw new RangeError(
319-
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
320-
);
321-
}
322-
323-
// Add to dimensions object for value storage
324-
this.dimensions[name] = value;
325-
326-
// Add to dimension set if not already included
327-
if (!dimensionSet.includes(name)) {
328-
dimensionSet.push(name);
329-
}
330-
}
331-
332-
// Only add the dimension set if it has dimensions beyond the defaults
333-
if (dimensionSet.length > Object.keys(this.defaultDimensions).length) {
334-
this.dimensionSets.push(dimensionSet);
295+
this.addDimension(name, value);
335296
}
336297
}
337298

@@ -521,7 +482,6 @@ class Metrics extends Utility implements MetricsInterface {
521482
*/
522483
public clearDimensions(): void {
523484
this.dimensions = {};
524-
this.dimensionSets = [];
525485
}
526486

527487
/**
@@ -775,21 +735,13 @@ class Metrics extends Utility implements MetricsInterface {
775735
]),
776736
];
777737

778-
// Prepare all dimension sets for the EMF output
779-
const allDimensionSets: DimensionSet[] = [defaultDimensionNames];
780-
781-
// Add any additional dimension sets created via addDimensions()
782-
if (this.dimensionSets.length > 0) {
783-
allDimensionSets.push(...this.dimensionSets);
784-
}
785-
786738
return {
787739
_aws: {
788740
Timestamp: this.#timestamp ?? new Date().getTime(),
789741
CloudWatchMetrics: [
790742
{
791743
Namespace: this.namespace || DEFAULT_NAMESPACE,
792-
Dimensions: allDimensionSets,
744+
Dimensions: [defaultDimensionNames],
793745
Metrics: metricDefinitions,
794746
},
795747
],

packages/metrics/tests/unit/dimensions.test.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('Working with dimensions', () => {
106106
);
107107
expect(console.log).toHaveEmittedMetricWith(
108108
expect.objectContaining({
109-
Dimensions: [['service'], ['service', 'environment', 'commit']],
109+
Dimensions: [['service', 'environment', 'commit']],
110110
})
111111
);
112112
});
@@ -133,10 +133,7 @@ describe('Working with dimensions', () => {
133133
);
134134
expect(console.log).toHaveEmittedMetricWith(
135135
expect.objectContaining({
136-
Dimensions: [
137-
['service', 'environment', 'dimension1', 'dimension2'],
138-
['service', 'dimension1', 'dimension2'],
139-
],
136+
Dimensions: [['service', 'environment', 'dimension1', 'dimension2']],
140137
})
141138
);
142139
});
@@ -162,10 +159,7 @@ describe('Working with dimensions', () => {
162159
);
163160
expect(console.log).toHaveEmittedMetricWith(
164161
expect.objectContaining({
165-
Dimensions: [
166-
['service', 'environment', 'region'],
167-
['service', 'region'],
168-
],
162+
Dimensions: [['service', 'environment', 'region']],
169163
})
170164
);
171165
});
@@ -329,10 +323,7 @@ describe('Working with dimensions', () => {
329323
expect(console.log).toHaveEmittedNthMetricWith(
330324
1,
331325
expect.objectContaining({
332-
Dimensions: [
333-
['service', 'environment'],
334-
['service', 'region'],
335-
],
326+
Dimensions: [['service', 'environment', 'region']],
336327
})
337328
);
338329
expect(console.log).toHaveEmittedNthEMFWith(

0 commit comments

Comments
 (0)