Esempio n. 1
0
    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')
Esempio n. 2
0
 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]]})
Esempio n. 3
0
 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')
Esempio n. 4
0
 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()
Esempio n. 5
0
    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')
Esempio n. 6
0
    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)
Esempio n. 7
0
 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')
Esempio n. 8
0
 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
Esempio n. 9
0
    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)
Esempio n. 10
0
    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')
Esempio n. 11
0
    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')
Esempio n. 12
0
 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'))
Esempio n. 13
0
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()
Esempio n. 14
0
    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')
Esempio n. 15
0
    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')
Esempio n. 16
0
    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')
Esempio n. 17
0
    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')
Esempio n. 18
0
    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')
Esempio n. 19
0
    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()
Esempio n. 20
0
 def test_normal(self):
     eq_(get_client().__module__, 'statsd.client')
Esempio n. 21
0
    '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'},
        ]
    },
Esempio n. 22
0
 def __init__(self, **kwargs):
     super(TimedConnection, self).__init__(**kwargs)
     self.statsd_client = get_client()
Esempio n. 23
0
 def test_toolbar_send(self):
     client = get_client()
     eq_(client.cache, {})
     client.incr('testing')
     eq_(client.cache, {'testing|count': [[1, 1]]})
Esempio n. 24
0
 def test_toolbar_send(self):
     client = get_client()
     eq_(client.cache, {})
     client.incr("testing")
     eq_(client.cache, {"testing|count": [[1, 1]]})
Esempio n. 25
0
 def test_toolbar_send(self):
     client = get_client()
     eq_(client.cache, {})
     client.incr('testing')
     eq_(client.cache, {'testing|count': [[1, 1]]})
Esempio n. 26
0
 def test_no_metlog(self):
     with self.settings(STATSD_PREFIX='moz_metlog',
                        STATSD_CLIENT='django_statsd.clients.moz_metlog'):
         get_client()
Esempio n. 27
0
 def test_null(self):
     eq_(get_client().__module__, 'django_statsd.clients.null')
Esempio n. 28
0
 def test_toolbar(self):
     eq_(get_client().__module__, 'django_statsd.clients.toolbar')
Esempio n. 29
0
 def test_normal(self):
     eq_(get_client().__module__, 'statsd.client')
Esempio n. 30
0
 def test_toolbar_decr(self):
     client = get_client()
     self.assertEqual(client.cache, {})
     client.decr('testing')
     self.assertEqual(client.cache, {'testing|count': [[-1, 1]]})
Esempio n. 31
0
 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)
Esempio n. 32
0
 def test_null(self):
     eq_(get_client().__module__, 'django_statsd.clients.null')
Esempio n. 33
0
 def test_toolbar(self):
     self.assertEqual(get_client().__module__, 'django_statsd.clients.toolbar')
Esempio n. 34
0
 def test_toolbar(self):
     eq_(get_client().__module__, 'django_statsd.clients.toolbar')
Esempio n. 35
0
 def test_no_metlog(self):
     with mock.patch.object(settings, 'STATSD_CLIENT',
             'django_statsd.clients.moz_metlog'):
         get_client()
Esempio n. 36
0
 def test_no_metlog(self):
     with self.settings(STATSD_PREFIX='moz_metlog',
                        STATSD_CLIENT='django_statsd.clients.moz_metlog'):
         get_client()
Esempio n. 37
0
 def test_null(self):
     self.assertEqual(get_client().__module__, 'django_statsd.clients.null')
Esempio n. 38
0
 def test_toolbar_set(self):
     client = get_client()
     self.assertEqual(client.cache, {})
     client.set('testing', 1)
     self.assertEqual(client.cache, {'testing|set': [[1, 1]]})
Esempio n. 39
0
 def test_normal(self):
     self.assertEqual(get_client().__module__, 'statsd.client')
Esempio n. 40
0
    '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': [
            {
Esempio n. 41
0
 def test_no_metlog(self):
     with mock.patch.object(settings, 'STATSD_CLIENT',
                            'django_statsd.clients.moz_metlog'):
         get_client()