From 039448f2f56bb965a6fb8b3cc6cc2ad4c29fba7d Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Tue, 2 Feb 2016 22:23:01 +0100 Subject: [PATCH 1/2] Track StatsD by authentication policy --- cliquet/initialization.py | 14 ++++++++++++-- cliquet/statsd.py | 4 ++-- cliquet/tests/test_statsd.py | 8 ++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cliquet/initialization.py b/cliquet/initialization.py index f8afd9a4..83537a2d 100644 --- a/cliquet/initialization.py +++ b/cliquet/initialization.py @@ -33,7 +33,8 @@ from pyramid.security import NO_PERMISSION_REQUIRED from pyramid.interfaces import IAuthenticationPolicy from pyramid.settings import asbool, aslist -from pyramid_multiauth import MultiAuthPolicySelected +from pyramid_multiauth import (MultiAuthenticationPolicy, + MultiAuthPolicySelected) def setup_request_bound_data(config): @@ -280,7 +281,16 @@ def setup_statsd(config): # Commit so that configured policy can be queried. config.commit() policy = config.registry.queryUtility(IAuthenticationPolicy) - client.watch_execution_time(policy, prefix='authentication') + if isinstance(policy, MultiAuthenticationPolicy): + # XXX: add method in pyramid_multiauth + # https://github.com/mozilla-services/pyramid_multiauth/pull/13 + for subpolicy in policy._policies: + name = getattr(subpolicy, "_pyramid_multiauth_name", None) + client.watch_execution_time(subpolicy, + prefix='authentication', + classname=name) + else: + client.watch_execution_time(policy, prefix='authentication') def on_new_response(event): request = event.request diff --git a/cliquet/statsd.py b/cliquet/statsd.py index 7ee3ec15..96d9a81c 100644 --- a/cliquet/statsd.py +++ b/cliquet/statsd.py @@ -15,8 +15,8 @@ class Client(object): def __init__(self, host, port, prefix): self._client = statsd_module.StatsClient(host, port, prefix=prefix) - def watch_execution_time(self, obj, prefix=''): - classname = utils.classname(obj) + def watch_execution_time(self, obj, prefix='', classname=None): + classname = classname or utils.classname(obj) members = dir(obj) for name in members: value = getattr(obj, name) diff --git a/cliquet/tests/test_statsd.py b/cliquet/tests/test_statsd.py index f68c0f02..28da6434 100644 --- a/cliquet/tests/test_statsd.py +++ b/cliquet/tests/test_statsd.py @@ -97,3 +97,11 @@ def test_statds_tracks_listeners_execution_duration(self): with mock.patch.object(statsd_client, 'timing') as mocked: self.app.get('/', headers=self.headers) self.assertTrue(mocked.called) + + def test_statds_tracks_authentication_policies(self): + statsd_client = self.app.app.registry.statsd._client + with mock.patch.object(statsd_client, 'timing') as mocked: + self.app.get('/', headers=self.headers) + timers = set(c[0][0] for c in mocked.call_args_list) + self.assertIn('authentication.basicauth.unauthenticated_userid', + timers) From 112e1c4a57cd60f5f374d95717c71a675ce3bf54 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Tue, 9 Feb 2016 11:40:05 +0100 Subject: [PATCH 2/2] Take advantage of new pyramid_multiauth --- cliquet/initialization.py | 5 +---- setup.py | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/cliquet/initialization.py b/cliquet/initialization.py index 83537a2d..658cb266 100644 --- a/cliquet/initialization.py +++ b/cliquet/initialization.py @@ -282,10 +282,7 @@ def setup_statsd(config): config.commit() policy = config.registry.queryUtility(IAuthenticationPolicy) if isinstance(policy, MultiAuthenticationPolicy): - # XXX: add method in pyramid_multiauth - # https://github.com/mozilla-services/pyramid_multiauth/pull/13 - for subpolicy in policy._policies: - name = getattr(subpolicy, "_pyramid_multiauth_name", None) + for name, subpolicy in policy.get_policies(): client.watch_execution_time(subpolicy, prefix='authentication', classname=name) diff --git a/setup.py b/setup.py index 2ec99572..89e529df 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ 'colander', 'cornice >= 1.1', # Fix cache CORS 'python-dateutil', - 'pyramid_multiauth >= 0.6', # Policy name in event. + 'pyramid_multiauth >= 0.7', # Contained policy names. 'pyramid_tm', 'redis', # Default backend 'requests',