From 761ee46c46d824980f7980739f21cc58724baa50 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Fri, 21 Oct 2022 19:01:39 +0400 Subject: [PATCH 1/2] test(metrics): add tests to cover clearing metadata and dimensions after publishing stored metrics --- packages/metrics/tests/unit/Metrics.test.ts | 56 +++++++++++++++++++++ 1 file changed, 56 insertions(+) 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', () => { From 4600112ae386b7b288e96efdc82e79886a049e50 Mon Sep 17 00:00:00 2001 From: Sergei Cherniaev Date: Fri, 21 Oct 2022 19:07:52 +0400 Subject: [PATCH 2/2] fix(metrics): fix metadata and dimensions not cleared on publish --- packages/metrics/src/Metrics.ts | 4 +++- 1 file changed, 3 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(); } /**