Skip to content

Instantly share code, notes, and snippets.

@luzfcb
Forked from hirokiky/callpraphmiddleware.py
Created March 1, 2021 21:48
Show Gist options
  • Save luzfcb/894fe086c59015baa88c08479e9359fd to your computer and use it in GitHub Desktop.
Save luzfcb/894fe086c59015baa88c08479e9359fd to your computer and use it in GitHub Desktop.
Django Middleware to show call graph.
from pycallgraph import PyCallGraph
from pycallgraph import Config
from pycallgraph import GlobbingFilter
from pycallgraph.output import GraphvizOutput
class ProfilerMiddleware(object):
includes = []
def can(self, request):
return settings.DEBUG and 'prof' in request.GET
def process_request(self, request, *args, **kwargs):
if self.can(request):
config = Config()
config.trace_filter = GlobbingFilter(include=self.includes)
graphviz = GraphvizOutput(output_file='callgraph.png')
self.profiler = PyCallGraph(output=graphviz, config=config)
self.profiler.start()
def process_response(self, request, response):
if self.can(request):
self.profiler.done()
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment