Skip to content

Commit c474ff9

Browse files
committed
fix: inject Django middleware automatically
Import appmap.django at startup, to make sure our middleware gets injected.
1 parent c501db5 commit c474ff9

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

appmap/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
"""AppMap recorder for Python"""
22
from ._implementation import generation # noqa: F401
33
from ._implementation.env import Env # noqa: F401
4-
from ._implementation.labels import labels # noqa: F401
54
from ._implementation.importer import instrument_module # noqa: F401
5+
from ._implementation.labels import labels # noqa: F401
66
from ._implementation.recording import Recording # noqa: F401
77

8+
try:
9+
from . import django # noqa: F401
10+
except ImportError:
11+
# not using django
12+
pass
13+
814
try:
915
from . import flask # noqa: F401
1016
except ImportError:

appmap/test/data/django/app/settings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@
1010
# Must set ROOT_URLCONF else we get
1111
# AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'
1212
ROOT_URLCONF = "app.urls"
13-
14-
MIDDLEWARE = ["appmap.django.Middleware"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import appmap

appmap/test/test_django.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ def test_framework_metadata(
6464

6565

6666
@pytest.mark.appmap_enabled
67-
def test_app_can_read_body(client, events, monkeypatch): # pylint: disable=unused-argument
67+
def test_app_can_read_body(
68+
client, events, monkeypatch
69+
): # pylint: disable=unused-argument
6870
monkeypatch.setenv("APPMAP_RECORD_REQUESTS", "false")
6971
response = client.post("/echo", json={"test": "json"})
7072
assert response.content == b'{"test": "json"}'
@@ -206,12 +208,15 @@ def test_middleware_reset(pytester, monkeypatch):
206208
class TestRecordRequestsDjango(TestRecordRequests):
207209
@staticmethod
208210
def server_start_thread(env_vars_str):
211+
# Use appmap from our working copy, not the module installed by virtualenv. Add the init
212+
# directory so the sitecustomize.py file it contains will be loaded on startup. This
213+
# simulates a real installation.
209214
exec_cmd(
210215
"""
211-
# use appmap from our working copy, not the module installed by virtualenv
212-
export PYTHONPATH=`pwd`
216+
export PYTHONPATH="$PWD"
213217
214218
cd appmap/test/data/django/
219+
PYTHONPATH="$PYTHONPATH:$PWD/init"
215220
"""
216221
+ env_vars_str
217222
+ """ APPMAP_OUTPUT_DIR=/tmp python manage.py runserver 127.0.0.1:"""

0 commit comments

Comments
 (0)