From e7bbff4edbb356c9432358cffa5c4d8d40edffae Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Tue, 2 Feb 2016 17:43:29 +0100 Subject: [PATCH] Fix statsd initialization on storage --- cliquet/statsd.py | 4 +++- cliquet/tests/test_statsd.py | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cliquet/statsd.py b/cliquet/statsd.py index 2ff2064e..7ee3ec15 100644 --- a/cliquet/statsd.py +++ b/cliquet/statsd.py @@ -1,4 +1,5 @@ from __future__ import absolute_import +import types try: import statsd as statsd_module @@ -19,7 +20,8 @@ def watch_execution_time(self, obj, prefix=''): members = dir(obj) for name in members: value = getattr(obj, name) - if not name.startswith('_') and hasattr(value, '__call__'): + is_method = isinstance(value, types.MethodType) + if not name.startswith('_') and is_method: statsd_key = "%s.%s.%s" % (prefix, classname, name) decorated_method = self.timer(statsd_key)(value) setattr(obj, name, decorated_method) diff --git a/cliquet/tests/test_statsd.py b/cliquet/tests/test_statsd.py index bdf85beb..f68c0f02 100644 --- a/cliquet/tests/test_statsd.py +++ b/cliquet/tests/test_statsd.py @@ -2,7 +2,7 @@ from pyramid import testing -from cliquet.tests.support import unittest +from cliquet.tests.support import unittest, BaseWebTest from cliquet import statsd @@ -80,3 +80,20 @@ def test_statsd_count_call_the_client_if_configured(self): request.registry.statsd = self.mocked_client statsd.statsd_count(request, 'toto') self.mocked_client.count.assert_called_with('toto') + + +@unittest.skipIf(not statsd.statsd_module, "statsd is not installed.") +class TimingTest(BaseWebTest, unittest.TestCase): + def get_app_settings(self, *args, **kwargs): + settings = super(TimingTest, self).get_app_settings(*args, **kwargs) + if not statsd.statsd_module: + return settings + + settings['statsd_url'] = 'udp://localhost:8125' + return settings + + def test_statds_tracks_listeners_execution_duration(self): + statsd_client = self.app.app.registry.statsd._client + with mock.patch.object(statsd_client, 'timing') as mocked: + self.app.get('/', headers=self.headers) + self.assertTrue(mocked.called)