Repository: scoutapp/scout_apm_python
Pull request #89 (2018-07-25 16:32:52)

Do not collect backtraces in Controller or Middleware
Resolves https://github.com/scoutapp/scout_apm_python/issues/88


Patch:
diff --git a/src/scout_apm/core/tracked_request.py b/src/scout_apm/core/tracked_request.py
index c1c9eca..4e25d09 100644
--- a/src/scout_apm/core/tracked_request.py
+++ b/src/scout_apm/core/tracked_request.py
@@ -42,23 +42,21 @@ class TrackedRequest(ThreadLocalSingleton):
             logger.debug('Overwriting previously set tag for request %s: %s' % (self.req_id, key))
         self.tags[key] = value
 
-    def start_span(self, operation=None, ignore=False, ignore_children=False):
+    def start_span(self, *args, **kwargs):
         maybe_parent = self.current_span()
 
         if maybe_parent is not None:
             parent_id = maybe_parent.span_id
             if maybe_parent.ignore_children:
-                ignore = True
-                ignore_children = True
+                kwargs['ignore'] = True
+                kwargs['ignore_children'] = True
         else:
             parent_id = None
 
-        new_span = Span(
-            request_id=self.req_id,
-            operation=operation,
-            parent=parent_id,
-            ignore=ignore,
-            ignore_children=ignore_children)
+        kwargs['parent'] = parent_id
+        kwargs['request_id'] = self.req_id
+
+        new_span = Span(**kwargs)
         self.active_spans.append(new_span)
         return new_span
 
@@ -134,6 +132,9 @@ class Span:
     # Add any interesting annotations to the span. Assumes that we are in the
     # process of stopping this span.
     def annotate(self):
+        if self.operation is not None:
+            if self.operation.startswith('Controller') or self.operation.startswith('Middleware'):
+                return
         slow_threshold = 0.500
         if self.duration() > slow_threshold:
             self.capture_backtrace()


Test patch:
diff --git a/tests/scout_apm_tests/tracked_request_test.py b/tests/scout_apm_tests/tracked_request_test.py
index 98e7794..6d74102 100644
--- a/tests/scout_apm_tests/tracked_request_test.py
+++ b/tests/scout_apm_tests/tracked_request_test.py
@@ -1,3 +1,5 @@
+from datetime import datetime, timedelta
+
 from scout_apm.core.tracked_request import TrackedRequest
 
 
@@ -65,3 +67,23 @@ def test_start_span_ignores_children():
     tr.stop_span()
     assert(1 == len(tr.complete_spans))
     assert('parent' == tr.complete_spans[0].operation)
+
+
+def test_span_captures_backtrace():
+    tr = TrackedRequest()
+    span = tr.start_span(operation='Sql/Work',
+                         start_time=datetime.now() - timedelta(seconds=1))
+    tr.stop_span()
+    assert(span.tags['stack'])
+
+
+def test_span_does_not_capture_backtrace():
+    tr = TrackedRequest()
+    controller = tr.start_span(operation='Controller/Work',
+                         start_time=datetime.now() - timedelta(seconds=10))
+    middleware = tr.start_span(operation='Middleware/Work',
+                         start_time=datetime.now() - timedelta(seconds=10))
+    tr.stop_span()
+    tr.stop_span()
+    assert('stack' not in controller.tags)
+    assert('stack' not in middleware.tags)