Skip to content

Make stacktraces optional #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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