diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index 7d96fa470a..94d615c108 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -609,15 +609,16 @@ class Metrics extends Utility implements MetricsInterface { ); } - /* v8 ignore else -- @preserve */ - if (!this.disabled) { - const emfOutput = this.serializeMetrics(); - hasMetrics && this.console.log(JSON.stringify(emfOutput)); + try { + if (!this.disabled) { + const emfOutput = this.serializeMetrics(); + hasMetrics && this.console.log(JSON.stringify(emfOutput)); + } + } finally { + this.clearMetrics(); + this.clearDimensions(); + this.clearMetadata(); } - - this.clearMetrics(); - this.clearDimensions(); - this.clearMetadata(); return this; } diff --git a/packages/metrics/tests/unit/creatingMetrics.test.ts b/packages/metrics/tests/unit/creatingMetrics.test.ts index 82123a3a48..4e61fe0233 100644 --- a/packages/metrics/tests/unit/creatingMetrics.test.ts +++ b/packages/metrics/tests/unit/creatingMetrics.test.ts @@ -209,6 +209,59 @@ describe('Creating metrics', () => { ); }); + it('clears dimensions and metadata even when throwOnEmptyMetrics throws', () => { + // Prepare + const metrics = new Metrics({ + singleMetric: false, + namespace: DEFAULT_NAMESPACE, + }); + metrics.setThrowOnEmptyMetrics(true); + metrics.addDimension('request_id', 'abc-123'); + metrics.addMetadata('trace_id', 'xyz-789'); + + // Act + expect(() => metrics.publishStoredMetrics()).toThrowError( + 'The number of metrics recorded must be higher than zero' + ); + metrics.setThrowOnEmptyMetrics(false); + metrics.addMetric('test', MetricUnit.Count, 1); + metrics.publishStoredMetrics(); + + // Assess + expect(console.log).toHaveEmittedNthEMFWith( + 1, + expect.not.objectContaining({ + request_id: 'abc-123', + }) + ); + expect(console.log).toHaveEmittedNthEMFWith( + 1, + expect.not.objectContaining({ + trace_id: 'xyz-789', + }) + ); + }); + + it('does not emit metrics when disabled but still clears state', () => { + // Prepare + process.env.POWERTOOLS_DEV = undefined; + process.env.POWERTOOLS_METRICS_DISABLED = 'true'; + const metrics = new Metrics({ + singleMetric: false, + namespace: DEFAULT_NAMESPACE, + }); + metrics.addMetric('test', MetricUnit.Count, 1); + metrics.addDimension('env', 'prod'); + metrics.addMetadata('trace', '123'); + + // Act + metrics.publishStoredMetrics(); + + // Assess + expect(console.log).not.toHaveBeenCalled(); + expect(metrics.hasStoredMetrics()).toBe(false); + }); + it('flushes the buffer automatically when the buffer is full', () => { // Prepare const metrics = new Metrics({