Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Merge pull request #639 from mozilla-services/track-statsd-by-policy
Browse files Browse the repository at this point in the history
StatsD execution timer by sub-policy
  • Loading branch information
Natim committed Feb 9, 2016
2 parents 1f63dbb + 112e1c4 commit c771bbd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
11 changes: 9 additions & 2 deletions cliquet/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -280,7 +281,13 @@ 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):
for name, subpolicy in policy.get_policies():
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
Expand Down
4 changes: 2 additions & 2 deletions cliquet/statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions cliquet/tests/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit c771bbd

Please sign in to comment.