Skip to content

API Gateway : Debug mode - print in multiple lines instead of a single one #972

Closed

Description

@filol

What were you trying to accomplish?
When I activate the debug mode for API Gateaway (https://awslabs.github.io/aws-lambda-powertools-python/1.24.2/core/event_handler/api_gateway/#debug-mode) it's spam my cloudwatch log. Instead havings a single entry with the object, it's creating a single entry for each line of input object.

Expected Behavior

One line
image

Current Behavior

One cloudwatch entry for each line of the object :
image

Steps to Reproduce (for bugs)

app = ApiGatewayResolver(debug=True) 

@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
def lambda_handler(event, context):
    return app.resolve(event, context)
    
@app.get("/api/v1/test")
def test():
    return "Hello"

Environment

  • Powertools version used: 1.24.1
  • Packaging format (Layers, PyPi):
  • AWS Lambda function runtime: Python 3.9 - Graviton

How to enable debug mode**

# paste logs here

Activity

  1. added
    bug
    triage
    on Jan 23, 2022
  2. heitorlessa commented on Jan 24, 2022

    @heitorlessa
    Contributor

    Hey @filol thanks a lot for raising this - first time I'm seeing a debug entry like this, since we're sending all of this as a string to avoid this already.

    https://github.com/awslabs/aws-lambda-powertools-python/blob/develop/aws_lambda_powertools/event_handler/api_gateway.py#L499

    Unsure whether CloudWatch is interpreting longer strings any differently, but we'll setup a dummy project to try reproducing it.

    Thank you!

  3. filol commented on Jan 24, 2022

    @filol
    Author

    Hello @heitorlessa I also want to specify that if I print the event manually via logger.info at the beginning of my function, it is displayed on one and the same line

  4. heitorlessa commented on Jan 25, 2022

    @heitorlessa
    Contributor

    I'll try to reproduce it on Thursday. I suspect despite being a single string, print is adding a new line by default, and CloudWatch Logs is interpreting it differently - if that's the case, we can solve it easily

  5. michaelbrewer commented on Jan 29, 2022

    @michaelbrewer
    Contributor

    i will have a look too

  6. added and removed
    triage
    on Jan 31, 2022
  7. heitorlessa commented on Jan 31, 2022

    @heitorlessa
    Contributor

    Diagnosis

    Confirmed. It's the extra indentation added for local debugging purposes (SAM CLI, local tests, Postman) but breaks in the Console.

    https://github.com/awslabs/aws-lambda-powertools-python/blob/develop/aws_lambda_powertools/event_handler/api_gateway.py#L451:L451

    The extra indentation adds a newline for each key which CloudWatch Logs Console interprets it differently compared to logger.info or simply print(json.dumps(event)) as \n is only added at the end of the string, not per key.

    Verdict

    UPDATE: Spoke to @jaredcnance from CloudWatch team and they can't know if the next newline is related hence creating a new entry. Since this will incur in additional charges for customers it doesn't conform with our tenets, we will remove indentation.

    Thank you again for bringing this up @filol

    I'm tempted to keep this under "it's a feature, not a bug". In the meantime, I believe we should do two things unless you @filol strongly disagree with it:

    1. Reach out to CloudWatch Logs team and ask whether this could be improved on their side (this will also happen with Chalice and any other frameworks with debugging enabled)
    2. Re-emphasize in our docs that this is optimized for local development

    To demonstrate its value as-is, this is how local dev looks like - 1st line is a log_event/logger.info and the 2nd line is the debug mode with the indentation:

    image

  8. added
    pending-release
    on Jan 31, 2022
  9. heitorlessa commented on Jan 31, 2022

    @heitorlessa
    Contributor

    Done, should be available in the next release.

  10. github-actions commented on Feb 9, 2022

    @github-actions
    Contributor

    This is now released under 1.25.0 version!

  11. removed
    pending-release
    on Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugp1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions