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): Adding thread safe logging keys #5141

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

Conversation

SimonBFrank
Copy link

@SimonBFrank SimonBFrank commented Sep 8, 2024

Issue number: RFC: Support thread-local keys for logger #991

Summary

Changes

  • New logger methods:
    • append_thread_local_keys : add key to thread-local ContextVar
    • get_current_thread_keys: get all keys from thread-local ContextVar
    • remove_thread_local_keys: remove list of keys from thread-local ContextVar
    • clear_thread_local_keys: remove all keys from thread-local ContextVar
  • Add new logic to_extract_log_keys method to handle error when an additional logging key's value is not a string and the name conflicts with a reserved keys
  • clear_state in inject_lambda_context also clears thread-local keys

User experience

Before:

import concurrent.futures

from aws_lambda_powertools import Logger

logger = Logger(service="payment")


def process_payments(payment_id):
    logger.info("Start processing payment", extra={"payment_id": payment_id})
    ...
    logger.info("End processing payment", extra={"payment_id": payment_id})


with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    futures = [executor.submit(process_payments, payment_id) for payment_id in range(10)]
    for future in concurrent.futures.as_completed(futures):
        future.result()

After:

import concurrent.futures

from aws_lambda_powertools import Logger

logger = Logger(service="payment")


def process_payments(payment_id):
    logger.clear_thread_local_keys()
    logger.append_thread_local_keys(payment_id=payment_id)
    logger.info("Start processing payment")
    ...
    logger.info("End processing payment")


with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    futures = [executor.submit(process_payments, payment_id) for payment_id in range(10)]
    for future in concurrent.futures.as_completed(futures):
        future.result()

Checklist

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

Is this a breaking change?: No, existing functionality isn't broken.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.

@SimonBFrank SimonBFrank requested a review from a team as a code owner September 8, 2024 18:28
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation logger tests labels Sep 8, 2024
Copy link

boring-cyborg bot commented Sep 8, 2024

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Sep 8, 2024
@leandrodamascena
Copy link
Contributor

Assigned to @sthulb.

@github-actions github-actions bot added feature New feature or functionality and removed documentation Improvements or additions to documentation labels Sep 9, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Sep 9, 2024
Copy link

sonarcloud bot commented Sep 9, 2024

@SimonBFrank
Copy link
Author

@sthulb I have made some changes to my PR since the last batch of tests failed due to type checking and | not being supported in Python versions < 3.9. Can you please allow the additional test cases to run?

@sthulb
Copy link
Contributor

sthulb commented Sep 13, 2024

@SimonBFrank We have paused all development and merging in to develop/v3 branches until v3 has gone GA, which is currently scheduled for the 23rd now.

We will review and merge after that. Thank you for this contribution.

@sthulb sthulb changed the base branch from v2 to develop September 25, 2024 08:33
@boring-cyborg boring-cyborg bot added the github-actions Pull requests that update Github_actions code label Sep 25, 2024
@sthulb sthulb changed the base branch from develop to v2 September 25, 2024 08:33
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality github-actions Pull requests that update Github_actions code 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.

3 participants