Skip to content

Bug: metrics not flushing on throw #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
flochaz opened this issue Nov 30, 2021 · 1 comment · Fixed by #249
Closed

Bug: metrics not flushing on throw #248

flochaz opened this issue Nov 30, 2021 · 1 comment · Fixed by #249
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped metrics This item relates to the Metrics Utility

Comments

@flochaz
Copy link
Contributor

flochaz commented Nov 30, 2021

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

@flochaz flochaz added the bug Something isn't working label Nov 30, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Dec 2, 2021

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@dreamorosi dreamorosi added this to the production-ready-release milestone Feb 28, 2022
@dreamorosi dreamorosi added the metrics This item relates to the Metrics Utility label Nov 13, 2022
@dreamorosi dreamorosi changed the title (metrics): Not flushing on throw Bug: metrics not flushing on throw Nov 14, 2022
@dreamorosi dreamorosi added the completed This item is complete and has been merged/shipped label Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped metrics This item relates to the Metrics Utility
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants