Repository: scoutapp/scout_apm_python

Issue:
Report error requests on Pyramid
Currently if an error occurs in a Pyramid request, we don't report it. Error cases can be just as useful to see so we should try and do this. It looks like it's possible by rearranging the existing code.

Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c277c34..5ea5780 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,8 @@
   ([PR #283](https://github.com/scoutapp/scout_apm_python/pull/283)).
 - Improve warning message for deprecated `log_level` configuration option
   ([PR #288](https://github.com/scoutapp/scout_apm_python/pull/288)).
+- Track errors on Pyramid
+  ([PR #298](https://github.com/scoutapp/scout_apm_python/pull/298)).
 
 ## [2.6.0] 2019-10-22
 
diff --git a/src/scout_apm/pyramid.py b/src/scout_apm/pyramid.py
index 73ade4e..b4d7f97 100644
--- a/src/scout_apm/pyramid.py
+++ b/src/scout_apm/pyramid.py
@@ -77,17 +77,18 @@ def instruments(handler, registry):
                     track_amazon_request_queue_time(amazon_queue_time, tracked_request)
 
             try:
-                response = handler(request)
+                try:
+                    response = handler(request)
+                finally:
+                    # 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()
+                        span.operation = "Controller/" + request.matched_route.name
             except Exception:
                 tracked_request.tag("error", "true")
                 raise
 
-            # This happens further down the call chain. So time it starting
-            # above, but only name it if it gets to here.
-            if request.matched_route is not None:
-                tracked_request.mark_real_request()
-                span.operation = "Controller/" + request.matched_route.name
-
         finally:
             tracked_request.stop_span()
 


Test patch:
diff --git a/tests/integration/test_pyramid.py b/tests/integration/test_pyramid.py
index fb4df40..2606691 100644
--- a/tests/integration/test_pyramid.py
+++ b/tests/integration/test_pyramid.py
@@ -217,7 +217,13 @@ def test_server_error(tracked_requests):
     with app_with_scout() as app, pytest.raises(ValueError):
         TestApp(app).get("/crash/", expect_errors=True)
 
-    assert tracked_requests == []
+    assert len(tracked_requests) == 1
+    tracked_request = tracked_requests[0]
+    assert tracked_request.tags["path"] == "/crash/"
+    assert tracked_request.tags["error"] == "true"
+    assert len(tracked_request.complete_spans) == 1
+    span = tracked_request.complete_spans[0]
+    assert span.operation == "Controller/crash"
 
 
 def test_no_monitor(tracked_requests):