diff options
| author | Rob Hudson | 2009-01-23 10:59:59 -0800 | 
|---|---|---|
| committer | Rob Hudson | 2009-01-23 10:59:59 -0800 | 
| commit | 85bd265bd5fe9dda44c67c2b2e618a79c2fb8fdc (patch) | |
| tree | 6b4b43b112ef98321b2e78d1b486f494cc4dd1e8 /debug_toolbar/panels/sql.py | |
| parent | c3bcecfd13d8c82c9ac0a88a578bdeb4ad7cfc9d (diff) | |
| download | django-debug-toolbar-85bd265bd5fe9dda44c67c2b2e618a79c2fb8fdc.tar.bz2 | |
Add preliminary support for stacktraces to see where SQL queries are coming from.
I'm wanting to test this a bit more before I merge it in fully.  Feedback welcome.
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 7396c3a..0369a59 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 | 
