Repository: scoutapp/scout_apm_python
Pull request #236 (2019-08-26 09:04:13)

Fix Django monitoring when SCOUT_MONITOR changes at runtime


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d61d985..4337213 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,8 +6,14 @@
 
 - Improved logging for debugging customer problems
   ([PR #234](https://github.com/scoutapp/scout_apm_python/pull/234)).
+
+### Fixed
+
 - Fixed Flask to not monitor requests when `SCOUT_MONITOR` is set to `False`
   ([PR #235](https://github.com/scoutapp/scout_apm_python/pull/235)).
+- Fixed Django to stop monitoring requests when `SCOUT_MONITOR` is set to
+  `False` during runtime
+  ([PR #236](https://github.com/scoutapp/scout_apm_python/pull/236)).
 
 ## [2.4.0] 2019-08-20
 
diff --git a/src/scout_apm/django/middleware.py b/src/scout_apm/django/middleware.py
index cfc64da..bd0b45e 100644
--- a/src/scout_apm/django/middleware.py
+++ b/src/scout_apm/django/middleware.py
@@ -3,6 +3,7 @@ from __future__ import absolute_import, division, print_function, unicode_litera
 
 import logging
 
+from scout_apm.core.config import ScoutConfig
 from scout_apm.core.ignore import ignore_path
 from scout_apm.core.queue_time import track_request_queue_time
 from scout_apm.core.tracked_request import TrackedRequest
@@ -64,6 +65,9 @@ class MiddlewareTimingMiddleware(object):
         self.get_response = get_response
 
     def __call__(self, request):
+        if not ScoutConfig().value("monitor"):
+            return self.get_response(request)
+
         tracked_request = TrackedRequest.instance()
 
         tracked_request.start_span(operation="Middleware")
@@ -98,6 +102,9 @@ class ViewTimingMiddleware(object):
         be recorded.  This can happen if a middleware further along the stack
         doesn't call onward, and instead returns a response directly.
         """
+        if not ScoutConfig().value("monitor"):
+            return self.get_response(request)
+
         tracked_request = TrackedRequest.instance()
 
         # This operation name won't be recorded unless changed later in
@@ -113,6 +120,8 @@ class ViewTimingMiddleware(object):
         """
         Capture details about the view_func that is about to execute
         """
+        if not ScoutConfig().value("monitor"):
+            return
         tracked_request = TrackedRequest.instance()
         tracked_request.mark_real_request()
 
@@ -128,6 +137,8 @@ class ViewTimingMiddleware(object):
 
         Does not modify or catch or otherwise change the exception thrown
         """
+        if not ScoutConfig().value("monitor"):
+            return
         TrackedRequest.instance().tag("error", "true")
 
 
@@ -138,6 +149,8 @@ class OldStyleMiddlewareTimingMiddleware(object):
     """
 
     def process_request(self, request):
+        if not ScoutConfig().value("monitor"):
+            return
         tracked_request = TrackedRequest.instance()
         request._scout_tracked_request = tracked_request
 


Test patch:
diff --git a/tests/integration/test_django.py b/tests/integration/test_django.py
index 1074a4d..e426e1c 100644
--- a/tests/integration/test_django.py
+++ b/tests/integration/test_django.py
@@ -276,13 +276,20 @@ def test_admin_view_operation_name(url, expected_op_name, tracked_requests):
     assert span.operation == expected_op_name
 
 
-@pytest.mark.xfail(reason="Test setup doesn't reset state fully at the moment.")
 def test_no_monitor(tracked_requests):
     with app_with_scout(SCOUT_MONITOR=False) as app:
         response = TestApp(app).get("/hello/")
 
     assert response.status_int == 200
-    assert len(tracked_requests) == 0
+    assert tracked_requests == []
+
+
+def test_no_monitor_server_error(tracked_requests):
+    with app_with_scout(SCOUT_MONITOR=False) as app:
+        response = TestApp(app).get("/crash/", expect_errors=True)
+
+    assert response.status_int == 500
+    assert tracked_requests == []
 
 
 def fake_authentication_middleware(get_response):