Skip to content

Commit

Permalink
Make stacktraces configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhowe committed Oct 7, 2011
1 parent 9067568 commit 295f6f8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ The debug toolbar has two settings that can be set in `settings.py`:
* `TAG`: If set, this will be the tag to which debug_toolbar will attach the
debug toolbar. Defaults to 'body'.

* `ENABLE_STACKTRACES`: If set, this will show stacktraces for SQL queries.
Enabling stacktraces can increase the CPU time used when executing
queries. Defaults to True.

Example configuration::

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

`debugsqlshell`
Expand Down
7 changes: 6 additions & 1 deletion debug_toolbar/utils/tracking/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ def execute(self, sql, params=()):
finally:
stop = datetime.now()
duration = ms_from_timedelta(stop - start)
stacktrace = tidy_stacktrace(reversed(inspect.stack()))
enable_stacktraces = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) \
.get('ENABLE_STACKTRACES', True)
if enable_stacktraces:
stacktrace = tidy_stacktrace(reversed(inspect.stack()))
else:
stacktrace = []
_params = ''
try:
_params = simplejson.dumps([force_unicode(x, strings_only=True) for x in params])
Expand Down
22 changes: 22 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,28 @@ def test_recording(self):
self.assertTrue('duration' in query[1])
self.assertTrue('stacktrace' in query[1])

# ensure the stacktrace is populated
self.assertTrue(len(query[1]['stacktrace']) > 0)

def test_disable_stacktraces(self):
panel = self.toolbar.get_panel(SQLDebugPanel)
self.assertEquals(len(panel._queries), 0)

with Settings(DEBUG_TOOLBAR_CONFIG={ 'ENABLE_STACKTRACES' : False }):
list(User.objects.all())

# ensure query was logged
self.assertEquals(len(panel._queries), 1)
query = panel._queries[0]
self.assertEquals(query[0], 'default')
self.assertTrue('sql' in query[1])
self.assertTrue('duration' in query[1])
self.assertTrue('stacktrace' in query[1])

# ensure the stacktrace is empty
self.assertEquals([], query[1]['stacktrace'])


class TemplatePanelTestCase(BaseTestCase):
def test_queryset_hook(self):
template_panel = self.toolbar.get_panel(TemplateDebugPanel)
Expand Down

0 comments on commit 295f6f8

Please sign in to comment.