Skip to content
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

Bug: clearState not working when lambda throws error #1027

Closed
Crafoord opened this issue Jul 27, 2022 · 4 comments · Fixed by #1045
Closed

Bug: clearState not working when lambda throws error #1027

Crafoord opened this issue Jul 27, 2022 · 4 comments · Fixed by #1045
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped logger This item relates to the Logger Utility

Comments

@Crafoord
Copy link

Crafoord commented Jul 27, 2022

Bug description

When using addPersistentLogAttributes :

logger.addPersistentLogAttributes({ memberId: 'test'  });

and using the middy to clearState:

export const handler = middy(lambdaHandler).use(
  injectLambdaContext(logger, { clearState: true })
);

and then exiting the lambda by throwing an error, the next invocation will still have the persistentLogAttributes present. My guess is that the middleware never runs because of the error.

Expected Behavior

When lambda crashes the middleware should still clearState.

Current Behavior

When lambda crashes the middleware does not clearState.

Possible Solution

I guess there could be a way of handling this by not using middleware but there is no documentation of how to clearState without it.

Steps to Reproduce

  1. create handler like:
import { Handler } from 'aws-lambda';
import middy from '@middy/core';
import { injectLambdaContext, Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger();

const lambdaHandler: Handler = async (event) => {
  logger.info('Before adding memberId');
  logger.addPersistentLogAttributes({ memberId: 'test'  });
  logger.info('After adding memberId');

  throw new Error();
};

export const handler = middy(lambdaHandler).use(
  injectLambdaContext(logger, { clearState: true })
);
  1. invoce lambda
  2. check logs, Before adding memberId will not have memberId property, After adding memberId will have memberId: test
  3. invoce lambda again
  4. check logs Before adding memberId will have memberId: test (which is not supposed to be there).

Environment

"@aws-lambda-powertools/logger": "^1.0.2",
"@middy/core": "^3.1.0"

Lambda runtime: nodejs16.x

@Crafoord Crafoord added bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels Jul 27, 2022
@dreamorosi
Copy link
Contributor

Hey @Crafoord thank you for reporting this and providing a reproduction example.

Please give us some time to look into it, we'll get back to you as soon as we can.

@dreamorosi dreamorosi added the logger This item relates to the Logger Utility label Jul 27, 2022
@saragerion saragerion self-assigned this Jul 27, 2022
@saragerion
Copy link
Contributor

Thanks again for reporting this bug @Crafoord.
I was able to reproduce this issue as well.

Source code:
https://github.com/saragerion/serverless-typescript-demo/blob/1027-bug-logger-clearstate-not-working-when-lambda-throws-error/src/api/get-products.ts#L20

CloudWatch Logs Insights query:

fields message, memberId, function_request_id, @timestamp
| filter service = 'serverless-typescript-demo'
| filter function_name LIKE /GetProducts/
| sort @timestamp desc
| limit 20

Logs output:
Screenshot 2022-07-28 at 11 01 24

We will start working on a fix right away and adding this scenario to our E2E tests.

@saragerion saragerion removed the triage This item has not been triaged by a maintainer, please wait label Jul 28, 2022
@saragerion saragerion linked a pull request Aug 10, 2022 that will close this issue
11 tasks
@github-actions
Copy link
Contributor

⚠️ 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.

@saragerion
Copy link
Contributor

This will be part of the next release. I am going to add the scenario in our E2E tests in a future PR (it requires some refactoring).

@dreamorosi dreamorosi changed the title Bug (logger): clearState not working when lambda throws error Bug: clearState not working when lambda throws error 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 logger This item relates to the Logger Utility
Projects
None yet
3 participants