Skip to content

Commit 2b23c86

Browse files
committed
improv(metrics): logMetricsAfterOrError middleware, examples update
1 parent 3d423e5 commit 2b23c86

File tree

3 files changed

+24
-57
lines changed

3 files changed

+24
-57
lines changed

packages/metrics/examples/decorator/empty-metrics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class Lambda implements LambdaInterface {
1717

1818
@metrics.logMetrics({ raiseOnEmptyMetrics: true })
1919
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
20-
20+
// Notice that no metrics are added
21+
// Since the raiseOnEmptyMetrics parameter is set to true, the Powertool throw an Error
2122
}
2223

2324
}

packages/metrics/src/middleware/middy.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ const logMetrics = (target: Metrics | Metrics[], options: ExtraOptions = {}): mi
2222

2323
};
2424

25-
const logMetricsAfter = async (): Promise<void> => {
25+
const logMetricsAfterOrError = async (): Promise<void> => {
2626
metricsInstances.forEach((metrics: Metrics) => {
2727
metrics.purgeStoredMetrics();
2828
});
2929
};
3030

3131
return {
3232
before: logMetricsBefore,
33-
after: logMetricsAfter
33+
after: logMetricsAfterOrError,
34+
onError: logMetricsAfterOrError
3435
};
3536
};
3637

packages/metrics/tests/unit/middleware/middy.test.ts

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ describe('Middy middleware', () => {
1717

1818
describe('logMetrics', () => {
1919

20+
const event = { foo: 'bar' };
21+
const getRandomInt = (): number => Math.floor(Math.random() * 1000000000);
22+
const awsRequestId = getRandomInt().toString();
23+
24+
const context = {
25+
callbackWaitsForEmptyEventLoop: true,
26+
functionVersion: '$LATEST',
27+
functionName: 'foo-bar-function',
28+
memoryLimitInMB: '128',
29+
logGroupName: '/aws/lambda/foo-bar-function',
30+
logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456',
31+
invokedFunctionArn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function',
32+
awsRequestId: awsRequestId,
33+
getRemainingTimeInMillis: () => 1234,
34+
done: () => console.log('Done!'),
35+
fail: () => console.log('Failed!'),
36+
succeed: () => console.log('Succeeded!'),
37+
};
38+
2039
test('when a metrics instance is passed WITH custom options, it prints the metrics in the stdout', async () => {
2140

2241
// Prepare
@@ -32,24 +51,6 @@ describe('Middy middleware', () => {
3251
captureColdStartMetric: true
3352
};
3453
const handler = middy(lambdaHandler).use(logMetrics(metrics, metricsOptions));
35-
const event = { foo: 'bar' };
36-
const getRandomInt = (): number => Math.floor(Math.random() * 1000000000);
37-
38-
const awsRequestId = getRandomInt().toString();
39-
const context = {
40-
callbackWaitsForEmptyEventLoop: true,
41-
functionVersion: '$LATEST',
42-
functionName: 'foo-bar-function',
43-
memoryLimitInMB: '128',
44-
logGroupName: '/aws/lambda/foo-bar-function',
45-
logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456',
46-
invokedFunctionArn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function',
47-
awsRequestId: awsRequestId,
48-
getRemainingTimeInMillis: () => 1234,
49-
done: () => console.log('Done!'),
50-
fail: () => console.log('Failed!'),
51-
succeed: () => console.log('Succeeded!'),
52-
};
5354

5455
// Act
5556
await handler(event, context, () => console.log('Lambda invoked!'));
@@ -102,24 +103,6 @@ describe('Middy middleware', () => {
102103
};
103104

104105
const handler = middy(lambdaHandler).use(logMetrics(metrics));
105-
const event = { foo: 'bar' };
106-
const getRandomInt = (): number => Math.floor(Math.random() * 1000000000);
107-
108-
const awsRequestId = getRandomInt().toString();
109-
const context = {
110-
callbackWaitsForEmptyEventLoop: true,
111-
functionVersion: '$LATEST',
112-
functionName: 'foo-bar-function',
113-
memoryLimitInMB: '128',
114-
logGroupName: '/aws/lambda/foo-bar-function',
115-
logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456',
116-
invokedFunctionArn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function',
117-
awsRequestId: awsRequestId,
118-
getRemainingTimeInMillis: () => 1234,
119-
done: () => console.log('Done!'),
120-
fail: () => console.log('Failed!'),
121-
succeed: () => console.log('Succeeded!'),
122-
};
123106

124107
// Act
125108
await handler(event, context, () => console.log('Lambda invoked!'));
@@ -155,24 +138,6 @@ describe('Middy middleware', () => {
155138
raiseOnEmptyMetrics: true
156139
};
157140
const handler = middy(lambdaHandler).use(logMetrics([metrics], metricsOptions));
158-
const event = { foo: 'bar' };
159-
const getRandomInt = (): number => Math.floor(Math.random() * 1000000000);
160-
161-
const awsRequestId = getRandomInt().toString();
162-
const context = {
163-
callbackWaitsForEmptyEventLoop: true,
164-
functionVersion: '$LATEST',
165-
functionName: 'foo-bar-function',
166-
memoryLimitInMB: '128',
167-
logGroupName: '/aws/lambda/foo-bar-function',
168-
logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456',
169-
invokedFunctionArn: 'arn:aws:lambda:eu-central-1:123456789012:function:foo-bar-function',
170-
awsRequestId: awsRequestId,
171-
getRemainingTimeInMillis: () => 1234,
172-
done: () => console.log('Done!'),
173-
fail: () => console.log('Failed!'),
174-
succeed: () => console.log('Succeeded!'),
175-
};
176141

177142
// Act
178143
await handler(event, context, () => console.log('Lambda invoked!'));

0 commit comments

Comments
 (0)