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

feat(logger): add clear state functionality #902

Merged
merged 12 commits into from
Jun 22, 2022
Merged

Conversation

saragerion
Copy link
Contributor

@saragerion saragerion commented May 25, 2022

Description of your changes

The Logger utility is commonly initialized in the global scope, outside the handler function.
When you attach persistent log attributes through the persistentLogAttributes constructor option or via the appendKeys, addPersistentLogAttributes methods, this data is attached to the Logger instance.

Due to the Lambda Execution Context reuse, this means those persistent log attributes may be reused across invocations.

This PR introduces a new flag that developers can enable to "clear the state" across invocations.
If developers want to make sure that persistent attributes added inside the handler function code are not persisted across invocations, they can now set the parameter clearState as true in the injectLambdaContext middleware or decorator.

How to verify this change

See Contributing guide for the docs build.

carbon (3)

Related issues, RFCs

#482

PR status

Is this ready for review?: YES
Is it a breaking change?: NO

Checklist

  • My changes meet the tenets criteria
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in areas that should be flagged with a TODO, or hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding changes to the examples
  • My changes generate no new warnings
  • The code coverage hasn't decreased
  • I have added tests that prove my change is effective and works
  • New and existing unit tests pass locally and in Github Actions
  • Any dependent changes have been merged and published in downstream module
  • The PR title follows the conventional commit semantics

Breaking change checklist

  • I have documented the migration process
  • I have added, implemented necessary warnings (if it can live side by side)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@saragerion saragerion changed the title feat(logger): add business logic for clear state feat(logger): add clear state functionality May 26, 2022
@saragerion saragerion added this to the production-ready-release milestone May 26, 2022
@saragerion saragerion marked this pull request as ready for review May 26, 2022 21:33
dreamorosi
dreamorosi previously approved these changes May 27, 2022
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@flochaz
Copy link
Contributor

flochaz commented May 30, 2022

Could we add an e2e test ?

@saragerion
Copy link
Contributor Author

saragerion commented May 30, 2022

Hi @flochaz, thanks for your suggestion.
The E2E test work is captured here: #924

To test this functionality, I will need to send different payloads to sequential invocations.

Option 1 - I'd need to tweak the way we pass the payload to SEQUENTIAL invocations so that we can have custom payloads in different invocations.
https://github.com/awslabs/aws-lambda-powertools-typescript/blob/main/packages/commons/tests/utils/e2eUtils.ts#L71
I'd like to talk with you and @ijemmy about this util, but I don't want to slow you down on your other issues you are working on which have higher priority.

Option 2 - Easy workaround: call this function twice.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 21, 2022

📊 Package size report   0.9%↑

File Before After
aws-lambda-powertools-logger-0.10.0.tgz 22.3 kB 3%↑22.9 kB
logger-bundle.zip 22.8 kB 3%↑23.4 kB
Total (Includes all files) 137.3 kB 0.9%↑138.6 kB
Tarball size 136.1 kB 0.9%↑137.4 kB
Unchanged files
File Size
aws-lambda-powertools-commons-0.10.0.tgz 6.5 kB
aws-lambda-powertools-metrics-0.10.0.tgz 17.5 kB
aws-lambda-powertools-tracer-0.10.0.tgz 21.4 kB
commons-bundle.zip 7.0 kB
metrics-bundle.zip 18.0 kB
tracer-bundle.zip 21.9 kB

🤖 This report was automatically generated by pkg-size-action
(options hash: e722ab0ac3c866b548292c9e04d301e7)

@@ -63,12 +63,15 @@ export const generateUniqueName = (name_prefix: string, uuid: string, runtime: s
export const invokeFunction = async (functionName: string, times: number = 1, invocationMode: 'PARALLEL' | 'SEQUENTIAL' = 'PARALLEL', payload: FunctionPayload = {}): Promise<InvocationLogs[]> => {
const invocationLogs: InvocationLogs[] = [];

const promiseFactory = (): Promise<void> => {
const promiseFactory = (index?: number): Promise<void> => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have multiple lambda invocations that are SEQUENTIAL in your e2e scenario, you can now know which invocation is it in the Lambda event payload

@github-actions
Copy link
Contributor

github-actions bot commented Jun 21, 2022

📊 Package size report   0.9%↑

File Before After
aws-lambda-powertools-logger-0.10.0.tgz 22.3 kB 3%↑22.9 kB
logger-bundle.zip 22.8 kB 3%↑23.4 kB
Total (Includes all files) 137.3 kB 0.9%↑138.6 kB
Tarball size 136.1 kB 1%↑137.4 kB
Unchanged files
File Size
aws-lambda-powertools-commons-0.10.0.tgz 6.5 kB
aws-lambda-powertools-metrics-0.10.0.tgz 17.5 kB
aws-lambda-powertools-tracer-0.10.0.tgz 21.4 kB
commons-bundle.zip 7.0 kB
metrics-bundle.zip 18.0 kB
tracer-bundle.zip 21.9 kB

🤖 This report was automatically generated by pkg-size-action
(options hash: 12b45646e4229a77e5b6704a1516acd4)

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dreamorosi dreamorosi merged commit fa1dacb into main Jun 22, 2022
@dreamorosi dreamorosi deleted the feat/clear-state branch June 22, 2022 07:27
dreamorosi pushed a commit that referenced this pull request Aug 2, 2022
* feat: add business logic for clear state

* chore: removed temporary code

* feat: business logic + unit tests for decorator and middleware

* chore: format TS

* chore: typo in logger docs

* chore: lint fix quotes

* chore: typo in logger docs

* tests(logger): rename test scenario for clear state

* chore: exclude non-null rule in decorator

* build(deps): revert package-lock.json

* test(logger): clear state e2e tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants