Skip to content

Commit 0890d36

Browse files
committed
Merge pull request #219 from colinhowe/master
Make stacktraces optional
2 parents 9067568 + 295f6f8 commit 0890d36

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ The debug toolbar has two settings that can be set in `settings.py`:
124124
* `TAG`: If set, this will be the tag to which debug_toolbar will attach the
125125
debug toolbar. Defaults to 'body'.
126126

127+
* `ENABLE_STACKTRACES`: If set, this will show stacktraces for SQL queries.
128+
Enabling stacktraces can increase the CPU time used when executing
129+
queries. Defaults to True.
130+
127131
Example configuration::
128132

129133
def custom_show_toolbar(request):
@@ -135,6 +139,7 @@ The debug toolbar has two settings that can be set in `settings.py`:
135139
'EXTRA_SIGNALS': ['myproject.signals.MySignal'],
136140
'HIDE_DJANGO_SQL': False,
137141
'TAG': 'div',
142+
'ENABLE_STACKTRACES' : True,
138143
}
139144

140145
`debugsqlshell`

debug_toolbar/utils/tracking/db.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ def execute(self, sql, params=()):
7171
finally:
7272
stop = datetime.now()
7373
duration = ms_from_timedelta(stop - start)
74-
stacktrace = tidy_stacktrace(reversed(inspect.stack()))
74+
enable_stacktraces = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) \
75+
.get('ENABLE_STACKTRACES', True)
76+
if enable_stacktraces:
77+
stacktrace = tidy_stacktrace(reversed(inspect.stack()))
78+
else:
79+
stacktrace = []
7580
_params = ''
7681
try:
7782
_params = simplejson.dumps([force_unicode(x, strings_only=True) for x in params])

tests/tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,28 @@ def test_recording(self):
204204
self.assertTrue('duration' in query[1])
205205
self.assertTrue('stacktrace' in query[1])
206206

207+
# ensure the stacktrace is populated
208+
self.assertTrue(len(query[1]['stacktrace']) > 0)
209+
210+
def test_disable_stacktraces(self):
211+
panel = self.toolbar.get_panel(SQLDebugPanel)
212+
self.assertEquals(len(panel._queries), 0)
213+
214+
with Settings(DEBUG_TOOLBAR_CONFIG={ 'ENABLE_STACKTRACES' : False }):
215+
list(User.objects.all())
216+
217+
# ensure query was logged
218+
self.assertEquals(len(panel._queries), 1)
219+
query = panel._queries[0]
220+
self.assertEquals(query[0], 'default')
221+
self.assertTrue('sql' in query[1])
222+
self.assertTrue('duration' in query[1])
223+
self.assertTrue('stacktrace' in query[1])
224+
225+
# ensure the stacktrace is empty
226+
self.assertEquals([], query[1]['stacktrace'])
227+
228+
207229
class TemplatePanelTestCase(BaseTestCase):
208230
def test_queryset_hook(self):
209231
template_panel = self.toolbar.get_panel(TemplateDebugPanel)

0 commit comments

Comments
 (0)