-
Notifications
You must be signed in to change notification settings - Fork 421
feat(logger): add thread safe logging keys #5141
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
Conversation
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
Assigned to @sthulb. |
7fc9e57
to
22956ce
Compare
@sthulb I have made some changes to my PR since the last batch of tests failed due to type checking and |
@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. |
This is fine, just got to rebase it into v3 |
@leandrodamascena Hey, thanks for the feedback. My responses may be delayed for the next month as my personal life is getting busy. I think making the logger thread safe by default would not be what I would want as a feature. Let me explain through an example. I have a Lambda that processes an order that has multiple items. Each item within an order is processed in a separate thread. When I start my function I used Code: import threading
from typing import List
from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger()
def threaded_func(item_id: str):
logger.append_thread_local_keys(item_id=item_id)
logger.info("Collecting payment")
def lambda_handler(event: dict, context: LambdaContext) -> str:
order_id: str = event["order_id"]
item_ids: List[str] = event["item_ids"]
logger.append_keys(order_id=order_id)
for item in item_ids:
threading.Thread(target=threaded_func, args=(item,)).start()
return "Success" Input: lambda_handler({"order_id": "abc123", "item_ids": ["A1", "A2"]}, {}) Output:
This is great because I only need to add the order_id once in the beginning of the Lambda call and will be used everywhere. I can use |
Hi @SimonBFrank! Thanks for the feedback! Last week was very busy and I'll check it out this week to merge the PR. |
Hey @leandrodamascena, any updates on getting this reviewed and merged? |
Hey @SimonBFrank! I've had to deal with some unexpected internal reprioritizations this past week. I dropped the ball a bit here, but tomorrow I'll push some small changes and tests for us to merge, ok? Thanks |
Update 21/10/2024:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @SimonBFrank! Thanks for your patience and hard work on this PR. I sent a commit with some small changes, but basically I renamed the methods and refactored the documentation to make it more specific.
Please let me know if you have any observation before I ask the @sthulb and merge it.
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |
Looks amazing to me. I will let me team know that this is ready to use. Thanks for the help! |
Issue number: #991
Summary
Changes
append_thread_local_keys
: add key to thread-local ContextVarget_current_thread_keys
: get all keys from thread-local ContextVarremove_thread_local_keys
: remove list of keys from thread-local ContextVarclear_thread_local_keys
: remove all keys from thread-local ContextVar_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 keysclear_state
ininject_lambda_context
also clears thread-local keysUser experience
Before:
After:
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:
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.