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(metrics): add ability to pass custom logger #3057

Merged
merged 9 commits into from
Nov 7, 2024

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented Sep 12, 2024

Summary

Changes

Please provide a summary of what's being changed

This PR adds the ability to pass a custom logger object to the Metrics utility. This is useful when you want to enable debug logs or suppress warnings emitted by the utility.

Customers have been asking for a way to silence warnings which is not possible today due to Node.js not having a standard logging library similar to other languages such as Python. To go around the issue, I decided to follow a pattern used by other Node.js libraries in the ecosystem such as AWS SDK for JavaScript v3 and AWS X-Ray SDK for Node.js, which accept a generic "logger" class.

When the logger class is present, these libraries start emitting debug logs and other telemetry. In our case we'll follow the same pattern for debug logs but fall back to the global console object for warnings since we always want to emit them.

Likewise, in the case of Metrics, the actual EMF blobs will always bypass any custom logger passed by customer and go to stdout. I deliberately choose to go this route to avoid compatibility issues with 3rd party loggers and decrease the risk of data loss.

While I hope customers will use this feature with our Logger, the interface for the logger object is as generic as possible and allows 3rd party loggers or even the Node.js console object (aka global console).

For now I opted for documenting this feature only in the API docs and not in the main docs as I want to soft-launch it before publicising it more. In future releases we'll work on adding some debug logs similar to what Powertools for AWS Lambda (Python) does, which was something we couldn't do up until now.

Once that work is done and this pattern is applied across more utilities we'll update the docs as well. This will also give us some time to learn how customers use the feature.

Example

This is the DX of how customers would specify a custom logger, when using the Powertools for AWS one this gives the added benefit of controlling the log level and silencing warnings as needed.

import { Logger } from '@aws-lambda-powertools/logger';
import { Metrics } from '@aws-lambda-powertools/metrics';

const logger = new Logger({ logLevel: 'error' });
const metrics = new Metrics({ logger });

// ...

This pattern is also useful for unit testing, as it essentially allows dependency injection as well.

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

Issue number: closes #2036


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 Sep 12, 2024
@boring-cyborg boring-cyborg bot added commons This item relates to the Commons Utility metrics This item relates to the Metrics Utility tests PRs that add or change tests labels Sep 12, 2024
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Sep 12, 2024
@github-actions github-actions bot added the feature PRs that introduce new features or minor changes label Sep 12, 2024
Copy link

@dreamorosi dreamorosi marked this pull request as ready for review September 27, 2024 14:12
@dreamorosi dreamorosi requested review from a team as code owners September 27, 2024 14:12
@dreamorosi dreamorosi marked this pull request as draft September 27, 2024 14:34
@dreamorosi
Copy link
Contributor Author

Integration tests are failing, will look into it tomorrow.

@dreamorosi
Copy link
Contributor Author

Once #3292 is merged, I'll be ready to mark this as ready for review.

@dreamorosi dreamorosi force-pushed the feat/metrics_custom_logger branch from 785d9ad to 652a5c5 Compare November 6, 2024 10:09
@dreamorosi
Copy link
Contributor Author

Integration tests are finally passing.

@dreamorosi dreamorosi marked this pull request as ready for review November 6, 2024 16:55
@dreamorosi dreamorosi requested a review from am29d November 6, 2024 17:37
Copy link

sonarqubecloud bot commented Nov 7, 2024

Copy link
Contributor

@am29d am29d left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, LGTM!

I checked the e2e test after main rebase they seem to be flake for the logger. Need to investiage later.

@am29d am29d merged commit a531b90 into main Nov 7, 2024
47 checks passed
@am29d am29d deleted the feat/metrics_custom_logger branch November 7, 2024 10:28
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 metrics This item relates to the Metrics Utility size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
2 participants