Skip to content
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

fix: append structured logs when injecting lambda context #86

Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.0.1] - 2020-07-06
### Fixed
- **Logger**: Fix a bug with `inject_lambda_context` causing existing an Logger keys to be overriden if `structure_logs` was called before

## [1.0.0] - 2020-06-18
### Added
- **Metrics**: `add_metadata` method to add any metric metadata you'd like to ease finding metric related data via CloudWatch Logs
Expand Down
2 changes: 1 addition & 1 deletion aws_lambda_powertools/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def decorate(event, context):
lambda_context = build_lambda_context_model(context)
cold_start = _is_cold_start()

self.structure_logs(cold_start=cold_start, **lambda_context.__dict__)
self.structure_logs(append=True, cold_start=cold_start, **lambda_context.__dict__)
return lambda_handler(event, context)

return decorate
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aws_lambda_powertools"
version = "1.0.0"
version = "1.0.1"
description = "Python utilities for AWS Lambda functions including but not limited to tracing, logging and custom metric"
authors = ["Amazon Web Services"]
classifiers=[
Expand Down
27 changes: 27 additions & 0 deletions tests/functional/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,30 @@ def test_logger_invalid_sampling_rate():
# THEN we should raise InvalidLoggerSamplingRateError
with pytest.raises(InvalidLoggerSamplingRateError):
Logger(sampling_rate="TEST")


def test_inject_lambda_context_with_structured_log(lambda_context, stdout):
# GIVEN Logger is initialized
logger = Logger(stream=stdout)

# WHEN structure_logs has been used to add an additional key upfront
# and a lambda function is decorated with logger.inject_lambda_context
logger.structure_logs(append=True, additional_key="test")

@logger.inject_lambda_context
def handler(event, context):
logger.info("Hello")

handler({}, lambda_context)

# THEN lambda contextual info should always be in the logs
log = capture_logging_output(stdout)
expected_logger_context_keys = (
"function_name",
"function_memory_size",
"function_arn",
"function_request_id",
"additional_key",
)
for key in expected_logger_context_keys:
assert key in log