From 4e82b675797fb4f05bd9954f240602d2fe181915 Mon Sep 17 00:00:00 2001 From: "Marcos A. Sobrinho" Date: Fri, 17 May 2019 14:47:38 -0300 Subject: [PATCH 1/3] Record HTTP status code correctly when using abort() with Bottle --- ddtrace/contrib/bottle/trace.py | 7 ++++++- tests/contrib/bottle/test.py | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/ddtrace/contrib/bottle/trace.py b/ddtrace/contrib/bottle/trace.py index 36e678be293..c67595ca3bd 100644 --- a/ddtrace/contrib/bottle/trace.py +++ b/ddtrace/contrib/bottle/trace.py @@ -1,5 +1,5 @@ # 3p -from bottle import response, request +from bottle import response, request, HTTPError # stdlib import ddtrace @@ -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. diff --git a/tests/contrib/bottle/test.py b/tests/contrib/bottle/test.py index bf3eb80f6be..6438b44bdf6 100644 --- a/tests/contrib/bottle/test.py +++ b/tests/contrib/bottle/test.py @@ -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 as e: + 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/') From 8cbb2acd5bf76b526c7774c437024c63cf087c79 Mon Sep 17 00:00:00 2001 From: "Marcos A. Sobrinho" Date: Fri, 17 May 2019 14:57:44 -0300 Subject: [PATCH 2/3] removing double space --- tests/contrib/bottle/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/contrib/bottle/test.py b/tests/contrib/bottle/test.py index 6438b44bdf6..ac92bf09b3b 100644 --- a/tests/contrib/bottle/test.py +++ b/tests/contrib/bottle/test.py @@ -97,7 +97,7 @@ def hi(): def test_abort(self): @self.app.route('/hi') def hi(): - raise bottle.abort(420, 'Enhance Your Calm') + raise bottle.abort(420, 'Enhance Your Calm') self._trace_app(self.tracer) # make a request From d54135eab8d7896a17940830a8f75d3512206e78 Mon Sep 17 00:00:00 2001 From: "Marcos A. Sobrinho" Date: Fri, 17 May 2019 15:12:25 -0300 Subject: [PATCH 3/3] :horse: --- tests/contrib/bottle/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/contrib/bottle/test.py b/tests/contrib/bottle/test.py index ac92bf09b3b..079c7b93ec7 100644 --- a/tests/contrib/bottle/test.py +++ b/tests/contrib/bottle/test.py @@ -104,7 +104,7 @@ def hi(): try: resp = self.app.get('/hi') assert resp.status_int == 420 - except Exception as e: + except Exception: pass spans = self.tracer.writer.pop()