Skip to content

Commit 8e0ab8c

Browse files
authored
fix(logger): allow custom JMESPath functions to extract correlation ID (#3382)
1 parent ea7508f commit 8e0ab8c

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

aws_lambda_powertools/logging/logger.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@
2222
overload,
2323
)
2424

25-
import jmespath
26-
2725
from aws_lambda_powertools.logging import compat
2826
from aws_lambda_powertools.shared import constants
2927
from aws_lambda_powertools.shared.functions import (
3028
extract_event_from_common_models,
3129
resolve_env_var_choice,
3230
resolve_truthy_env_var_choice,
3331
)
32+
from aws_lambda_powertools.utilities import jmespath_utils
3433

3534
from ..shared.types import AnyCallableT
3635
from .exceptions import InvalidLoggerSamplingRateError
@@ -443,7 +442,9 @@ def decorate(event, context, *args, **kwargs):
443442
self.append_keys(cold_start=cold_start, **lambda_context.__dict__)
444443

445444
if correlation_id_path:
446-
self.set_correlation_id(jmespath.search(correlation_id_path, event))
445+
self.set_correlation_id(
446+
jmespath_utils.extract_data_from_envelope(envelope=correlation_id_path, data=event),
447+
)
447448

448449
if log_event:
449450
logger.debug("Event received")

docs/core/logger.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ When debugging in non-production environments, you can instruct Logger to log th
8585

8686
### Setting a Correlation ID
8787

88-
You can set a Correlation ID using `correlation_id_path` param by passing a [JMESPath expression](https://jmespath.org/tutorial.html){target="_blank" rel="nofollow"}.
88+
You can set a Correlation ID using `correlation_id_path` param by passing a [JMESPath expression](https://jmespath.org/tutorial.html){target="_blank" rel="nofollow"}, including [our custom JMESPath Functions](../utilities/jmespath_functions.md#powertools_json-function).
8989

9090
???+ tip
91-
You can retrieve correlation IDs via `get_correlation_id` method
91+
You can retrieve correlation IDs via `get_correlation_id` method.
9292

9393
=== "set_correlation_id.py"
9494

tests/functional/test_logger.py

+18
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,24 @@ def handler(event, context):
569569
assert request_id == log["correlation_id"]
570570

571571

572+
def test_logger_set_correlation_id_path_custom_functions(lambda_context, stdout, service_name):
573+
# GIVEN an initialized Logger
574+
# AND a Lambda handler decorated with a JMESPath expression using Powertools custom functions
575+
logger = Logger(service=service_name, stream=stdout)
576+
577+
@logger.inject_lambda_context(correlation_id_path="Records[*].powertools_json(body).id")
578+
def handler(event, context):
579+
...
580+
581+
# WHEN handler is called
582+
request_id = "xxx-111-222"
583+
mock_event = {"Records": [{"body": json.dumps({"id": request_id})}]}
584+
handler(mock_event, lambda_context)
585+
586+
# THEN there should be no exception and correlation ID should match
587+
assert logger.get_correlation_id() == [request_id]
588+
589+
572590
def test_logger_append_remove_keys(stdout, service_name):
573591
# GIVEN a Logger is initialized
574592
logger = Logger(service=service_name, stream=stdout)

0 commit comments

Comments
 (0)