def test_metlog_timing(self): client_from_dict_config = self.check_metlog() # Need to load within the test in case metlog is not installed from metlog.config import client_from_dict_config METLOG_CONF = { 'logger': 'django-statsd', 'sender': { 'class': 'metlog.senders.DebugCaptureSender', }, } metlog = client_from_dict_config(METLOG_CONF) with mock.patch.object(settings, 'METLOG', metlog): with mock.patch.object(settings, 'STATSD_CLIENT', 'django_statsd.clients.moz_metlog'): client = get_client() eq_(len(client.metlog.sender.msgs), 0) client.timing('testing', 512, rate=2) eq_(len(client.metlog.sender.msgs), 1) msg = json.loads(client.metlog.sender.msgs[0]) eq_(msg['severity'], 6) eq_(msg['payload'], '512') eq_(msg['fields']['rate'], 2) eq_(msg['fields']['name'], 'testing') eq_(msg['type'], 'timer')
def test_toolbar_gauge(self): client = get_client() self.assertEqual(client.cache, {}) client.gauge('testing', 1) self.assertEqual(client.cache, {'testing|gauge': [[1, 1]]}) client.gauge('testing', 1, delta=True) self.assertEqual(client.cache, {'testing|gauge': [[1, 1], [1, 1]]})
def test_get_client(self): metlog = self._create_client() with self.settings(METLOG=metlog, STATSD_PREFIX='moz_metlog', STATSD_CLIENT='django_statsd.clients.moz_metlog'): client = get_client() eq_(client.__module__, 'django_statsd.clients.moz_metlog')
def process_exception(self, request, exception): if not isinstance(exception, Http404): statsd = get_client().pipeline() statsd.incr('response.500', count=6) if hasattr(request, 'user') and request.user.is_authenticated(): statsd.incr('response.auth.500', count=6) statsd.send()
def test_metlog_prefixes(self): metlog = self._create_client() with self.settings(METLOG=metlog, STATSD_PREFIX='some_prefix', STATSD_CLIENT='django_statsd.clients.moz_metlog'): client = get_client() eq_(len(client.metlog.sender.msgs), 0) client.timing('testing', 512, rate=2) client.incr('foo', 2) client.decr('bar', 5) eq_(len(client.metlog.sender.msgs), 3) msg = json.loads(client.metlog.sender.msgs[0]) eq_(msg['severity'], 6) eq_(msg['payload'], '512') eq_(msg['fields']['rate'], 2) eq_(msg['fields']['name'], 'some_prefix.testing') eq_(msg['type'], 'timer') msg = json.loads(client.metlog.sender.msgs[1]) eq_(msg['severity'], 6) eq_(msg['payload'], '2') eq_(msg['fields']['rate'], 1) eq_(msg['fields']['name'], 'some_prefix.foo') eq_(msg['type'], 'counter') msg = json.loads(client.metlog.sender.msgs[2]) eq_(msg['severity'], 6) eq_(msg['payload'], '-5') eq_(msg['fields']['rate'], 1) eq_(msg['fields']['name'], 'some_prefix.bar') eq_(msg['type'], 'counter')
def test_metlog_no_prefixes(self): metlog = self._create_client() with self.settings(METLOG=metlog, STATSD_CLIENT='django_statsd.clients.moz_metlog'): client = get_client() client.incr('foo', 2)
def process_response(self, request, response): statsd = get_client().pipeline() statsd.incr('response.%s' % response.status_code, count=6) if hasattr(request, 'user') and request.user.is_authenticated(): statsd.incr('response.auth.%s' % response.status_code, count=6) statsd.send() return response
def test_log_client(self, l): client = get_client() client.timing('testing.timing', 1) client.incr('testing.incr') client.decr('testing.decr') client.gauge('testing.gauge', 1) l.check(('statsd', 'INFO', 'Timing: testing.timing, 1, 1'), ('statsd', 'INFO', 'Increment: testing.incr, 1, 1'), ('statsd', 'INFO', 'Decrement: testing.decr, 1, 1'), ('statsd', 'INFO', 'Gauge: testing.gauge, 1, 1'))
def configure_settings(): if not settings.configured: settings.configure(**{ 'DATABASES': {'default': {}}, # Use the toolbar for tests because it handly caches results for us. 'STATSD_CLIENT': 'django_statsd.clients.toolbar', 'STATSD_PREFIX': None, }) from django_statsd.clients import get_client lib.statsd = get_client()
def test_datadog_send(self): client = get_client() client.socket = FakeSocket() eq_(list(client.socket.payloads), []) client.incr('testing') eq_(client.socket.recv(), 'testing:1|c') client.decr('testing') eq_(client.socket.recv(), 'testing:-1|c') client.timing('testing', 8) eq_(client.socket.recv(), 'testing:8|ms')
def test_metlog_timing(self): metlog = self._create_client() with self.settings(METLOG=metlog, STATSD_PREFIX='moz_metlog', STATSD_CLIENT='django_statsd.clients.moz_metlog'): client = get_client() eq_(len(client.metlog.sender.msgs), 0) client.timing('testing', 512, rate=2) eq_(len(client.metlog.sender.msgs), 1) msg = json.loads(client.metlog.sender.msgs[0]) eq_(msg['severity'], 6) eq_(msg['payload'], '512') eq_(msg['fields']['rate'], 2) eq_(msg['fields']['name'], 'moz_metlog.testing') eq_(msg['type'], 'timer')
def test_get_client(self): client_from_dict_config = self.check_metlog() METLOG_CONF = { 'logger': 'django-statsd', 'sender': { 'class': 'metlog.senders.DebugCaptureSender', }, } metlog = client_from_dict_config(METLOG_CONF) with mock.patch.object(settings, 'METLOG', metlog): with mock.patch.object(settings, 'STATSD_CLIENT', 'django_statsd.clients.moz_metlog'): client = get_client() eq_(client.__module__, 'django_statsd.clients.moz_metlog')
def _record_time(self, request): if hasattr(request, '_start_time'): statsd = get_client().pipeline() ms = int((time.time() - request._start_time) * 1000) data = dict(module=request._view_module, name=request._view_name, method=request.method) statsd.timing('view.{module}.{name}.{method}'.format(**data), ms) statsd.timing('view.{module}.{method}'.format(**data), ms) statsd.timing('view.{method}'.format(**data), ms) # Track requests above certain threshholds for threshhold in range(5, 16, 5): if ms > threshhold * 1000: statsd.incr('view.{module}.{name}.{method}.over_{time}' .format(time=threshhold, **data), count=6) else: # Won't be larger than already larger numbers break statsd.send()
def test_normal(self): eq_(get_client().__module__, 'statsd.client')
'CURLING_FORMAT_LISTS': True, # Use the toolbar for tests because it handly caches results for us. 'STATSD_CLIENT': 'django_statsd.clients.toolbar', 'STATSD_PREFIX': None, } if not settings.configured: settings.configure(**minimal) from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned import mock from nose.tools import eq_, ok_, raises from django_statsd.clients import get_client import lib lib.statsd = get_client() from requests.exceptions import ConnectionError # Some samples for the Mock. samples = { 'GET:/services/settings/APPEND_SLASH/': { 'key': 'APPEND_SLASH' }, 'GET:/services/settings/': { 'meta': {'limit': 20, 'total_count': 185}, 'objects': [ {'key': 'ABSOLUTE_URL_OVERRIDES'}, {'key': 'ADMINS'}, ] },
def __init__(self, **kwargs): super(TimedConnection, self).__init__(**kwargs) self.statsd_client = get_client()
def test_toolbar_send(self): client = get_client() eq_(client.cache, {}) client.incr('testing') eq_(client.cache, {'testing|count': [[1, 1]]})
def test_toolbar_send(self): client = get_client() eq_(client.cache, {}) client.incr("testing") eq_(client.cache, {"testing|count": [[1, 1]]})
def test_no_metlog(self): with self.settings(STATSD_PREFIX='moz_metlog', STATSD_CLIENT='django_statsd.clients.moz_metlog'): get_client()
def test_null(self): eq_(get_client().__module__, 'django_statsd.clients.null')
def test_toolbar(self): eq_(get_client().__module__, 'django_statsd.clients.toolbar')
def test_toolbar_decr(self): client = get_client() self.assertEqual(client.cache, {}) client.decr('testing') self.assertEqual(client.cache, {'testing|count': [[-1, 1]]})
def test_toolbar_timing(self): client = get_client() self.assertEqual(client.timings, []) client.timing('testing', 1) self.assertEqual(client.timings[0][0], 'testing|timing') self.assertEqual(client.timings[0][2], 1)
def test_toolbar(self): self.assertEqual(get_client().__module__, 'django_statsd.clients.toolbar')
def test_no_metlog(self): with mock.patch.object(settings, 'STATSD_CLIENT', 'django_statsd.clients.moz_metlog'): get_client()
def test_null(self): self.assertEqual(get_client().__module__, 'django_statsd.clients.null')
def test_toolbar_set(self): client = get_client() self.assertEqual(client.cache, {}) client.set('testing', 1) self.assertEqual(client.cache, {'testing|set': [[1, 1]]})
def test_normal(self): self.assertEqual(get_client().__module__, 'statsd.client')
'CURLING_FORMAT_LISTS': True, # Use the toolbar for tests because it handly caches results for us. 'STATSD_CLIENT': 'django_statsd.clients.toolbar', 'STATSD_PREFIX': None, } if not settings.configured: settings.configure(**minimal) from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned import mock from nose.tools import eq_, ok_, raises from django_statsd.clients import get_client import lib lib.statsd = get_client() from requests.exceptions import ConnectionError # Some samples for the Mock. samples = { 'GET:/services/settings/APPEND_SLASH/': { 'key': 'APPEND_SLASH' }, 'GET:/services/settings/': { 'meta': { 'limit': 20, 'total_count': 185 }, 'objects': [ {