From b209c30df92da07875f204f7f211294feea729db Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Wed, 26 Oct 2022 22:46:06 +0400 Subject: [PATCH] fix(metrics): metadata and dimensions not cleared on publish (#1129) * test(metrics): add tests to cover clearing metadata and dimensions after publishing stored metrics * fix(metrics): fix metadata and dimensions not cleared on publish --- packages/metrics/src/Metrics.ts | 4 +- packages/metrics/tests/unit/Metrics.test.ts | 56 +++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index 429e8a7d54..c1b78ac943 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -287,7 +287,9 @@ class Metrics extends Utility implements MetricsInterface { public publishStoredMetrics(): void { const target = this.serializeMetrics(); console.log(JSON.stringify(target)); - this.storedMetrics = {}; + this.clearMetrics(); + this.clearDimensions(); + this.clearMetadata(); } /** diff --git a/packages/metrics/tests/unit/Metrics.test.ts b/packages/metrics/tests/unit/Metrics.test.ts index 8ce2956969..0240904b88 100644 --- a/packages/metrics/tests/unit/Metrics.test.ts +++ b/packages/metrics/tests/unit/Metrics.test.ts @@ -80,6 +80,35 @@ describe('Class: Metrics', () => { expect(loggedData[additionalDimension.name]).toEqual(additionalDimension.value); }); + test('Publish Stored Metrics should clear added dimensions', async () => { + const metrics = new Metrics({ namespace: 'test' }); + const dimensionItem = { name: 'dimensionName', value: 'dimensionValue' }; + + class LambdaFunction implements LambdaInterface { + @metrics.logMetrics() + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + public handler( + _event: TEvent, + _context: Context, + _callback: Callback, + ): void | Promise { + metrics.addMetric('test_name_1', MetricUnits.Count, 1); + metrics.addDimension(dimensionItem.name, dimensionItem.value); + metrics.publishStoredMetrics(); + } + } + + await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!')); + const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ]; + + expect(console.log).toBeCalledTimes(2); + expect(loggedData[0][dimensionItem.name]).toEqual(dimensionItem.value); + expect(loggedData[0]._aws.CloudWatchMetrics[0].Dimensions[0].length).toEqual(1); + expect(loggedData[1][dimensionItem.name]).toBeUndefined(); + expect(loggedData[1]._aws.CloudWatchMetrics[0].Dimensions[0].length).toEqual(0); + }); + test('Adding more than max dimensions should throw error', () => { expect.assertions(1); const metrics = new Metrics(); @@ -144,6 +173,33 @@ describe('Class: Metrics', () => { expect(loggedData[metadataItem.name]).toEqual(metadataItem.value); expect(postClearLoggedData[metadataItem.name]).toBeUndefined(); }); + + test('Publish Stored Metrics should clear metadata', async () => { + const metrics = new Metrics({ namespace: 'test' }); + const metadataItem = { name: 'metaName', value: 'metaValue' }; + + class LambdaFunction implements LambdaInterface { + @metrics.logMetrics() + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + public handler( + _event: TEvent, + _context: Context, + _callback: Callback, + ): void | Promise { + metrics.addMetric('test_name_1', MetricUnits.Count, 1); + metrics.addMetadata(metadataItem.name, metadataItem.value); + metrics.publishStoredMetrics(); + } + } + + await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!')); + const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ]; + + expect(console.log).toBeCalledTimes(2); + expect(loggedData[0][metadataItem.name]).toEqual(metadataItem.value); + expect(loggedData[1][metadataItem.name]).toBeUndefined(); + }); }); describe('Feature: Default Dimensions', () => {