fix: respect PYINSTRUMENT_SHOW_CALLBACK when PYINSTRUMENT_PROFILE_DIR is set - #452
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
left a comment
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.
| 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, | ||
| ): |
|
+1 to the copilot comment and once the formatter is happy we can get it in. |
|
Both done in a2e224d. Switched the two tests to pytest's The formatter complaint was black wanting the Note |
|
thanks! |
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.