Skip to content

Commit

Permalink
Merge pull request #339 from HazemBZ/update/middleware-custom-filename
Browse files Browse the repository at this point in the history
  • Loading branch information
joerick authored Nov 15, 2024
2 parents e362034 + b48c613 commit b7af150
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ profile something. In this case, add `PYINSTRUMENT_PROFILE_DIR = 'profiles'`
to your `settings.py`. Pyinstrument will profile every request and save the
HTML output to the folder `profiles` in your working directory.

You can further customize the filename by adding `PYINSTRUMENT_FILENAME` to
`settings.py`, default values is `"{total_time:.3f}s {path} {timestamp:.0f}.{ext}"`.

If you want to show the profiling page depending on the request you can define
`PYINSTRUMENT_SHOW_CALLBACK` as dotted path to a function used for determining
whether the page should show or not.
Expand Down
3 changes: 2 additions & 1 deletion pyinstrument/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def process_response(self, request, response):
path = path.replace("?", "_qs_")

if profile_dir:
filename = "{total_time:.3f}s {path} {timestamp:.0f}.{ext}".format(
default_filename = "{total_time:.3f}s {path} {timestamp:.0f}.{ext}"
filename = getattr(settings, "PYINSTRUMENT_FILENAME", default_filename).format(
total_time=profile_session.duration,
path=path,
timestamp=time.time(),
Expand Down

0 comments on commit b7af150

Please sign in to comment.