-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logger): add thread safe logging keys (#5141)
* Getting baseline thread safe keys working * Functional tests for thread safe keys * Updating documentation * Cleanup * Small fixes for linting * Clearing thread local keys with clear_state=True * Cleaning up PR * Small fixes to docs * Fixing type annotations for THREAD_LOCAL_KEYS * Replacing '|' with {**dict1, **dict2} due to support of Python < 3.9 * fix types from v2 to v3 * Changing documentation and method names --------- Co-authored-by: Simon Thulbourn <sthulb@users.noreply.github.com> Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
- Loading branch information
1 parent
fd609bc
commit d3698d2
Showing
13 changed files
with
459 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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(order_id: str): | ||
logger.thread_safe_append_keys(order_id=order_id, thread_id=threading.get_ident()) | ||
logger.info("Collecting payment") | ||
|
||
|
||
def lambda_handler(event: dict, context: LambdaContext) -> str: | ||
order_ids: List[str] = event["order_ids"] | ||
|
||
threading.Thread(target=threaded_func, args=(order_ids[0],)).start() | ||
threading.Thread(target=threaded_func, args=(order_ids[1],)).start() | ||
|
||
return "hello world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
{ | ||
"level": "INFO", | ||
"location": "threaded_func:11", | ||
"message": "Collecting payment", | ||
"timestamp": "2024-09-08 03:04:11,316-0400", | ||
"service": "payment", | ||
"order_id": "order_id_value_1", | ||
"thread_id": "3507187776085958" | ||
}, | ||
{ | ||
"level": "INFO", | ||
"location": "threaded_func:11", | ||
"message": "Collecting payment", | ||
"timestamp": "2024-09-08 03:04:11,316-0400", | ||
"service": "payment", | ||
"order_id": "order_id_value_2", | ||
"thread_id": "140718447808512" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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(order_id: str): | ||
logger.thread_safe_append_keys(order_id=order_id, thread_id=threading.get_ident()) | ||
logger.info("Collecting payment") | ||
logger.thread_safe_clear_keys() | ||
logger.info("Exiting thread") | ||
|
||
|
||
def lambda_handler(event: dict, context: LambdaContext) -> str: | ||
order_ids: List[str] = event["order_ids"] | ||
|
||
threading.Thread(target=threaded_func, args=(order_ids[0],)).start() | ||
threading.Thread(target=threaded_func, args=(order_ids[1],)).start() | ||
|
||
return "hello world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[ | ||
{ | ||
"level": "INFO", | ||
"location": "threaded_func:11", | ||
"message": "Collecting payment", | ||
"timestamp": "2024-09-08 12:26:10,648-0400", | ||
"service": "payment", | ||
"order_id": "order_id_value_1", | ||
"thread_id": 140077070292544 | ||
}, | ||
{ | ||
"level": "INFO", | ||
"location": "threaded_func:11", | ||
"message": "Collecting payment", | ||
"timestamp": "2024-09-08 12:26:10,649-0400", | ||
"service": "payment", | ||
"order_id": "order_id_value_2", | ||
"thread_id": 140077061899840 | ||
}, | ||
{ | ||
"level": "INFO", | ||
"location": "threaded_func:13", | ||
"message": "Exiting thread", | ||
"timestamp": "2024-09-08 12:26:10,649-0400", | ||
"service": "payment" | ||
}, | ||
{ | ||
"level": "INFO", | ||
"location": "threaded_func:13", | ||
"message": "Exiting thread", | ||
"timestamp": "2024-09-08 12:26:10,649-0400", | ||
"service": "payment" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
logger = Logger() | ||
|
||
|
||
@logger.inject_lambda_context | ||
def lambda_handler(event: dict, context: LambdaContext) -> str: | ||
logger.info("Collecting payment") | ||
|
||
if "order" not in logger.thread_safe_get_current_keys(): | ||
logger.thread_safe_append_keys(order=event.get("order")) | ||
|
||
return "hello world" |
Oops, something went wrong.