Exemplo n.º 1
0
 def setUp(self):
     self.application = None
     super(StatsdMetricCollectionTests, self).setUp()
     self.statsd = FakeStatsdServer(self.io_loop)
     statsd.install(
         self.application, **{
             'namespace': 'testing',
             'host': self.statsd.sockaddr[0],
             'port': self.statsd.sockaddr[1],
             'prepend_metric_type': True
         })
    def setUp(self):
        self.application = None
        self.namespace = 'testing'

        super().setUp()
        self.statsd = FakeStatsdServer(self.io_loop, protocol='tcp')

        statsd.install(
            self.application, **{
                'namespace': self.namespace,
                'host': self.statsd.sockaddr[0],
                'port': self.statsd.sockaddr[1],
                'protocol': 'tcp',
                'prepend_metric_type': True
            })
Exemplo n.º 3
0
 def setUp(self):
     self.application = None
     super(StatsdMethodTimingTests, self).setUp()
     self.statsd = FakeStatsdServer(self.io_loop)
     self.application.settings[statsd.SETTINGS_KEY] = {
         'host': self.statsd.sockaddr[0],
         'port': self.statsd.sockaddr[1],
         'namespace': 'testing',
     }
class UDPStatsdConfigurationTests(testing.AsyncHTTPTestCase):
    def get_app(self):
        self.application = web.Application([
            web.url('/', examples.statsd.SimpleHandler),
            web.url('/counters/(.*)/([.0-9]*)', CounterBumper),
        ])
        return self.application

    def setUp(self):
        self.application = None
        self.namespace = 'testing'

        super().setUp()
        self.statsd = FakeStatsdServer(self.io_loop, protocol='udp')

        statsd.install(
            self.application, **{
                'namespace': self.namespace,
                'host': self.statsd.sockaddr[0],
                'port': self.statsd.sockaddr[1],
                'protocol': 'udp',
                'prepend_metric_type': False
            })

    def tearDown(self):
        self.statsd.close()
        super().tearDown()

    def test_that_http_method_call_is_recorded(self):
        response = self.fetch('/')
        self.assertEqual(response.code, 204)

        expected = 'testing.SimpleHandler.GET.204'
        for path, value, stat_type in self.statsd.find_metrics(expected, 'ms'):
            assert_between(250.0, float(value), 500.0)

    def test_that_counter_accepts_increment_value(self):
        response = self.fetch('/counters/path/5', method='POST', body='')
        self.assertEqual(response.code, 204)

        prefix = 'testing.path'
        for path, value, stat_type in self.statsd.find_metrics(prefix, 'c'):
            self.assertEqual(int(value), 5)
