Skip to content

Commit 924d49d

Browse files
authored
fix(metrics): skip empty string dimension values (#3319)
1 parent 5350afe commit 924d49d

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

Diff for: packages/metrics/src/Metrics.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class Metrics extends Utility implements MetricsInterface {
218218
* Add a dimension to metrics.
219219
*
220220
* A dimension is a key-value pair that is used to group metrics, and it is included in all metrics emitted after it is added.
221+
* Invalid dimension values are skipped and a warning is logged.
221222
*
222223
* When calling the {@link Metrics.publishStoredMetrics | `publishStoredMetrics()`} method, the dimensions are cleared. This type of
223224
* dimension is useful when you want to add request-specific dimensions to your metrics. If you want to add dimensions that are
@@ -227,6 +228,12 @@ class Metrics extends Utility implements MetricsInterface {
227228
* @param value - The value of the dimension
228229
*/
229230
public addDimension(name: string, value: string): void {
231+
if (!value) {
232+
this.#logger.warn(
233+
`The dimension ${name} doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
234+
);
235+
return;
236+
}
230237
if (MAX_DIMENSION_COUNT <= this.getCurrentDimensionsCount()) {
231238
throw new RangeError(
232239
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
@@ -239,6 +246,7 @@ class Metrics extends Utility implements MetricsInterface {
239246
* Add multiple dimensions to the metrics.
240247
*
241248
* This method is useful when you want to add multiple dimensions to the metrics at once.
249+
* Invalid dimension values are skipped and a warning is logged.
242250
*
243251
* When calling the {@link Metrics.publishStoredMetrics | `publishStoredMetrics()`} method, the dimensions are cleared. This type of
244252
* dimension is useful when you want to add request-specific dimensions to your metrics. If you want to add dimensions that are
@@ -249,7 +257,14 @@ class Metrics extends Utility implements MetricsInterface {
249257
public addDimensions(dimensions: Dimensions): void {
250258
const newDimensions = { ...this.dimensions };
251259
for (const dimensionName of Object.keys(dimensions)) {
252-
newDimensions[dimensionName] = dimensions[dimensionName];
260+
const value = dimensions[dimensionName];
261+
if (value) {
262+
newDimensions[dimensionName] = value;
263+
} else {
264+
this.#logger.warn(
265+
`The dimension ${dimensionName} doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
266+
);
267+
}
253268
}
254269
if (Object.keys(newDimensions).length > MAX_DIMENSION_COUNT) {
255270
throw new RangeError(

Diff for: packages/metrics/tests/unit/Metrics.test.ts

+86
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,45 @@ describe('Class: Metrics', () => {
431431
`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
432432
);
433433
});
434+
435+
describe('invalid values should not be added as dimensions', () => {
436+
const testCases = [
437+
{ value: undefined as unknown as string, description: 'undefined' },
438+
{ value: null as unknown as string, description: 'null' },
439+
{ value: '', description: 'empty string' },
440+
];
441+
442+
for (const { value, description } of testCases) {
443+
it(`it should not add dimension with ${description} value and log a warning`, () => {
444+
// Prepare
445+
const customLogger = {
446+
warn: jest.fn(),
447+
debug: jest.fn(),
448+
error: jest.fn(),
449+
info: jest.fn(),
450+
};
451+
const metrics: Metrics = new Metrics({
452+
namespace: TEST_NAMESPACE,
453+
logger: customLogger,
454+
});
455+
const consoleWarnSpy = jest.spyOn(customLogger, 'warn');
456+
const testDimensionName = 'test-dimension';
457+
458+
// Act
459+
metrics.addDimension(testDimensionName, value);
460+
461+
// Assess
462+
expect(consoleWarnSpy).toHaveBeenCalledWith(
463+
`The dimension ${testDimensionName} doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
464+
);
465+
expect(metrics).toEqual(
466+
expect.objectContaining({
467+
dimensions: {},
468+
})
469+
);
470+
});
471+
}
472+
});
434473
});
435474

436475
describe('Method: addDimensions', () => {
@@ -520,6 +559,53 @@ describe('Class: Metrics', () => {
520559
`Unable to add 1 dimensions: the number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
521560
);
522561
});
562+
563+
describe('invalid values should not be added as dimensions', () => {
564+
const testCases = [
565+
{ value: undefined as unknown as string, description: 'undefined' },
566+
{ value: null as unknown as string, description: 'null' },
567+
{ value: '', description: 'empty string' },
568+
];
569+
570+
for (const { value, description } of testCases) {
571+
it(`it should not add dimension with ${description} value and log a warning`, () => {
572+
// Prepare
573+
const customLogger = {
574+
warn: jest.fn(),
575+
debug: jest.fn(),
576+
error: jest.fn(),
577+
info: jest.fn(),
578+
};
579+
const metrics: Metrics = new Metrics({
580+
namespace: TEST_NAMESPACE,
581+
logger: customLogger,
582+
});
583+
const consoleWarnSpy = jest.spyOn(customLogger, 'warn');
584+
const dimensionsToBeAdded: LooseObject = {
585+
'test-dimension-1': 'test-value-1',
586+
'test-dimension-2': 'test-value-2',
587+
};
588+
const testDimensionName = 'test-dimension';
589+
590+
// Act
591+
metrics.addDimensions(dimensionsToBeAdded);
592+
metrics.addDimensions({ [testDimensionName]: value });
593+
594+
// Assess
595+
expect(consoleWarnSpy).toHaveBeenCalledWith(
596+
`The dimension ${testDimensionName} doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
597+
);
598+
expect(metrics).toEqual(
599+
expect.objectContaining({
600+
dimensions: {
601+
'test-dimension-1': 'test-value-1',
602+
'test-dimension-2': 'test-value-2',
603+
},
604+
})
605+
);
606+
});
607+
}
608+
});
523609
});
524610

525611
describe('Method: addMetadata', () => {

0 commit comments

Comments
 (0)