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

Logger: inject_lambda_context overrides keys appended before in custom middleware #85

Closed
heitorlessa opened this issue Jul 5, 2020 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@heitorlessa
Copy link
Contributor

What were you trying to accomplish?

Have a custom middleware to inject additional keys to the Logger as well as using inject_lambda_context.

@lambda_handler_decorator(trace_execution=True)
def process_booking_handler(
    handler: Callable, event: Dict, context: Any, logger: Logger = None
) -> Callable:
    if logger is None:
        logger = Logger()

    handler = logger.inject_lambda_context(handler)

    logger.info("Injecting and annotating process booking state machine")
    process_booking_context = _build_process_booking_model(event)
    _logger_inject_process_booking_sfn(logger=logger, event=event)
    _tracer_annotate_process_booking_sfn(process_booking_context=process_booking_context)

    return handler(event, context)

Expected Behavior

Have additional key added to the log when structure_logs is used outside the handler e.g. shared file, before the handler

Current Behavior

Any additional key is removed once the handler is executed with inject_lambda_context

Possible Solution

inject_lambda_context is overwriting existing structured logs instead of appending

Steps to Reproduce (for bugs)

  1. Use structure_logs before inject_lambda_context is used

Code

    logger = Logger()
    logger.structure_logs(append=True, additional_key="test")

    @logger.inject_lambda_context
    def handler(event, context):
        logger.info("Hello") # will not contain additional_key

    handler({}, lambda_context)

Environment

  • Powertools version used: 1.0.0
  • Packaging format (Layers, PyPi): PyPi
  • AWS Lambda function runtime: Python 3.7
  • Debugging logs

How to enable debug mode**

# paste logs here
@heitorlessa heitorlessa added bug Something isn't working triage Pending triage from maintainers labels Jul 5, 2020
@heitorlessa heitorlessa self-assigned this Jul 5, 2020
@triage-new-issues triage-new-issues bot removed the triage Pending triage from maintainers label Jul 5, 2020
heitorlessa referenced this issue in heitorlessa/aws-lambda-powertools-python Jul 5, 2020
heitorlessa added a commit that referenced this issue Jul 5, 2020
* fix: append logs when injecting lambda context #85

* docs: update changelog

Signed-off-by: heitorlessa <[email protected]>

* chore: bump version to 1.0.1

Signed-off-by: heitorlessa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

1 participant