diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index 1fa15a4038..cf251bd7ed 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -1167,6 +1167,12 @@ class Metrics extends Utility implements MetricsInterface { ); if (!isNumber(value)) throw new RangeError(`${value} is not a valid number`); + if (!Number.isFinite(value)) { + this.#logger.warn( + `The metric ${name} has a non-finite value (${value}) and won't be added. CloudWatch EMF only accepts finite numbers.` + ); + return; + } if (!Object.values(MetricUnits).includes(unit)) throw new RangeError( `Invalid metric unit '${unit}', expected either option: ${Object.values( diff --git a/packages/metrics/tests/unit/creatingMetrics.test.ts b/packages/metrics/tests/unit/creatingMetrics.test.ts index 4e61fe0233..905c4d2ff1 100644 --- a/packages/metrics/tests/unit/creatingMetrics.test.ts +++ b/packages/metrics/tests/unit/creatingMetrics.test.ts @@ -373,6 +373,74 @@ describe('Creating metrics', () => { ).toThrowError(new RangeError('one is not a valid number')); }); + it.each([ + { value: Number.NaN }, + { value: Number.POSITIVE_INFINITY }, + { value: Number.NEGATIVE_INFINITY }, + ])( + 'skips a metric with a non-finite value ($value) and logs a warning', + ({ value }) => { + // Prepare + const metrics = new Metrics({ + singleMetric: false, + namespace: DEFAULT_NAMESPACE, + }); + + // Act + metrics + .addMetric('ok', MetricUnit.Count, 1) + .addMetric('ratio', MetricUnit.Percent, value) + .publishStoredMetrics(); + + // Assess + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith( + `The metric ratio has a non-finite value (${value}) and won't be added. CloudWatch EMF only accepts finite numbers.` + ); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toHaveEmittedNthEMFWith(1, { + _aws: { + Timestamp: expect.any(Number), + CloudWatchMetrics: [ + { + Namespace: DEFAULT_NAMESPACE, + Dimensions: [['service']], + Metrics: [{ Name: 'ok', Unit: 'Count' }], + }, + ], + }, + service: 'hello-world', + ok: 1, + }); + } + ); + + it('skips a non-finite value added to a metric that already has values', () => { + // Prepare + const metrics = new Metrics({ + singleMetric: false, + namespace: DEFAULT_NAMESPACE, + }); + + // Act + metrics + .addMetric('ok', MetricUnit.Count, 1) + .addMetric('ok', MetricUnit.Count, Number.NaN) + .addMetric('ok', MetricUnit.Count, 2) + .publishStoredMetrics(); + + // Assess + expect(console.warn).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith( + "The metric ok has a non-finite value (NaN) and won't be added. CloudWatch EMF only accepts finite numbers." + ); + expect(console.log).toHaveBeenCalledTimes(1); + expect(console.log).toHaveEmittedNthEMFWith( + 1, + expect.objectContaining({ ok: [1, 2] }) + ); + }); + it('throws when an invalid unit is passed', () => { // Prepare const metrics = new Metrics();