Closed
Description
Bug description
If using the decorator on handler and any execution flow endup throwing an exception, the metrics won't be flushed.
Expected Behavior
Metrics should be flushed whatever is happening in the customer code.
Current Behavior
If exception thrown, no metrics sent to cloudwatch.
Possible Solution
Option 1: Flush in a finally statement here: https://github.com/awslabs/aws-lambda-powertools-typescript/blob/main/packages/metrics/src/Metrics.ts#L93
Option 2: Leverage official lib https://github.com/awslabs/aws-embedded-metrics-node that handle this properly
Steps to Reproduce
Add this test to unit tests:
test('Using decorator should log even if exception thrown', async () => {
const metrics = new Metrics({ namespace: 'test' });
class LambdaFunction implements LambdaInterface {
@metrics.logMetrics()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
public handler<TEvent, TResult>(
_event: TEvent,
_context: Context,
_callback: Callback<TResult>
): void | Promise<TResult> {
metrics.addMetric('test_name_1', MetricUnits.Count, 1);
throw new Error('Test Error');
}
}
try {
await new LambdaFunction().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
} catch (error) {
// DO NOTHING
}
expect(console.log).toBeCalledTimes(1);
});
Should pass after fix
Environment
- Powertools version used: v0.0.1-alpha.3
- Packaging format (Layers, npm): npm
- AWS Lambda function runtime: nodejs
- Debugging logs: N/A
Related issues, RFCs
N/A