Skip to content

Commit c9d6aa0

Browse files
authored
fix(metrics): revert changes when raise warning with overridden default dimensions (#4226)
1 parent a933a6a commit c9d6aa0

File tree

4 files changed

+8
-144
lines changed

4 files changed

+8
-144
lines changed

packages/metrics/src/Metrics.ts

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Console } from 'node:console';
22
import {
33
isIntegerNumber,
4-
isNullOrUndefined,
54
isNumber,
6-
isRecord,
75
isString,
86
isStringUndefinedNullEmpty,
97
Utility,
@@ -242,10 +240,8 @@ class Metrics extends Utility implements MetricsInterface {
242240
super();
243241

244242
this.dimensions = {};
245-
this.setEnvConfig();
246-
this.setConsole();
247-
this.#logger = options.logger || this.console;
248243
this.setOptions(options);
244+
this.#logger = options.logger || this.console;
249245
}
250246

251247
/**
@@ -828,41 +824,13 @@ class Metrics extends Utility implements MetricsInterface {
828824
* @param dimensions - The dimensions to be added to the default dimensions object
829825
*/
830826
public setDefaultDimensions(dimensions: Dimensions | undefined): void {
831-
if (isNullOrUndefined(dimensions) || !isRecord(dimensions)) {
832-
return;
833-
}
834-
835-
const cleanedDimensions: Dimensions = {};
836-
837-
for (const [key, value] of Object.entries(dimensions)) {
838-
if (
839-
isStringUndefinedNullEmpty(key) ||
840-
isStringUndefinedNullEmpty(value)
841-
) {
842-
this.#logger.warn(
843-
`The dimension ${key} doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
844-
);
845-
continue;
846-
}
847-
848-
if (Object.hasOwn(this.defaultDimensions, key)) {
849-
this.#logger.warn(
850-
`Dimension "${key}" has already been added. The previous value will be overwritten.`
851-
);
852-
}
853-
854-
cleanedDimensions[key] = value;
855-
}
856-
857827
const targetDimensions = {
858828
...this.defaultDimensions,
859-
...cleanedDimensions,
829+
...dimensions,
860830
};
861-
862-
if (Object.keys(targetDimensions).length >= MAX_DIMENSION_COUNT) {
831+
if (MAX_DIMENSION_COUNT <= Object.keys(targetDimensions).length) {
863832
throw new Error('Max dimension count hit');
864833
}
865-
866834
this.defaultDimensions = targetDimensions;
867835
}
868836

@@ -1090,6 +1058,8 @@ class Metrics extends Utility implements MetricsInterface {
10901058
functionName,
10911059
} = options;
10921060

1061+
this.setEnvConfig();
1062+
this.setConsole();
10931063
this.setCustomConfigService(customConfigService);
10941064
this.setDisabled();
10951065
this.setNamespace(namespace);

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ describe('Setting custom timestamp', () => {
4848

4949
it('logs a warning when the provided timestamp is too far in the past', () => {
5050
// Prepare
51-
const metrics = new Metrics({
52-
singleMetric: true,
53-
});
51+
const metrics = new Metrics({ singleMetric: true });
5452

5553
// Act
5654
metrics.setTimestamp(Date.now() - EMF_MAX_TIMESTAMP_PAST_AGE - 1000);
@@ -65,9 +63,7 @@ describe('Setting custom timestamp', () => {
6563

6664
it('logs a warning when the provided timestamp is too far in the future', () => {
6765
// Prepare
68-
const metrics = new Metrics({
69-
singleMetric: true,
70-
});
66+
const metrics = new Metrics({ singleMetric: true });
7167

7268
// Act
7369
metrics.setTimestamp(Date.now() + EMF_MAX_TIMESTAMP_FUTURE_AGE + 1000);

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

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -552,104 +552,4 @@ describe('Working with dimensions', () => {
552552
})
553553
);
554554
});
555-
556-
it('warns when setDefaultDimensions overwrites existing dimensions', () => {
557-
// Prepare
558-
const metrics = new Metrics({
559-
namespace: DEFAULT_NAMESPACE,
560-
defaultDimensions: { environment: 'prod' },
561-
});
562-
563-
// Act
564-
metrics.setDefaultDimensions({ region: 'us-east-1' });
565-
metrics.setDefaultDimensions({
566-
environment: 'staging', // overwrites default dimension
567-
});
568-
569-
// Assess
570-
expect(console.warn).toHaveBeenCalledOnce();
571-
expect(console.warn).toHaveBeenCalledWith(
572-
'Dimension "environment" has already been added. The previous value will be overwritten.'
573-
);
574-
});
575-
576-
it('returns immediately if dimensions is undefined', () => {
577-
// Prepare
578-
const metrics = new Metrics({
579-
singleMetric: true,
580-
namespace: DEFAULT_NAMESPACE,
581-
});
582-
583-
// Act
584-
metrics.addMetric('myMetric', MetricUnit.Count, 1);
585-
586-
// Assert
587-
expect(console.warn).not.toHaveBeenCalled();
588-
589-
expect(console.log).toHaveEmittedEMFWith(
590-
expect.objectContaining({
591-
service: 'hello-world',
592-
})
593-
);
594-
});
595-
596-
it.each([
597-
{ value: undefined, name: 'valid-name' },
598-
{ value: null, name: 'valid-name' },
599-
{ value: '', name: 'valid-name' },
600-
{ value: 'valid-value', name: '' },
601-
])(
602-
'skips invalid default dimension values in setDefaultDimensions ($name)',
603-
({ value, name }) => {
604-
// Arrange
605-
const metrics = new Metrics({
606-
singleMetric: true,
607-
namespace: DEFAULT_NAMESPACE,
608-
});
609-
610-
// Act
611-
metrics.setDefaultDimensions({
612-
validDimension: 'valid',
613-
[name as string]: value as string,
614-
});
615-
616-
metrics.addMetric('test', MetricUnit.Count, 1);
617-
metrics.publishStoredMetrics();
618-
619-
// Assert
620-
expect(console.warn).toHaveBeenCalledWith(
621-
`The dimension ${name} doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
622-
);
623-
624-
expect(console.log).toHaveEmittedEMFWith(
625-
expect.objectContaining({ validDimension: 'valid' })
626-
);
627-
628-
expect(console.log).toHaveEmittedEMFWith(
629-
expect.not.objectContaining({ [name]: value })
630-
);
631-
}
632-
);
633-
it('returns immediately without logging if dimensions is not a plain object', () => {
634-
// Prepare
635-
const metrics = new Metrics({
636-
singleMetric: true,
637-
namespace: DEFAULT_NAMESPACE,
638-
});
639-
640-
// Act
641-
// @ts-expect-error – simulate runtime misuse
642-
metrics.setDefaultDimensions('not-an-object');
643-
644-
// Assert
645-
expect(console.warn).not.toHaveBeenCalled();
646-
647-
metrics.addMetric('someMetric', MetricUnit.Count, 1);
648-
649-
expect(console.log).toHaveEmittedEMFWith(
650-
expect.objectContaining({
651-
service: 'hello-world',
652-
})
653-
);
654-
});
655555
});

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ describe('Initialize Metrics', () => {
6565

6666
it('uses the default namespace when none is provided', () => {
6767
// Prepare
68-
const metrics = new Metrics({
69-
singleMetric: true,
70-
});
68+
const metrics = new Metrics({ singleMetric: true });
7169

7270
// Act
7371
metrics.addMetric('test', MetricUnit.Count, 1);

0 commit comments

Comments
 (0)