Repository: scoutapp/scout_apm_python
Pull request #338 (2019-11-03 10:17:32)

Move is_real_request to attribute
Remove the unnecessary level of indirection from the methods. Generally in Python if we have a simple attribute, we don't need to add getters or setters.


Patch:
diff --git a/src/scout_apm/api/__init__.py b/src/scout_apm/api/__init__.py
index bd348b8..5294c1e 100644
--- a/src/scout_apm/api/__init__.py
+++ b/src/scout_apm/api/__init__.py
@@ -85,7 +85,7 @@ class Transaction(ContextDecorator):
         operation = text(kind) + "/" + text(name)
 
         tracked_request = TrackedRequest.instance()
-        tracked_request.mark_real_request()
+        tracked_request.is_real_request = True
         span = tracked_request.start_span(
             operation=operation, should_capture_backtrace=False
         )
diff --git a/src/scout_apm/bottle.py b/src/scout_apm/bottle.py
index e2a8b45..746f7bf 100644
--- a/src/scout_apm/bottle.py
+++ b/src/scout_apm/bottle.py
@@ -37,7 +37,7 @@ class ScoutPlugin(object):
 
         def wrapper(*args, **kwargs):
             tracked_request = TrackedRequest.instance()
-            tracked_request.mark_real_request()
+            tracked_request.is_real_request = True
 
             path = request.path
             # allitems() is an undocumented bottle internal
diff --git a/src/scout_apm/celery.py b/src/scout_apm/celery.py
index 8ffd2b7..e65aac1 100644
--- a/src/scout_apm/celery.py
+++ b/src/scout_apm/celery.py
@@ -17,7 +17,7 @@ def before_publish_callback(headers=None, properties=None, **kwargs):
 
 def prerun_callback(task=None, **kwargs):
     tracked_request = TrackedRequest.instance()
-    tracked_request.mark_real_request()
+    tracked_request.is_real_request = True
 
     start = getattr(task.request, "scout_task_start", None)
     if start is not None:
diff --git a/src/scout_apm/core/tracked_request.py b/src/scout_apm/core/tracked_request.py
index f9caea7..c0f7250 100644
--- a/src/scout_apm/core/tracked_request.py
+++ b/src/scout_apm/core/tracked_request.py
@@ -30,7 +30,7 @@ class TrackedRequest(ThreadLocalSingleton):
         "active_spans",
         "complete_spans",
         "tags",
-        "_is_real_request",
+        "is_real_request",
         "_memory_start",
         "callset",
     )
@@ -42,7 +42,7 @@ class TrackedRequest(ThreadLocalSingleton):
         self.active_spans = []
         self.complete_spans = []
         self.tags = {}
-        self._is_real_request = False
+        self.is_real_request = False
         self._memory_start = get_rss_in_mb()
         self.callset = NPlusOneCallSet()
         logger.debug("Starting request: %s", self.request_id)
@@ -53,12 +53,6 @@ class TrackedRequest(ThreadLocalSingleton):
             repr(self.request_id), repr(self.tags)
         )
 