Exemplo n.º 5
0
class StatsdMethodTimingTests(testing.AsyncHTTPTestCase):

    def get_app(self):
        self.application = web.Application([
            web.url('/', examples.statsd.SimpleHandler),
            web.url('/counters/(.*)/([.0-9]*)', CounterBumper),
        ])
        return self.application

    def setUp(self):
        self.application = None
        super(StatsdMethodTimingTests, self).setUp()
        self.statsd = FakeStatsdServer(self.io_loop)
        self.application.settings[statsd.SETTINGS_KEY] = {
            'host': self.statsd.sockaddr[0],
            'port': self.statsd.sockaddr[1],
            'namespace': 'testing',
        }

    def tearDown(self):
        self.statsd.close()
        super(StatsdMethodTimingTests, self).tearDown()

    @property
    def settings(self):
        return self.application.settings[statsd.SETTINGS_KEY]

    def test_that_http_method_call_is_recorded(self):
        response = self.fetch('/')
        self.assertEqual(response.code, 204)

        expected = 'testing.SimpleHandler.GET.204'
        for path, value, stat_type in self.statsd.find_metrics(expected, 'ms'):
            assert_between(250.0, float(value), 500.0)

    def test_that_cached_socket_is_used(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
        self.settings['socket'] = sock
        self.fetch('/')
        self.assertIs(self.settings['socket'], sock)

    def test_that_default_prefix_is_stored(self):
        del self.settings['namespace']
        self.fetch('/')
        self.assertEqual(
            self.settings['namespace'],
            'applications.' + examples.statsd.SimpleHandler.__module__)

    def test_that_counter_increment_defaults_to_one(self):
        response = self.fetch('/', method='POST', body='')
        self.assertEqual(response.code, 204)

        prefix = 'testing.request.path'
        for path, value, stat_type in self.statsd.find_metrics(prefix, 'c'):
            self.assertEqual(int(value), 1)

    def test_that_counter_accepts_increment_value(self):
        response = self.fetch('/counters/path/5', method='POST', body='')
        self.assertEqual(response.code, 204)

        prefix = 'testing.path'
        for path, value, stat_type in self.statsd.find_metrics(prefix, 'c'):
            self.assertEqual(int(value), 5)

    def test_that_execution_timer_records_time_spent(self):
        response = self.fetch('/counters/one.two.three/0.25')
        self.assertEqual(response.code, 204)

        prefix = 'testing.one.two.three'
        for path, value, stat_type in self.statsd.find_metrics(prefix, 'ms'):
            assert_between(250.0, float(value), 300.0)

    def test_that_add_metric_tag_is_ignored(self):
        response = self.fetch('/',
                              headers={'Correlation-ID': 'does not matter'})
        self.assertEqual(response.code, 204)
Exemplo n.º 6
0
class StatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
    def get_app(self):
        self.application = web.Application([
            web.url('/', examples.statsd.SimpleHandler),
            web.url('/counters/(.*)/([.0-9]*)', CounterBumper),
            web.url('/status_code', DefaultStatusCode),
        ])
        return self.application

    def setUp(self):
        self.application = None
        super(StatsdMetricCollectionTests, self).setUp()
        self.statsd = FakeStatsdServer(self.io_loop)
        statsd.install(
            self.application, **{
                'namespace': 'testing',
                'host': self.statsd.sockaddr[0],
                'port': self.statsd.sockaddr[1],
                'prepend_metric_type': True
            })

    def tearDown(self):
        self.statsd.close()
        super(StatsdMetricCollectionTests, self).tearDown()

    def test_that_http_method_call_is_recorded(self):
        response = self.fetch('/')
        self.assertEqual(response.code, 204)

        expected = 'testing.timers.SimpleHandler.GET.204'
        for path, value, stat_type in self.statsd.find_metrics(expected, 'ms'):
            assert_between(250.0, float(value), 500.0)

    def test_that_counter_increment_defaults_to_one(self):
        response = self.fetch('/', method='POST', body='')
        self.assertEqual(response.code, 204)

        prefix = 'testing.counters.request.path'
        for path, value, stat_type in self.statsd.find_metrics(prefix, 'c'):
            self.assertEqual(int(value), 1)

    def test_that_counter_accepts_increment_value(self):
        response = self.fetch('/counters/path/5', method='POST', body='')
        self.assertEqual(response.code, 204)

        prefix = 'testing.counters.path'
        for path, value, stat_type in self.statsd.find_metrics(prefix, 'c'):
            self.assertEqual(int(value), 5)

    def test_that_execution_timer_records_time_spent(self):
        response = self.fetch('/counters/one.two.three/0.25')
        self.assertEqual(response.code, 204)

        prefix = 'testing.timers.one.two.three'
        for path, value, stat_type in self.statsd.find_metrics(prefix, 'ms'):
            assert_between(250.0, float(value), 300.0)

    def test_that_add_metric_tag_is_ignored(self):
        response = self.fetch('/',
                              headers={'Correlation-ID': 'does not matter'})
        self.assertEqual(response.code, 204)

    def test_that_status_code_is_used_when_not_explicitly_set(self):
        response = self.fetch('/status_code')
        self.assertEqual(response.code, 200)

        expected = 'testing.timers.DefaultStatusCode.GET.200'
        self.assertEqual(expected,
                         list(self.statsd.find_metrics(expected, 'ms'))[0][0])
class TCPStatsdMetricCollectionTests(testing.AsyncHTTPTestCase):
    def get_app(self):
        self.application = web.Application([
            web.url('/', examples.statsd.SimpleHandler),
            web.url('/counters/(.*)/([.0-9]*)', CounterBumper),
            web.url('/status_code', DefaultStatusCode),
        ])
        return self.application

    def setUp(self):
        self.application = None
        self.namespace = 'testing'

        super().setUp()
        self.statsd = FakeStatsdServer(self.io_loop, protocol='tcp')

        statsd.install(
            self.application, **{
                'namespace': self.namespace,
                'host': self.statsd.sockaddr[0],
                'port': self.statsd.sockaddr[1],
                'protocol': 'tcp',
                'prepend_metric_type': True
            })

    @mock.patch.object(iostream.IOStream, 'write')
    def test_write_not_executed_when_connection_is_closed(self, mock_write):
        self.application.statsd._sock.close()
        self.application.statsd.send('foo', 500, 'c')
        mock_write.assert_not_called()

    @mock.patch.object(iostream.IOStream, 'write')
    def test_expected_counters_data_written(self, mock_sock):
        path = ('foo', 'bar')
        value = 500
        metric_type = 'c'
        expected = "{}:{}|{}\n".format(
            '.'.join(itertools.chain((self.namespace, 'counters'), path)),
            value, metric_type)

        self.application.statsd.send(path, value, metric_type)
        mock_sock.assert_called_once_with(expected.encode())

    @mock.patch.object(iostream.IOStream, 'write')
    def test_expected_timers_data_written(self, mock_sock):
        path = ('foo', 'bar')
        value = 500
        metric_type = 'ms'
        expected = "{}:{}|{}\n".format(
            '.'.join(itertools.chain((self.namespace, 'timers'), path)), value,
            metric_type)

        self.application.statsd.send(path, value, metric_type)
        mock_sock.assert_called_once_with(expected.encode())

    def test_tcp_message_format(self):
        expected = '{path}:{value}|{metric_type}\n'
        self.assertEqual(self.application.statsd._msg_format, expected)

    def test_that_http_method_call_is_recorded(self):
        response = self.fetch('/')
        self.assertEqual(response.code, 204)

        expected = 'testing.timers.SimpleHandler.GET.204'
        for path, value, stat_type in self.statsd.find_metrics(expected, 'ms'):
            assert_between(250.0, float(value), 500.0)

    def test_that_counter_increment_defaults_to_one(self):
        response = self.fetch('/', method='POST', body='')
        self.assertEqual(response.code, 204)

        prefix = 'testing.counters.request.path'
        for path, value, stat_type in self.statsd.find_metrics(prefix, 'c'):
            self.assertEqual(int(value), 1)

    def test_that_counter_accepts_increment_value(self):
        response = self.fetch('/counters/path/5', method='POST', body='')
        self.assertEqual(response.code, 204)

        prefix = 'testing.counters.path'
        for path, value, stat_type in self.statsd.find_metrics(prefix, 'c'):
            self.assertEqual(int(value), 5)

    def test_that_execution_timer_records_time_spent(self):
        response = self.fetch('/counters/one.two.three/0.25')
        self.assertEqual(response.code, 204)

        prefix = 'testing.timers.one.two.three'
        for path, value, stat_type in self.statsd.find_metrics(prefix, 'ms'):
            assert_between(250.0, float(value), 300.0)

    def test_that_add_metric_tag_is_ignored(self):
        response = self.fetch('/',
                              headers={'Correlation-ID': 'does not matter'})
        self.assertEqual(response.code, 204)

    def test_that_status_code_is_used_when_not_explicitly_set(self):
        response = self.fetch('/status_code')
        self.assertEqual(response.code, 200)

        expected = 'testing.timers.DefaultStatusCode.GET.200'
        self.assertEqual(expected,
                         list(self.statsd.find_metrics(expected, 'ms'))[0][0])