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 new logic to sample debug logs #6142

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

leandrodamascena
Copy link
Contributor

Issue number: #6141

Summary

This PR improves the log sampling rate calculation mechanism in our Logger utility. It aims to provide a more consistent and expected behavior for customers using log sampling, especially when reusing Lambda environments.

Changes

  1. Introduced a new method refresh_sample_rate_calculation() in the Logger class.
  2. Updated the @logger.inject_lambda_context decorator to call refresh_sample_rate_calculation() at the end of each invocation.
  3. Modified the internal sampling logic to use the refreshed calculation.

Expected Behavior

  1. Customers will continue to set sampling_rate at the constructor level.
  2. When using @logger.inject_lambda_context, the sampling rate will be recalculated on every request, providing the expected sampling behavior.
  3. Customers not using the decorator can manually call refresh_sample_rate_calculation() to achieve the same effect.
  4. If neither the decorator nor manual refresh is used, sampling behavior may be unexpected.

Backwards Compatibility

This change is backwards compatible. Existing code will continue to work, but may benefit from adopting the new best practices.

Missing

  • Update the documentation

User experience

Using decorator

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger(level="INFO", sampling_rate=0.5)

@logger.inject_lambda_context
def lambda_handler(event, context: LambdaContext):
    logger.debug("test") # expected to be flushed in 50% of the requests
    pass

Using manual method

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger(level="INFO", sampling_rate=0.5)

def lambda_handler(event, context: LambdaContext):
    logger.debug("test") # expected to be flushed in 50% of the requests
    logger.refresh_sample_rate_calculation()
    pass

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

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.

@leandrodamascena leandrodamascena requested a review from a team as a code owner February 21, 2025 17:57
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 21, 2025
@leandrodamascena leandrodamascena linked an issue Feb 21, 2025 that may be closed by this pull request
2 tasks
@leandrodamascena leandrodamascena self-assigned this Feb 21, 2025
@github-actions github-actions bot added the feature New feature or functionality label Feb 21, 2025
Copy link

codecov bot commented Feb 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.25%. Comparing base (3d9ceb2) to head (3649531).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #6142   +/-   ##
========================================
  Coverage    96.25%   96.25%           
========================================
  Files          236      236           
  Lines        11399    11407    +8     
  Branches       828      830    +2     
========================================
+ Hits         10972    10980    +8     
  Misses         337      337           
  Partials        90       90           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 21, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 21, 2025
anafalcao
anafalcao previously approved these changes Feb 21, 2025
Copy link
Contributor

@anafalcao anafalcao left a comment

Choose a reason for hiding this comment

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

APPROVED

@leandrodamascena leandrodamascena dismissed anafalcao’s stale review February 21, 2025 20:53

Hey Ana! I'm dismissing the review because I'll need to change the documentation.

@leandrodamascena
Copy link
Contributor Author

Hey @dreamorosi! Pls I'd like a review in the documentation part. Thanks

@dreamorosi
Copy link
Contributor

cc @am29d - FYI we can lift the doc updates for the feature in our own PR.

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 24, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 24, 2025
@leandrodamascena
Copy link
Contributor Author

Thanks @dreamorosi! I've incorporated all your suggestions.

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 24, 2025
dreamorosi
dreamorosi previously approved these changes Feb 24, 2025
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.

Thanks for working on this and addressing the comments.

@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 24, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 24, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 24, 2025
@destinydick
Copy link

great work

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 25, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 25, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 25, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 25, 2025
@leandrodamascena
Copy link
Contributor Author

Hey @dreamorosi! I need your review here again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge feature New feature or functionality logger size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Calculate Log Sampling Per Request
4 participants