Skip to content

Commit

Permalink
Record HTTP status code correctly when using abort() with Bottle (#943)
Browse files Browse the repository at this point in the history
* Record HTTP status code correctly when using abort() with Bottle

* removing double space

* 🐴
  • Loading branch information
equake authored and majorgreys committed May 20, 2019
1 parent 07d3188 commit c6bd5ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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

0 comments on commit c6bd5ff

Please sign in to comment.