def test_hook_exception(self):

        message = 'exception message'

        def exception_hook(exception, **kwargs):
            self.assertIsInstance(exception, TimeExecutionException)
            return dict(exception_message=str(exception))

        def asserts(name, **data):
            self.assertEqual(data['exception_message'], message)

        configure(
            backends=[AssertBackend(asserts)],
            hooks=[exception_hook]
        )

        class TimeExecutionException(Exception):
            pass

        @time_execution
        def go():
            raise TimeExecutionException(message)

        with self.assertRaises(TimeExecutionException):
            go()
    def test_duration_field(self):

        configure(backends=[self.backend], duration_field='my_duration')

        go()

        for metric in self._query_backend(go.fqn)['hits']['hits']:
            self.assertTrue('my_duration' in metric['_source'])
    def test_duration_field(self):

        configure(backends=[self.backend], duration_field='my_duration')

        go()

        for metric in self._query_influx(go.fqn):
            self.assertTrue('my_duration' in metric)
    def test_duration_field(self):

        configure(backends=[self.backend], duration_field='my_duration')

        go()

        for metric in self._query_backend(go.fqn)['hits']['hits']:
            self.assertTrue('my_duration' in metric['_source'])
    def setUp(self):
        super(TestTimeExecution, self).setUp()

        self.backend = ElasticsearchBackend(
            'elasticsearch',
            index='unittest',
        )

        self._clear()
        configure(backends=[self.backend])
    def setUp(self):
        super(TestTimeExecution, self).setUp()

        self.backend = ElasticsearchBackend(
            'elasticsearch',
            index='unittest',
        )

        self._clear()
        configure(backends=[self.backend])
    def test_hook_func_kwargs(self):
        param = 'foo'

        def hook(response, exception, metric, func_args, func_kwargs):
            self.assertEqual(func_kwargs['param1'], param)

        configure(hooks=[hook])

        @time_execution
        def go(param1):
            return '200 OK'

        go(param1=param)
Example #8
0
    def test_hook_func_kwargs(self):
        param = 'foo'

        def hook(response, exception, metric, func_args, func_kwargs):
            self.assertEqual(func_kwargs['param1'], param)

        configure(hooks=[hook])

        @time_execution
        def go(param1):
            return '200 OK'

        go(param1=param)
    def test_time_execution_with_func_kwargs(self):
        param = 'foo'

        def hook(response, exception, metric, func_args, func_kwargs):
            self.assertEqual(func_kwargs['param1'], param)
            return metric

        configure(backends=[self.backend], hooks=[hook])

        @time_execution
        def go(param1):
            return '200 OK'

        go(param1=param)
    def setUp(self):
        super(TestTimeExecution, self).setUp()

        self.database = 'unittest'
        self.backend = InfluxBackend(host='influx',
                                     database=self.database,
                                     use_udp=False)

        try:
            self.backend.client.create_database(self.database)
        except InfluxDBClientError:
            # Something blew up so ignore it
            pass

        configure(backends=[self.backend])
    def test_hook(self):
        def test_args(**kwargs):
            self.assertIn('response', kwargs)
            self.assertIn('exception', kwargs)
            self.assertIn('metric', kwargs)
            return dict()

        def test_metadata(*args, **kwargs):
            return dict(test_key='test value')

        configure(backends=[self.backend], hooks=[test_args, test_metadata])

        go()

        for metric in self._query_backend(go.fqn):
            self.assertEqual(metric['test_key'], 'test value')
    def setUp(self):
        super(TestTimeExecution, self).setUp()

        self.database = 'unittest'
        self.backend = InfluxBackend(
            host='influx',
            database=self.database,
            use_udp=False
        )

        try:
            self.backend.client.create_database(self.database)
        except InfluxDBClientError:
            # Something blew up so ignore it
            pass

        configure(backends=[self.backend])
    def test_hook(self):

        def test_args(**kwargs):
            self.assertIn('response', kwargs)
            self.assertIn('exception', kwargs)
            self.assertIn('metric', kwargs)
            return dict()

        def test_metadata(*args, **kwargs):
            return dict(test_key='test value')

        configure(backends=[self.backend], hooks=[test_args, test_metadata])

        go()

        for metric in self._query_backend(go.fqn):
            self.assertEqual(metric['test_key'], 'test value')
Example #14
0
    def test_hook(self):
        def test_args(**kwargs):
            self.assertIn('response', kwargs)
            self.assertIn('exception', kwargs)
            self.assertIn('metric', kwargs)
            return dict()

        def test_metadata(*args, **kwargs):
            return dict(test_key='test value')

        def asserts(name, **data):
            self.assertEqual(data['test_key'], 'test value')

        configure(backends=[AssertBackend(asserts)],
                  hooks=[test_args, test_metadata])

        go()
Example #15
0
    def test_hook(self):

        def test_args(**kwargs):
            self.assertIn('response', kwargs)
            self.assertIn('exception', kwargs)
            self.assertIn('metric', kwargs)
            return dict()

        def test_metadata(*args, **kwargs):
            return dict(test_key='test value')

        def asserts(name, **data):
            self.assertEqual(data['test_key'], 'test value')

        configure(
            backends=[AssertBackend(asserts)],
            hooks=[test_args, test_metadata]
        )

        go()
    def test_time_execution_with_exception(self):

        def exception_hook(exception, **kwargs):
            self.assertTrue(exception)
            return dict(exception_message=str(exception))

        configure(backends=[self.backend], hooks=[exception_hook])

        class TimeExecutionException(Exception):
            message = 'default'

        @time_execution
        def go():
            raise TimeExecutionException('test exception')

        with self.assertRaises(TimeExecutionException):
            go()

        for metric in self._query_influx(go.fqn):
            self.assertEqual(metric['exception_message'], 'test exception')
Example #17
0
    def test_hook_exception(self):

        message = 'exception message'

        def exception_hook(exception, **kwargs):
            self.assertIsInstance(exception, TimeExecutionException)
            return dict(exception_message=str(exception))

        def asserts(name, **data):
            self.assertEqual(data['exception_message'], message)

        configure(backends=[AssertBackend(asserts)], hooks=[exception_hook])

        class TimeExecutionException(Exception):
            pass

        @time_execution
        def go():
            raise TimeExecutionException(message)

        with self.assertRaises(TimeExecutionException):
            go()