Skip to content

fix: respect PYINSTRUMENT_SHOW_CALLBACK when PYINSTRUMENT_PROFILE_DIR is set#452

Open
Sanjays2402 wants to merge 1 commit into
joerick:mainfrom
Sanjays2402:fix/show-callback-with-profile-dir
Open

fix: respect PYINSTRUMENT_SHOW_CALLBACK when PYINSTRUMENT_PROFILE_DIR is set#452
Sanjays2402 wants to merge 1 commit into
joerick:mainfrom
Sanjays2402:fix/show-callback-with-profile-dir

Conversation

@Sanjays2402

Copy link
Copy Markdown

Closes #342

The Django middleware's condition was (show_pyinstrument(request) and url_argument in request.GET) or profile_dir, so as soon as PYINSTRUMENT_PROFILE_DIR was set every request was profiled and PYINSTRUMENT_SHOW_CALLBACK was 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.

… 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 joerick left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_CALLBACK gates both URL-argument profiling and profile-dir recording.
  • Add regression tests covering callback behavior when PYINSTRUMENT_PROFILE_DIR is configured.
  • Document that PYINSTRUMENT_SHOW_CALLBACK also 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. Prefer tmp_path to 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,
):
@joerick

joerick commented Jul 25, 2026

Copy link
Copy Markdown
Owner

+1 to the copilot comment and once the formatter is happy we can get it in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PYINSTRUMENT_SHOW_CALLBACK should not be ignored if PYINSTRUMENT_PROFILE_DIR is defined

3 participants