-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Option to configure renderers in Django settings #158
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d22c9bc
Add Option to configure renderers in Django settings
kracekumar ddd0acf
Run precommit hook on all files
kracekumar e1d59fb
Incorporate the review feedback
kracekumar dd5682c
Update pyinstrument/middleware.py
joerick 268f663
Remove exit condition and use html renderer when render is missing
kracekumar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
from django.utils.module_loading import import_string | ||
|
||
from pyinstrument import Profiler | ||
from pyinstrument.renderers import Renderer | ||
from pyinstrument.renderers.html import HTMLRenderer | ||
|
||
try: | ||
|
@@ -16,6 +17,25 @@ | |
MiddlewareMixin = object | ||
|
||
|
||
def get_renderer_and_extension(path): | ||
"""Return the renderer instance and output file extension.""" | ||
if path: | ||
try: | ||
renderer = import_string(path)() | ||
except ImportError as exc: | ||
print("Unable to import the class: %s" % path) | ||
raise exc | ||
|
||
else: | ||
renderer = HTMLRenderer() | ||
|
||
if isinstance(renderer, Renderer): | ||
return renderer, renderer.output_file_extension | ||
else: | ||
print("Renderer should subclass: %s. Using HTMLRenderer" % Renderer) | ||
return HTMLRenderer(), HTMLRenderer.output_file_extension | ||
|
||
|
||
class ProfilerMiddleware(MiddlewareMixin): # type: ignore | ||
def process_request(self, request): | ||
profile_dir = getattr(settings, "PYINSTRUMENT_PROFILE_DIR", None) | ||
|
@@ -41,8 +61,10 @@ def process_response(self, request, response): | |
if hasattr(request, "profiler"): | ||
profile_session = request.profiler.stop() | ||
|
||
renderer = HTMLRenderer() | ||
output_html = renderer.render(profile_session) | ||
configured_renderer = getattr(settings, "PYINSTRUMENT_PROFILE_DIR_RENDERER", None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeping this here since this renderer is used in the |
||
renderer, ext = get_renderer_and_extension(configured_renderer) | ||
|
||
output = renderer.render(profile_session) | ||
|
||
profile_dir = getattr(settings, "PYINSTRUMENT_PROFILE_DIR", None) | ||
|
||
|
@@ -55,10 +77,8 @@ def process_response(self, request, response): | |
path = path.replace("?", "_qs_") | ||
|
||
if profile_dir: | ||
filename = "{total_time:.3f}s {path} {timestamp:.0f}.html".format( | ||
total_time=profile_session.duration, | ||
path=path, | ||
timestamp=time.time(), | ||
filename = "{total_time:.3f}s {path} {timestamp:.0f}.{ext}".format( | ||
total_time=profile_session.duration, path=path, timestamp=time.time(), ext=ext | ||
) | ||
|
||
file_path = os.path.join(profile_dir, filename) | ||
|
@@ -67,10 +87,15 @@ def process_response(self, request, response): | |
os.mkdir(profile_dir) | ||
|
||
with open(file_path, "w", encoding="utf-8") as f: | ||
f.write(output_html) | ||
f.write(output) | ||
|
||
if getattr(settings, "PYINSTRUMENT_URL_ARGUMENT", "profile") in request.GET: | ||
return HttpResponse(output_html) | ||
if isinstance(renderer, HTMLRenderer): | ||
return HttpResponse(output) | ||
else: | ||
renderer = HTMLRenderer() | ||
output = renderer.render(profile_session) | ||
return HttpResponse(output) | ||
else: | ||
return response | ||
else: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,10 @@ class Renderer: | |
Dictionary containing processor options, passed to each processor. | ||
""" | ||
|
||
output_file_extension: str = "txt" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added the |
||
"""Renderer output file extension with out dot prefix. The default value is `txt` | ||
""" | ||
|
||
def __init__( | ||
self, | ||
show_all: bool = False, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pyright was pointing out this can return None. So when the renderer is not a subclass of Renderer, we will be using HTMLRenderer. Let me know if this is fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to raise an error, instead. I'll update it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made the update. For some reason I wasn't able to push to this branch. I've pushed it to 69b195b - if you could cherry-pick that here, I think we're good to go.