Skip to content

Commit 0e89add

Browse files
committed
Attach stack trace to Flask errors
1 parent a64c95b commit 0e89add

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

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)