powertools-lambda-python
powertools-lambda-python copied to clipboard
feat(logger): add thread safe logging keys
Issue number: #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
ininject_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.
- [x] Meet tenets criteria
- [x] I have performed a self-review of this change
- [x] Changes have been tested
- [x] Changes are documented
- [x] PR title follows conventional commit semantics
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.