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

Record HTTP status code correctly when using abort() with Bottle #943

Merged
merged 4 commits into from
May 20, 2019
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
7 changes: 6 additions & 1 deletion ddtrace/contrib/bottle/trace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 3p
from bottle import response, request
from bottle import response, request, HTTPError

# stdlib
import ddtrace
Expand Down Expand Up @@ -47,6 +47,11 @@ def wrapped(*args, **kwargs):
code = 0
try:
return callback(*args, **kwargs)
except HTTPError as e:
# you can interrupt flows using abort(status_code, 'message')...
# we need to respect the defined status_code.
code = e.status_code
raise
except Exception:
# bottle doesn't always translate unhandled exceptions, so
# we mark it here.
Expand Down
23 changes: 23 additions & 0 deletions tests/contrib/bottle/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ def hi():
assert s.get_tag('http.method') == 'GET'
assert s.get_tag(http.URL) == 'http://localhost:80/hi'

def test_abort(self):
@self.app.route('/hi')
def hi():
raise bottle.abort(420, 'Enhance Your Calm')
self._trace_app(self.tracer)

# make a request
try:
resp = self.app.get('/hi')
assert resp.status_int == 420
except Exception:
pass

spans = self.tracer.writer.pop()
assert len(spans) == 1
s = spans[0]
assert s.name == 'bottle.request'
assert s.service == 'bottle-app'
assert s.resource == 'GET /hi'
assert s.get_tag('http.status_code') == '420'
assert s.get_tag('http.method') == 'GET'
assert s.get_tag(http.URL) == 'http://localhost:80/hi'

def test_bottle_global_tracer(self):
# without providing a Tracer instance, it should work
@self.app.route('/home/')
Expand Down