fix: respect PYINSTRUMENT_SHOW_CALLBACK when PYINSTRUMENT_PROFILE_DIR is set#452
Open
Sanjays2402 wants to merge 1 commit into
Open
fix: respect PYINSTRUMENT_SHOW_CALLBACK when PYINSTRUMENT_PROFILE_DIR is set#452Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
… is set The Django middleware's condition was ((show_callback and url_argument) or profile_dir), so setting PYINSTRUMENT_PROFILE_DIR profiled every request and ignored PYINSTRUMENT_SHOW_CALLBACK entirely. The callback now gates both the URL-argument and profile-directory paths. Adds a regression test covering both the rejecting and allowing callback.
joerick
approved these changes
Jul 25, 2026
joerick
left a comment
Owner
There was a problem hiding this comment.
Looks good. Thanks.
I really like how the test is written. If you had time to make a few more tests like this for Django and check it was all run in CI that would be great!
There was a problem hiding this comment.
Pull request overview
This PR fixes the Django ProfilerMiddleware gating logic so PYINSTRUMENT_SHOW_CALLBACK is respected even when PYINSTRUMENT_PROFILE_DIR is set, allowing users to selectively record requests to disk.
Changes:
- Update Django middleware condition so
PYINSTRUMENT_SHOW_CALLBACKgates both URL-argument profiling and profile-dir recording. - Add regression tests covering callback behavior when
PYINSTRUMENT_PROFILE_DIRis configured. - Document that
PYINSTRUMENT_SHOW_CALLBACKalso applies to profile-dir output.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
pyinstrument/middleware.py |
Fix conditional so the show callback controls profiling even with PYINSTRUMENT_PROFILE_DIR. |
test/test_django_middleware.py |
Add regression tests to ensure callback is respected with profile-dir profiling enabled. |
docs/guide.md |
Clarify documentation about callback behavior when profile-dir output is enabled. |
Comments suppressed due to low confidence (1)
test/test_django_middleware.py:48
- Same portability/flake issue here: using a shared hard-coded
/tmp/...path can break on non-POSIX platforms and when tests run concurrently. Prefertmp_pathto get an isolated directory for this test.
def test_profile_dir_still_profiles_when_callback_allows():
from django.test import override_settings
with override_settings(
PYINSTRUMENT_PROFILE_DIR="/tmp/pyinstrument-test-profiles",
PYINSTRUMENT_SHOW_CALLBACK=lambda request: True,
):
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+27
to
+33
| def test_show_callback_is_respected_with_profile_dir(): | ||
| from django.test import override_settings | ||
|
|
||
| with override_settings( | ||
| PYINSTRUMENT_PROFILE_DIR="/tmp/pyinstrument-test-profiles", | ||
| PYINSTRUMENT_SHOW_CALLBACK=lambda request: False, | ||
| ): |
Owner
|
+1 to the copilot comment and once the formatter is happy we can get it in. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #342
The Django middleware's condition was
(show_pyinstrument(request) and url_argument in request.GET) or profile_dir, so as soon asPYINSTRUMENT_PROFILE_DIRwas set every request was profiled andPYINSTRUMENT_SHOW_CALLBACKwas ignored. The callback now gates both branches, so it can be used to select which requests get written to the profile directory. Regression test fails on main (assert not hasattr(request, "profiler")) and passes with the fix.