-    def mark_real_request(self):
-        self._is_real_request = True
-
-    def is_real_request(self):
-        return self._is_real_request
-
     def tag(self, key, value):
         if key in self.tags:
             logger.debug(
@@ -111,7 +105,7 @@ class TrackedRequest(ThreadLocalSingleton):
         logger.debug("Stopping request: %s", self.request_id)
         if self.end_time is None:
             self.end_time = dt.datetime.utcnow()
-        if self.is_real_request():
+        if self.is_real_request:
             self.tag("mem_delta", self._get_mem_delta())
             if not self.is_ignored():
                 batch_command = BatchCommand.from_tracked_request(self)
diff --git a/src/scout_apm/django/middleware.py b/src/scout_apm/django/middleware.py
index 3e4cca9..d5e205e 100644
--- a/src/scout_apm/django/middleware.py
+++ b/src/scout_apm/django/middleware.py
@@ -190,7 +190,7 @@ class ViewTimingMiddleware(object):
         if not scout_config.value("monitor"):
             return
         tracked_request = TrackedRequest.instance()
-        tracked_request.mark_real_request()
+        tracked_request.is_real_request = True
 
         track_request_view_data(request, tracked_request)
 
@@ -252,7 +252,7 @@ class OldStyleViewMiddleware(object):
             # don't do anything
             return
 
-        tracked_request.mark_real_request()
+        tracked_request.is_real_request = True
 
         track_request_view_data(request, tracked_request)
 
diff --git a/src/scout_apm/dramatiq.py b/src/scout_apm/dramatiq.py
index 05071ca..824764b 100644
--- a/src/scout_apm/dramatiq.py
+++ b/src/scout_apm/dramatiq.py
@@ -24,7 +24,7 @@ class ScoutMiddleware(dramatiq.Middleware):
         if self._do_nothing:
             return
         tracked_request = TrackedRequest.instance()
-        tracked_request.mark_real_request()
+        tracked_request.is_real_request = True
         if exception:
             tracked_request.tag("error", "true")
         tracked_request.stop_span()
diff --git a/src/scout_apm/falcon.py b/src/scout_apm/falcon.py
index 687ac6e..4340906 100644
--- a/src/scout_apm/falcon.py
+++ b/src/scout_apm/falcon.py
@@ -36,7 +36,7 @@ class ScoutMiddleware(object):
 
     def process_request(self, req, resp):
         tracked_request = TrackedRequest.instance()
-        tracked_request.mark_real_request()
+        tracked_request.is_real_request = True
         req.context.scout_tracked_request = tracked_request
 
         path = req.path
diff --git a/src/scout_apm/flask/__init__.py b/src/scout_apm/flask/__init__.py
index ef99370..6c185c6 100644
--- a/src/scout_apm/flask/__init__.py
+++ b/src/scout_apm/flask/__init__.py
@@ -61,7 +61,7 @@ class ScoutApm(object):
         operation = "Controller/" + name
 
         tracked_request = TrackedRequest.instance()
-        tracked_request.mark_real_request()
+        tracked_request.is_real_request = True
         span = tracked_request.start_span(
             operation=operation, should_capture_backtrace=False
         )
diff --git a/src/scout_apm/nameko.py b/src/scout_apm/nameko.py
index 484eeaf..6363968 100644
--- a/src/scout_apm/nameko.py
+++ b/src/scout_apm/nameko.py
@@ -19,7 +19,7 @@ class ScoutReporter(DependencyProvider):
         if self._do_nothing:
             return
         tracked_request = TrackedRequest.instance()
-        tracked_request.mark_real_request()
+        tracked_request.is_real_request = True
 
         # Get HTTP details for HTTP handlers
         if isinstance(worker_ctx.entrypoint, HttpRequestHandler):
diff --git a/src/scout_apm/pyramid.py b/src/scout_apm/pyramid.py
index f211b6e..ad765bb 100644
--- a/src/scout_apm/pyramid.py
+++ b/src/scout_apm/pyramid.py
@@ -69,7 +69,7 @@ def instruments(handler, registry):
                     # Routing further down the call chain. So time it starting
                     # above, but only name it if it gets a name
                     if request.matched_route is not None:
-                        tracked_request.mark_real_request()
+                        tracked_request.is_real_request = True
                         span.operation = "Controller/" + request.matched_route.name
             except Exception:
                 tracked_request.tag("error", "true")


Test patch:
diff --git a/tests/conftest.py b/tests/conftest.py
index 4632f06..2a5e592 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -134,7 +134,7 @@ def tracked_requests():
 
     @wrapt.decorator
     def capture_requests(wrapped, instance, args, kwargs):
-        if instance.is_real_request() and not instance.is_ignored():
+        if instance.is_real_request and not instance.is_ignored():
             requests.append(instance)
         return wrapped(*args, **kwargs)
 
diff --git a/tests/unit/core/test_tracked_request.py b/tests/unit/core/test_tracked_request.py
index 74bc3c6..c1b5b97 100644
--- a/tests/unit/core/test_tracked_request.py
+++ b/tests/unit/core/test_tracked_request.py
@@ -23,12 +23,7 @@ def test_tracked_request_instance_is_a_singleton():
 
 
 def test_is_real_request_default_false(tracked_request):
-    assert not tracked_request.is_real_request()
-
-
-def test_is_real_request_marked(tracked_request):
-    tracked_request.mark_real_request()
-    assert tracked_request.is_real_request()
+    assert not tracked_request.is_real_request
 
 
 def test_tag_request(tracked_request):
@@ -161,7 +156,7 @@ def test_finish_does_not_capture_memory(tracked_request):
 
 
 def test_finish_does_captures_memory_on_real_requests(tracked_request):
-    tracked_request.mark_real_request()
+    tracked_request.is_real_request = True
     tracked_request.finish()
 
     assert "mem_delta" in tracked_request.tags