Skip to content

fix(logger): prevent overwriting standard keys #3553

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

Merged
merged 11 commits into from
Feb 5, 2025

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented Feb 4, 2025

Summary

Changes

Please provide a summary of what's being changed

This PR fixes a bug that allowed customer payloads to override Powertools-managed keys in logs.

Logger has a set of keys that are added to every log entry, for example level, timestamp, service, and more. These keys are what make logs discoverable.

Due to a bug, in some cases when customers appended keys to the Logger instance or passed extra keys to a log method, some of the service keys would be overridden by the customer-provided keys.

For example:

const user = {
  name: "john",
  level: "assistant"
};

logger.debug("user info", user);

In this case the level key from the user object would override the level in the log entry.

This PR fixes this bug and makes it so that when a customer-provided key conflicts with a reserved key, Logger will drop the customer one and log a warning.

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: fixes #3217


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.

@dreamorosi dreamorosi self-assigned this Feb 4, 2025
@dreamorosi dreamorosi linked an issue Feb 4, 2025 that may be closed by this pull request
@boring-cyborg boring-cyborg bot added logger This item relates to the Logger Utility tests PRs that add or change tests labels Feb 4, 2025
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Feb 4, 2025
@github-actions github-actions bot added the bug Something isn't working label Feb 4, 2025
@dreamorosi
Copy link
Contributor Author

Ran e2e tests and they're passing.

@dreamorosi dreamorosi marked this pull request as ready for review February 4, 2025 17:40
@dreamorosi dreamorosi requested a review from a team February 4, 2025 17:40
@dreamorosi dreamorosi requested a review from a team as a code owner February 4, 2025 17:40
@leandrodamascena
Copy link
Contributor

I'll review this today morning.

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Hi @dreamorosi, I left a comment about protected keys.

Another thing is that even I consider this a bug because the customer may end up having some logs with level: 'BLABLA and this is not a valid log level + we should enforce good practices. But this is a breaking change both in terms of possible data loss and in the customers' CI with warnings popping up in their pipeline and possible breaking this. We must highlight this in the documentation/release notes and monitor if we have customers complaining.

My vote is to move forward with this PR, but monitor the feedback after releasing it.

@dreamorosi
Copy link
Contributor Author

Another thing is that even I consider this a bug because the customer may end up having some logs with level: 'BLABLA and this is not a valid log level + we should enforce good practices. But this is a breaking change both in terms of possible data loss and in the customers' CI with warnings popping up in their pipeline and possible breaking this. We must highlight this in the documentation/release notes and monitor if we have customers complaining.

My vote is to move forward with this PR, but monitor the feedback after releasing it.

I added two callouts to the docs, one in the structured keys section:

image

and one in the section that talks about adding keys (the link goes to the previously mentioned one):

image

I also updated the API docs for the two methods so they include the same info:

see in IDE:

image

and in the API docs:

image


I also improved the types of the methods so that customers can get an error in their IDE if they try to set one of these keys.

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 5, 2025
Copy link

sonarqubecloud bot commented Feb 5, 2025

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

@leandrodamascena leandrodamascena self-requested a review February 5, 2025 12:36
Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Thank you for improving the PR based on feedback and helping customers identify potential issues before pushing code to production.

Approved.

@dreamorosi dreamorosi merged commit f0bdf3c into main Feb 5, 2025
40 checks passed
@dreamorosi dreamorosi deleted the fix/logger_key_overwrite branch February 5, 2025 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation logger This item relates to the Logger Utility size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
Development

Successfully merging this pull request may close these issues.

Bug: replace system fields value from seconds parameters
2 participants