Skip to content

Instantly share code, notes, and snippets.

@mdogo
Forked from roger-link/pycallgraph_middleware.py
Last active August 29, 2015 14:06
Show Gist options
  • Save mdogo/5620dfcfd4cfa31063f2 to your computer and use it in GitHub Desktop.
Save mdogo/5620dfcfd4cfa31063f2 to your computer and use it in GitHub Desktop.
import time
from pycallgraph import Config, GlobbingFilter, PyCallGraph
from pycallgraph.output import GraphvizOutput
from django.conf import settings
class CallgraphMiddleware(object):
def process_view(self, request, callback, callback_args, callback_kwargs):
if settings.DEBUG and 'graph' in request.GET:
config = Config(
max_depth=getattr(settings, 'CALLGRAPH_MAXDEPTH', 100))
config.trace_filter = GlobbingFilter(
include=getattr(settings, 'CALLGRAPH_INCLUDES', [])
)
graphviz = GraphvizOutput(
output_file='callgraph-' + str(time.time()) + '.svg',
output_type='svg'
)
pycallgraph = PyCallGraph(
output=graphviz,
config=config
)
pycallgraph.start()
self.pycallgraph = pycallgraph
def process_response(self, request, response):
if settings.DEBUG and 'graph' in request.GET:
self.pycallgraph.done()
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment