Skip to content

Proper handling of HTTPResponse with bottle.py #1156

Description

@mandreascz

Hi,

I noticed that TracePluging for bottle is incorrectly logging event status code in case we use raise to return the response.

We are doing some validation so we want to return 400 status code. DD logs it as 500 instead.

Which version of dd-trace-py are you using?

datadog==0.31.0
ddtrace==0.30.2

How can we reproduce your problem?

# demo.py

import bottle
from ddtrace.contrib.bottle import TracePlugin


RESPONSE = '{"hello": "json"}'

def create_response():
    res = bottle.HTTPResponse(body=RESPONSE, status=400)
    res.content_type = "application/json"
    return res


@bottle.route("/raising")
def raising():
    """This causes 500 status_code to appear in DD logs"""
    raise create_response()


@bottle.route("/returning")
def returning():
    return create_response()

application = bottle.app()
application.install(TracePlugin(service="demo"))

application.run()

Then run the app with python demo.py

What is the result that you get?

Both endpoints are correctly returning following response:

 $ http http://localhost:8080/returning
HTTP/1.0 400 Bad Request
Content-Length: 17
Content-Type: application/json
Date: Mon, 02 Dec 2019 11:15:09 GMT
Server: WSGIServer/0.2 CPython/3.8.0

{
    "hello": "json"
}

But event with status code 500 is logged into DD.

What is result that you expected?

Expected bahavior is to use status_code from HTTPResponse instead - so logging event with status code 400 in this case.

TracePlugin does not honor the status_code we set in HTTPResponse.

So when we use raise general Exception is caught and event with status code 500 is logged into DD.

Problem lies in this try/except https://github.com/DataDog/dd-trace-py/blob/master/ddtrace/contrib/bottle/trace.py#L55

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions