Skip to content

Commit f15e591

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

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

appmap/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
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/flask.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ def request_params(req):
4444
return values_dict(params.lists())
4545

4646

47-
NP_REGEXP = re.compile(r"<Rule '(.*?)'")
48-
NP_XLATE = re.compile(r"(?P<l><)|(?P<r>>)")
47+
NP_PARAMS = re.compile(r"<Rule '(.*?)'")
48+
NP_PARAM_DELIMS = str.maketrans("<>", "{}")
4949

5050

5151
class AppmapFlask(AppmapMiddleware):
@@ -93,15 +93,12 @@ def before_request_main(self, rec, request):
9393
# Transform request.url to the expected normalized-path form. For example,
9494
# "/post/<username>/<post_id>/summary" becomes "/post/{username}/{post_id}/summary".
9595
# Notes:
96-
# * the value of `repr` of this rule begins with "<Repr '/post/<username>/<post_id>/summary'"
96+
# * the value of `repr` of this rule begins with "<Rule '/post/<username>/<post_id>/summary'"
9797
# * the variable names in a rule can only contain alphanumerics:
9898
# * flask 1: https://github.com/pallets/werkzeug/blob/1dde4b1790f9c46b7122bb8225e6b48a5b22a615/src/werkzeug/routing.py#L143
9999
# * flask 2: https://github.com/pallets/werkzeug/blob/99f328cf2721e913bd8a3128a9cdd95ca97c334c/src/werkzeug/routing/rules.py#L56
100100
r = repr(request.url_rule)
101-
np = NP_XLATE.sub(
102-
lambda m: "{" if m["l"] else "}" if m["r"] else "",
103-
NP_REGEXP.findall(r)[0],
104-
)
101+
np = NP_PARAMS.findall(r)[0].translate(NP_PARAM_DELIMS)
105102

106103
call_event = HttpServerRequestEvent(
107104
request_method=request.method,

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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,15 @@ def test_middleware_reset(pytester, monkeypatch):
208208
class TestRecordRequestsDjango(TestRecordRequests):
209209
@staticmethod
210210
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.
211214
exec_cmd(
212215
"""
213-
# use appmap from our working copy, not the module installed by virtualenv
214-
export PYTHONPATH=`pwd`
216+
export PYTHONPATH="$PWD"
215217
216218
cd appmap/test/data/django/
219+
PYTHONPATH="$PYTHONPATH:$PWD/init"
217220
"""
218221
+ env_vars_str
219222
+ """ APPMAP_OUTPUT_DIR=/tmp python manage.py runserver 127.0.0.1:"""

0 commit comments

Comments
 (0)