diff options
| author | Rob Hudson | 2009-03-21 10:54:56 -0700 |
|---|---|---|
| committer | Rob Hudson | 2009-03-21 10:56:08 -0700 |
| commit | 30ab58a69510f3fd35e19f6be9b669d2fdf956c0 (patch) | |
| tree | 5d03755816d3aa8e10cc435a2213e306d5572e3c /debug_toolbar/panels/sql.py | |
| parent | 2e0d4d3b7e2aad03743ca255b053caf55b0b73b2 (diff) | |
| parent | 1a52939905b7a43a9cf83cde6216a98cc5cbc595 (diff) | |
| download | django-debug-toolbar-30ab58a69510f3fd35e19f6be9b669d2fdf956c0.tar.bz2 | |
Adding stack traces to SQL panel to see where the calls are coming from.
Merge branch 'sql_stacktrace'
* sql_stacktrace:
Tuple unpacking of stacktrace variables for niceness.
Add preliminary support for stacktraces to see where SQL queries are coming from.
Diffstat (limited to 'debug_toolbar/panels/sql.py')
| -rw-r--r-- | debug_toolbar/panels/sql.py | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index b9800b8..8934fb3 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -1,5 +1,8 @@ +import os +import SocketServer import time -from debug_toolbar.panels import DebugPanel +import traceback +import django from django.conf import settings from django.db import connection from django.db.backends import util @@ -7,6 +10,28 @@ from django.template.loader import render_to_string from django.utils import simplejson from django.utils.encoding import force_unicode from django.utils.hashcompat import sha_constructor +from debug_toolbar.panels import DebugPanel + +# Figure out some paths +django_path = os.path.realpath(os.path.dirname(django.__file__)) +socketserver_path = os.path.realpath(os.path.dirname(SocketServer.__file__)) + +def tidy_stacktrace(strace): + """ + Clean up stacktrace and remove all entries that: + 1. Are part of Django (except contrib apps) + 2. Are part of SocketServer (used by Django's dev server) + 3. Are the last entry (which is part of our stacktracing code) + """ + trace = [] + for s in strace[:-1]: + s_path = os.path.realpath(s[0]) + if django_path in s_path and not 'django/contrib' in s_path: + continue + if socketserver_path in s_path: + continue + trace.append((s[0], s[1], s[2], s[3])) + return trace class DatabaseStatTracker(util.CursorDebugWrapper): """ @@ -19,6 +44,7 @@ class DatabaseStatTracker(util.CursorDebugWrapper): return self.cursor.execute(sql, params) finally: stop = time.time() + stacktrace = tidy_stacktrace(traceback.extract_stack()) _params = '' try: _params = simplejson.dumps([force_unicode(x) for x in params]) @@ -31,6 +57,7 @@ class DatabaseStatTracker(util.CursorDebugWrapper): 'raw_sql': sql, 'params': _params, 'hash': sha_constructor(settings.SECRET_KEY + sql + _params).hexdigest(), + 'stacktrace': stacktrace, }) util.CursorDebugWrapper = DatabaseStatTracker |
