Skip to content

fix(logger): allow custom JMESPath functions to extract correlation ID #3382

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

Merged
merged 6 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
overload,
)

import jmespath

from aws_lambda_powertools.logging import compat
from aws_lambda_powertools.shared import constants
from aws_lambda_powertools.shared.functions import (
extract_event_from_common_models,
resolve_env_var_choice,
resolve_truthy_env_var_choice,
)
from aws_lambda_powertools.utilities import jmespath_utils

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

if correlation_id_path:
self.set_correlation_id(jmespath.search(correlation_id_path, event))
self.set_correlation_id(
jmespath_utils.extract_data_from_envelope(envelope=correlation_id_path, data=event),
)

if log_event:
logger.debug("Event received")
Expand Down
4 changes: 2 additions & 2 deletions docs/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ When debugging in non-production environments, you can instruct Logger to log th

### Setting a Correlation ID

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"}.
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).

???+ tip
You can retrieve correlation IDs via `get_correlation_id` method
You can retrieve correlation IDs via `get_correlation_id` method.

=== "set_correlation_id.py"

Expand Down
18 changes: 18 additions & 0 deletions tests/functional/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,24 @@ def handler(event, context):
assert request_id == log["correlation_id"]


def test_logger_set_correlation_id_path_custom_functions(lambda_context, stdout, service_name):
# GIVEN an initialized Logger
# AND a Lambda handler decorated with a JMESPath expression using Powertools custom functions
logger = Logger(service=service_name, stream=stdout)

@logger.inject_lambda_context(correlation_id_path="Records[*].powertools_json(body).id")
def handler(event, context):
...

# WHEN handler is called
request_id = "xxx-111-222"
mock_event = {"Records": [{"body": json.dumps({"id": request_id})}]}
handler(mock_event, lambda_context)

# THEN there should be no exception and correlation ID should match
assert logger.get_correlation_id() == [request_id]


def test_logger_append_remove_keys(stdout, service_name):
# GIVEN a Logger is initialized
logger = Logger(service=service_name, stream=stdout)
Expand Down