Skip to content

Commit

Permalink
Add filtering to capture only requests with some text in the url
Browse files Browse the repository at this point in the history
  • Loading branch information
jredrejo committed Jun 13, 2018
1 parent 6cb4a5d commit 004a66c
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions pyinstrument/middleware.py
Expand Up @@ -9,38 +9,41 @@
except:
import object as MiddlewareMixin


class ProfilerMiddleware(MiddlewareMixin):
def process_request(self, request):
profile_dir = getattr(settings, 'PYINSTRUMENT_PROFILE_DIR', None)
self.filter = getattr(settings, 'PYINSTRUMENT_FILTER', None)
if self.filter:
if self.filter not in request.get_full_path():
return
self.profile_dir = getattr(settings, 'PYINSTRUMENT_PROFILE_DIR', None)

if getattr(settings, 'PYINSTRUMENT_URL_ARGUMENT', 'profile') in request.GET or profile_dir:
if getattr(settings, 'PYINSTRUMENT_URL_ARGUMENT', 'profile') in request.GET or self.profile_dir:
profiler = Profiler()
profiler.start()

request.profiler = profiler


def process_response(self, request, response):
if hasattr(request, 'profiler'):
if self.filter:
if self.filter not in request.get_full_path():
return response
if hasattr(request, 'profiler') or self.profile_dir:
try:
request.profiler.stop()

output_html = request.profiler.output_html()

profile_dir = getattr(settings, 'PYINSTRUMENT_PROFILE_DIR', None)

if profile_dir:
if self.profile_dir:
filename = '{total_time:.3f}s {path} {timestamp:.0f}.html'.format(
total_time=request.profiler.root_frame().time(),
path=request.get_full_path().replace('/', '_'),
timestamp=time.time()
)

file_path = os.path.join(profile_dir, filename)
file_path = os.path.join(self.profile_dir, filename)

if not os.path.exists(profile_dir):
os.mkdir(profile_dir)
if not os.path.exists(self.profile_dir):
os.mkdir(self.profile_dir)

with open(file_path, 'w') as f:
f.write(output_html)
Expand Down

0 comments on commit 004a66c

Please sign in to comment.