Skip to content

Commit 8d40471

Browse files
authored
feat(metric): bring feature parity between decorator and utility function (#291)
1 parent 2561ede commit 8d40471

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

packages/metrics/src/Metrics.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Metrics implements MetricsInterface {
2525
private isSingleMetric: boolean = false;
2626
private metadata: { [key: string]: string } = {};
2727
private namespace?: string;
28-
private raiseOnEmptyMetrics: boolean = false;
28+
private shouldRaiseOnEmptyMetrics: boolean = false;
2929
private storedMetrics: StoredMetrics = {};
3030

3131
public constructor(options: MetricsOptions = {}) {
@@ -80,9 +80,19 @@ class Metrics implements MetricsInterface {
8080
this.storedMetrics = {};
8181
}
8282

83+
public captureColdStartMetric(): void {
84+
this.captureColdStart();
85+
}
86+
87+
public raiseOnEmptyMetrics(): void {
88+
this.shouldRaiseOnEmptyMetrics = true;
89+
}
90+
8391
public logMetrics(options: DecoratorOptions = {}): HandlerMethodDecorator {
8492
const { raiseOnEmptyMetrics, defaultDimensions, captureColdStartMetric } = options;
85-
this.raiseOnEmptyMetrics = raiseOnEmptyMetrics || false;
93+
if (raiseOnEmptyMetrics) {
94+
this.raiseOnEmptyMetrics();
95+
}
8696
if (defaultDimensions !== undefined) {
8797
this.setDefaultDimensions(defaultDimensions);
8898
}
@@ -114,7 +124,7 @@ class Metrics implements MetricsInterface {
114124
Name: metricDefinition.name,
115125
Unit: metricDefinition.unit,
116126
}));
117-
if (metricDefinitions.length === 0 && this.raiseOnEmptyMetrics) {
127+
if (metricDefinitions.length === 0 && this.shouldRaiseOnEmptyMetrics) {
118128
throw new RangeError('The number of metrics recorded must be higher than zero');
119129
}
120130

packages/metrics/tests/unit/Metrics.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,25 @@ describe('Class: Metrics', () => {
225225
});
226226

227227
describe('Feature: Cold Start', () => {
228+
test('Cold start metric should only be written out once and flushed automatically', async () => {
229+
const metrics = new Metrics({ namespace: 'test' });
230+
231+
const handler = async (event: any, context: Context) => {
232+
// Should generate only one log
233+
metrics.captureColdStartMetric();
234+
};
235+
236+
await handler(dummyEvent, dummyContext);
237+
await handler(dummyEvent, dummyContext);
238+
const loggedData = [JSON.parse(consoleSpy.mock.calls[0][0])];
239+
240+
expect(console.log).toBeCalledTimes(1);
241+
expect(loggedData[0]._aws.CloudWatchMetrics[0].Metrics.length).toBe(1);
242+
expect(loggedData[0]._aws.CloudWatchMetrics[0].Metrics[0].Name).toBe('ColdStart');
243+
expect(loggedData[0]._aws.CloudWatchMetrics[0].Metrics[0].Unit).toBe('Count');
244+
expect(loggedData[0].ColdStart).toBe(1);
245+
});
246+
228247
test('Cold start metric should only be written out once', async () => {
229248
const metrics = new Metrics({ namespace: 'test' });
230249

@@ -338,6 +357,23 @@ describe('Class: Metrics', () => {
338357
expect((<Error>e).message).toBe('The number of metrics recorded must be higher than zero');
339358
}
340359
});
360+
361+
test('Error should be thrown on empty metrics when raiseOnEmptyMetrics() is callse', async () => {
362+
expect.assertions(1);
363+
364+
const metrics = new Metrics({ namespace: 'test' });
365+
const handler = async (event: any, context: Context) => {
366+
metrics.raiseOnEmptyMetrics();
367+
// Logic goes here
368+
metrics.purgeStoredMetrics();
369+
};
370+
371+
try {
372+
await handler(dummyEvent, dummyContext);
373+
} catch (e) {
374+
expect((<Error>e).message).toBe('The number of metrics recorded must be higher than zero');
375+
}
376+
});
341377
});
342378

343379
describe('Feature: Auto log at limit', () => {

0 commit comments

Comments
 (0)