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(commons): add cleanupPowertools function #1473

Merged
merged 3 commits into from
May 24, 2023
Merged

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented May 24, 2023

Description of your changes

This PR introduces a new function used to cleanup Powertools for AWS resources when a Middy middleware returns early and terminates the middleware chain.

When a middleware returns early, all the middleware lifecycle functions that come after it are not executed. This means that if a middleware was relying on certain logic to be run during the after or onError lifecycle functions, that logic will not be executed.

All Powertools for AWS middlewares rely on these lifecycle functions to perform some operations like:

  • close and restore the segments for Tracer
  • flush stored metrics for Metrics
  • clear any existing state in Logger

The function in this PR is aimed at advanced Middy users who author their own middlewares, as well as ourselves in future middleware implementations. The idea is that when writing a middleware that might return early you only have to import the cleanupPowertools function and run it before returning early. The function will then take care of inspecting the Middy internal object and execute any Powertools related function that might be present.

Related issues, RFCs

Issue number: #1410

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
  • My changes generate no new warnings
  • I have added tests that prove my change is effective and works
  • The PR title follows the conventional commit semantics

Breaking change checklist

Is it a breaking change?: NO

  • 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.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@boring-cyborg boring-cyborg bot added commons This item relates to the Commons Utility tests PRs that add or change tests labels May 24, 2023
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label May 24, 2023
@dreamorosi dreamorosi self-assigned this May 24, 2023
@github-actions github-actions bot added the feature PRs that introduce new features or minor changes label May 24, 2023
@dreamorosi dreamorosi linked an issue May 24, 2023 that may be closed by this pull request
@dreamorosi dreamorosi requested a review from am29d May 24, 2023 12:58
@dreamorosi dreamorosi marked this pull request as ready for review May 24, 2023 12:58
@dreamorosi
Copy link
Contributor Author

@am29d renamed the function as discussed, ready to review!

@dreamorosi dreamorosi merged commit 5bd0166 into main May 24, 2023
@dreamorosi dreamorosi deleted the dreamorosi/issue1410 branch May 24, 2023 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commons This item relates to the Commons Utility feature PRs that introduce new features or minor changes size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Docs: explain how to cleanup with middy early return
2 participants