Skip to content

Appending additional keys state kept between Lambda invocations #407

Closed
@mh-love

Description

@mh-love

From the "Appending additional keys" docs:

You can append additional keys using either mechanism:

Persist new keys across all future log messages via structure_logs method

"Persist new keys across all future log messages via structure_logs method" could be read as either:

  1. "new keys will be added to all future logs across all Lambda invocations "
  2. "new keys will be added to all future logs for the duration of a Lambda invocation".

I chose the latter because of your example, which I think implies that an order_id is appended to all logs for for the duration of a Lambda invocation:

def handler(event, context):
 order_id = event.get("order_id")
 logger.structure_logs(append=True, order_id=order_id)
 logger.info("Collecting payment")

 
 If I change your example to this:
 

 def handler(event, context):
   logger.info('Something before order_id is known')
   order_id = event.get("order_id")
   logger.structure_logs(append=True, order_id=order_id)
   logger.info("Collecting payment")

The result is:

  1. If order_id=1... Lambda is invoked the first time and the correct order_id of "1" is logged with "Collecting payment".
  2. If order_id=2... Lambda is invoked the second time and incorrect order_id is logged as "1" with "Something before order_id is known", and then the correct order_id of "2" is logged with "Collecting payment".
     
    Is this expected behavior? Seems like a state issue? Either way I think it might make sense to make the docs or examples more clear! Thanks!

Environment

  • Powertools version used: 1.14.0
  • Packaging format (Layers, PyPi): PyPi
  • AWS Lambda function runtime: Python 3.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdocumentationImprovements or additions to documentation

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions