Skip to content

Commit b209c30

Browse files
authored
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
1 parent c25279c commit b209c30

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ class Metrics extends Utility implements MetricsInterface {
287287
public publishStoredMetrics(): void {
288288
const target = this.serializeMetrics();
289289
console.log(JSON.stringify(target));
290-
this.storedMetrics = {};
290+
this.clearMetrics();
291+
this.clearDimensions();
292+
this.clearMetadata();
291293
}
292294

293295
/**

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

+56
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,35 @@ describe('Class: Metrics', () => {
8080
expect(loggedData[additionalDimension.name]).toEqual(additionalDimension.value);
8181
});
8282

83+
test('Publish Stored Metrics should clear added dimensions', async () => {
84+
const metrics = new Metrics({ namespace: 'test' });
85+
const dimensionItem = { name: 'dimensionName', value: 'dimensionValue' };
86+
87+
class LambdaFunction implements LambdaInterface {
88+
@metrics.logMetrics()
89+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
90+
// @ts-ignore
91+
public handler<TEvent, TResult>(
92+
_event: TEvent,
93+
_context: Context,
94+
_callback: Callback<TResult>,
95+
): void | Promise<TResult> {
96+
metrics.addMetric('test_name_1', MetricUnits.Count, 1);
97+
metrics.addDimension(dimensionItem.name, dimensionItem.value);
98+
metrics.publishStoredMetrics();
99+
}
100+
}
101+
102+
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
103+
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];
104+
105+
expect(console.log).toBeCalledTimes(2);
106+
expect(loggedData[0][dimensionItem.name]).toEqual(dimensionItem.value);
107+
expect(loggedData[0]._aws.CloudWatchMetrics[0].Dimensions[0].length).toEqual(1);
108+
expect(loggedData[1][dimensionItem.name]).toBeUndefined();
109+
expect(loggedData[1]._aws.CloudWatchMetrics[0].Dimensions[0].length).toEqual(0);
110+
});
111+
83112
test('Adding more than max dimensions should throw error', () => {
84113
expect.assertions(1);
85114
const metrics = new Metrics();
@@ -144,6 +173,33 @@ describe('Class: Metrics', () => {
144173
expect(loggedData[metadataItem.name]).toEqual(metadataItem.value);
145174
expect(postClearLoggedData[metadataItem.name]).toBeUndefined();
146175
});
176+
177+
test('Publish Stored Metrics should clear metadata', async () => {
178+
const metrics = new Metrics({ namespace: 'test' });
179+
const metadataItem = { name: 'metaName', value: 'metaValue' };
180+
181+
class LambdaFunction implements LambdaInterface {
182+
@metrics.logMetrics()
183+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
184+
// @ts-ignore
185+
public handler<TEvent, TResult>(
186+
_event: TEvent,
187+
_context: Context,
188+
_callback: Callback<TResult>,
189+
): void | Promise<TResult> {
190+
metrics.addMetric('test_name_1', MetricUnits.Count, 1);
191+
metrics.addMetadata(metadataItem.name, metadataItem.value);
192+
metrics.publishStoredMetrics();
193+
}
194+
}
195+
196+
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
197+
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];
198+
199+
expect(console.log).toBeCalledTimes(2);
200+
expect(loggedData[0][metadataItem.name]).toEqual(metadataItem.value);
201+
expect(loggedData[1][metadataItem.name]).toBeUndefined();
202+
});
147203
});
148204

149205
describe('Feature: Default Dimensions', () => {

0 commit comments

Comments
 (0)