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

fix: avoid logging the same resolved layer multiple times #33

Merged
merged 2 commits into from
Nov 8, 2022

Conversation

rfoel
Copy link
Contributor

@rfoel rfoel commented Aug 5, 2022

In my use case, I have about 30 lambdas and each one of them have the same 2 layers, which makes the terminal look like this on every deploy:

image

This small fix caches the resolved layers and only log once for each one, making it look a little more pleasent.

image

Copy link
Owner

@mooyoul mooyoul left a comment

Choose a reason for hiding this comment

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

Hello! First, Thank you for your contribution. I'm so sorry for late - I've been super busy.

Before merging this, I need some changes. Let me know if you need any further assistance!

index.js Outdated
@@ -199,6 +200,10 @@ class ServerlessPlugin {
args.unshift(TAG);
}

// Avoid logging the same resolved layer multiple times
Copy link
Owner

Choose a reason for hiding this comment

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

Please don't place business logic for specific use-case here. Think log method as console.log. This method should be for general logging. Move this to replaceLayerVersions method implementation instead.

Copy link
Owner

Choose a reason for hiding this comment

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

  1. declare set which stores "resolved" layer arn
  2. if set does not contain given layer arn, log & store given layer arn, otherwise, just skip.

and It would be appreciated if you would implement "log level" option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, I've updated the PR.
I just didn't do anything about "log level" because I didn't quite get what this option would do.

Copy link
Owner

@mooyoul mooyoul Nov 7, 2022

Choose a reason for hiding this comment

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

I thought we can implement a plugin option for controlling log output verbosity.

For example,

  • INFO: Most verbose logging output. Prints any informational log (e.g. prints entire layer replacements without deduplication, with entire context including function name and layer arn)
  • WARN: Prints warnings only. Prints warnings only (e.g. prints deduplicated layer replacements - which current PR does)
  • ERROR: Prints errors only. Prints errors only (e.g. unknown layer arn)

Then, Users can control log verbosity like this:

// serverless.yml

// ... (truncated)
plugins:
  - serverless-latest-layer-version
custom:
  latestLayerVersion:
    logLevel: WARN

Anyway this isn't mandatory for merging. You can ignore this request ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will happily ignore this request, but I think this would be a great addition to this plugin. I've created a new issue for this feature: #34.

Copy link
Owner

Choose a reason for hiding this comment

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

Thank you for creating an issue for that 👍

Copy link
Owner

@mooyoul mooyoul left a comment

Choose a reason for hiding this comment

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

One minor thing...

index.js Outdated
@@ -12,6 +12,7 @@ class ServerlessPlugin {
this.serverless = serverless;
this.provider = serverless.getProvider("aws");
this.options = options;
this.resolvedLayers = [];
Copy link
Owner

Choose a reason for hiding this comment

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

I prefer Set over Array to store unique items.

Suggested change
this.resolvedLayers = [];
this.resolvedLayerSet = new Set();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

index.js Outdated
@@ -181,6 +182,9 @@ class ServerlessPlugin {
const resolvedLayerArn = arnVersionMap.get(node);
if (resolvedLayerArn) {
this.update(resolvedLayerArn);
// Avoid logging the same resolved layer multiple times
if (self.resolvedLayers.some(item => item === resolvedLayerArn)) return
Copy link
Owner

Choose a reason for hiding this comment

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

Then this can be expressed as more cleaner way :)

Suggested change
if (self.resolvedLayers.some(item => item === resolvedLayerArn)) return
if (self.resolvedLayers.has(resolvedLayerArn)) {
return;
}
self.resolvedLayers.add(resolvedLayerArn);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And done

@rfoel rfoel requested a review from mooyoul November 8, 2022 12:51
@rfoel rfoel mentioned this pull request Nov 8, 2022
Copy link
Owner

@mooyoul mooyoul left a comment

Choose a reason for hiding this comment

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

Looks good to me. Really appreciated it!

@mooyoul mooyoul merged commit 33f289f into mooyoul:master Nov 8, 2022
@mooyoul
Copy link
Owner

mooyoul commented Nov 8, 2022

Just merged this PR and new release will be automatically published to npm. Stay tuned!

mooyoul pushed a commit that referenced this pull request Nov 8, 2022
## [2.1.2](v2.1.1...v2.1.2) (2022-11-08)

### Bug Fixes

* avoid logging the same resolved layer multiple times ([#33](#33)) ([33f289f](33f289f))
@mooyoul
Copy link
Owner

mooyoul commented Nov 8, 2022

🎉 This PR is included in version 2.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@mooyoul
Copy link
Owner

mooyoul commented Nov 8, 2022

Okay, v2.1.2 just published to npm. Please let me know that release fixes your issue (or if you need anything further).
Thank you for your contribution! <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants