Skip to content

Commit c2df100

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #302 from benjamin/flask-stacktrace
Attach stack trace to Flask errors
2 parents e811f7d + 0e89add commit c2df100

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

ddtrace/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def send_traces(self, traces):
5353
self._downgrade()
5454
return self.send_traces(traces)
5555

56-
log.debug("reported %d spans in %.5fs", len(traces), time.time() - start)
56+
log.debug("reported %d traces in %.5fs", len(traces), time.time() - start)
5757
return response
5858

5959
def send_services(self, services):

ddtrace/contrib/flask/middleware.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,17 @@ def _finish_span(self, response=None, exception=None):
116116
# if we didn't get a response, but we did get an exception, set
117117
# codes accordingly.
118118
if not response and exception:
119-
error = 1
120119
code = 500
120+
# The 3 next lines might not be strictly required, since `set_traceback`
121+
# also get the exception from the sys.exc_info (and fill the error meta).
122+
# Since we aren't sure it always work/for insuring no BC break, keep
123+
# these lines which get overridden anyway.
124+
error = 1
121125
span.set_tag(errors.ERROR_TYPE, type(exception))
122126
span.set_tag(errors.ERROR_MSG, exception)
127+
# The provided `exception` object doesn't have a stack trace attached,
128+
# so attach the stack trace with `set_traceback`.
129+
span.set_traceback()
123130

124131
# the endpoint that matched the request is None if an exception
125132
# happened so we fallback to a common resource

tests/contrib/flask/test_flask.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import logging
55
import os
6+
import re
67

78
# 3p
89
from flask import Flask, render_template
@@ -260,8 +261,8 @@ def test_fatal(self):
260261
eq_(s.meta.get(http.STATUS_CODE), '500')
261262
eq_(s.meta.get(http.METHOD), 'GET')
262263
assert "ZeroDivisionError" in s.meta.get(errors.ERROR_TYPE)
263-
msg = s.meta.get(errors.ERROR_MSG)
264-
assert "by zero" in msg, msg
264+
assert "by zero" in s.meta.get(errors.ERROR_MSG)
265+
assert re.search('File ".*/contrib/flask/test_flask.py", line [0-9]+, in fatal', s.meta.get(errors.ERROR_STACK))
265266

266267
def test_unicode(self):
267268
start = time.time()

0 commit comments

Comments
 (0)