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

Commit

Permalink
Fix statsd initialization on storage
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Feb 3, 2016
1 parent 3189f68 commit e7bbff4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cliquet/statsd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import
import types

try:
import statsd as statsd_module
Expand All @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion cliquet/tests/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)

0 comments on commit e7bbff4

Please sign in to comment.