Repository: Kinto/kinto

Issue:
request.matched_route.pattern has no attribute pattern when matched_route is None
This error occurred when deploying Kinto 4.3 in stage.

```
AttributeError: 'NoneType' object has no attribute 'pattern'
(2 additional frame(s) were not displayed)
...
  File "pyramid/registry.py", line 91, in notify
    [ _ for _ in self.subscribers(events, None) ]
  File "/data/kinto-dist/lib/python2.7/site-packages/zope/interface/registry.py", line 442, in subscribers
    return self.adapters.subscribers(objects, provided)
  File "/data/kinto-dist/lib/python2.7/site-packages/zope/interface/adapter.py", line 596, in subscribers
    subscription(*objects)
  File "pyramid/config/adapters.py", line 103, in derived_subscriber
    return subscriber(arg[0])
  File "kinto/core/initialization.py", line 275, in on_new_response
    pattern = request.matched_route.pattern
```

request.matched_route.pattern has no attribute pattern when matched_route is None
This error occurred when deploying Kinto 4.3 in stage.

```
AttributeError: 'NoneType' object has no attribute 'pattern'
(2 additional frame(s) were not displayed)
...
  File "pyramid/registry.py", line 91, in notify
    [ _ for _ in self.subscribers(events, None) ]
  File "/data/kinto-dist/lib/python2.7/site-packages/zope/interface/registry.py", line 442, in subscribers
    return self.adapters.subscribers(objects, provided)
  File "/data/kinto-dist/lib/python2.7/site-packages/zope/interface/adapter.py", line 596, in subscribers
    subscription(*objects)
  File "pyramid/config/adapters.py", line 103, in derived_subscriber
    return subscriber(arg[0])
  File "kinto/core/initialization.py", line 275, in on_new_response
    pattern = request.matched_route.pattern
```



Discussion:
This code is very old, but statsd was never enabled in prod, which explains why we haven't seen it before. Matched route is None when no service matches the URL I guess...

Yes.

This code is very old, but statsd was never enabled in prod, which explains why we haven't seen it before. Matched route is None when no service matches the URL I guess...

Yes.


Resolving patch:
diff --git a/kinto/core/initialization.py b/kinto/core/initialization.py
--- a/kinto/core/initialization.py
+++ b/kinto/core/initialization.py
@@ -272,9 +272,7 @@ def on_new_response(event):
                 client.count('authn_type.%s' % request.authn_type)
 
             # Count view calls.
-            pattern = request.matched_route.pattern
-            services = request.registry.cornice_services
-            service = services.get(pattern)
+            service = request.current_service
             if service:
                 client.count('view.%s.%s' % (service.name, request.method))
 


Test patch:
diff --git a/tests/core/test_initialization.py b/tests/core/test_initialization.py
--- a/tests/core/test_initialization.py
+++ b/tests/core/test_initialization.py
@@ -328,6 +328,12 @@ def test_statsd_counts_views_and_methods(self):
         app.get('/v0/__heartbeat__')
         self.mocked().count.assert_any_call('view.heartbeat.GET')
 
+    def test_statsd_counts_unknown_urls(self):
+        kinto.core.initialize(self.config, '0.0.1', 'project_name')
+        app = webtest.TestApp(self.config.make_wsgi_app())
+        app.get('/v0/coucou', status=404)
+        self.assertFalse(self.mocked.count.called)
+
     @mock.patch('kinto.core.utils.hmac_digest')
     def test_statsd_counts_unique_users(self, digest_mocked):
         digest_mocked.return_value = u'mat'