Exemplo n.º 1
0
class TestSummary(unittest.TestCase):
    def setUp(self):
        self.data = {
            "name": "http_request_duration_microseconds",
            "doc": "Request duration per application",
            "const_labels": {"app": "my_app"},
        }

        self.s = Summary(**self.data)

    def test_add(self):
        data = (
            {"labels": {"handler": "/static"}, "values": range(0, 500, 50)},
            {"labels": {"handler": "/p"}, "values": range(0, 1000, 100)},
            {"labels": {"handler": "/p/login"}, "values": range(0, 10000, 1000)},
        )

        for i in data:
            for j in i["values"]:
                self.s.add(i["labels"], j)

        for i in data:
            self.assertEqual(len(i["values"]), self.s.values[i["labels"]]._observations)

    def test_get(self):
        labels = {"handler": "/static"}
        values = [3, 5.2, 13, 4]

        for i in values:
            self.s.add(labels, i)

        data = self.s.get(labels)
        correct_data = {"sum": 25.2, "count": 4, 0.50: 4.0, 0.90: 5.2, 0.99: 5.2}

        self.assertEqual(correct_data, data)

    def test_add_get_without_labels(self):
        labels = None
        values = [3, 5.2, 13, 4]

        for i in values:
            self.s.add(labels, i)

        self.assertEqual(1, len(self.s.values))

        correct_data = {"sum": 25.2, "count": 4, 0.50: 4.0, 0.90: 5.2, 0.99: 5.2}
        self.assertEqual(correct_data, self.s.get(labels))

    def test_add_wrong_types(self):
        labels = None
        values = ["3", (1, 2), {"1": 2}, True]

        for i in values:
            with self.assertRaises(TypeError) as context:
                self.s.add(labels, i)
        self.assertEqual(
            "Summary only works with digits (int, float)", str(context.exception)
        )
Exemplo n.º 2
0
    async def test_timer(self):
        m = Summary("metric_label", "metric help")

        # decorator should work methods as well as functions
        @timer(m, {"kind": "function"})
        async def a():
            return

        await a()

        m_function = m.get({"kind": "function"})
        self.assertEqual(m_function["count"], 1)

        # decorator should work methods as well as functions
        class B(object):

            @timer(m, {"kind": "method"})
            async def b(self, arg1, arg2=None):
                return arg1 == "b_arg", arg2 == "arg_2"

        b = B()
        results = await b.b("b_arg", arg2="arg_2")
        self.assertTrue(all(results))

        m_method = m.get({"kind": "method"})
        self.assertEqual(m_method["count"], 1)

        # Only Summary metric type can be used with @timer, others should
        # raise an exception.
        with self.assertRaises(Exception) as cm:
            m = Counter("metric_label", "metric help")

            @timer(m)
            async def c():
                return

        self.assertIn(
            "timer decorator expects a Summary metric but got:", str(cm.exception)
        )
Exemplo n.º 3
0
    async def test_timer(self):
        m = Summary('metric_label', 'metric help')

        # decorator should work methods as well as functions
        @timer(m, {'kind': 'function'})
        async def a():
            return

        await a()

        m_function = m.get({'kind': 'function'})
        self.assertEqual(m_function['count'], 1)

        # decorator should work methods as well as functions
        class B(object):

            @timer(m, {'kind': 'method'})
            async def b(self, arg1, arg2=None):
                return arg1 == 'b_arg', arg2 == 'arg_2'

        b = B()
        results = await b.b('b_arg', arg2='arg_2')
        self.assertTrue(all(results))

        m_method = m.get({'kind': 'method'})
        self.assertEqual(m_method['count'], 1)

        # Only Summary metric type can be used with @timer, others should
        # raise an exception.
        with self.assertRaises(Exception) as cm:
            m = Counter('metric_label', 'metric help')

            @timer(m)
            async def c():
                return
        self.assertIn(
            "timer decorator expects a Summary metric but got:",
            str(cm.exception))
Exemplo n.º 4
0
    async def test_timer(self):
        m = Summary("metric_label", "metric help")

        # decorator should work methods as well as functions
        @timer(m, {"kind": "function"})
        async def a():
            return

        await a()

        m_function = m.get({"kind": "function"})
        self.assertEqual(m_function["count"], 1)

        # decorator should work methods as well as functions
        class B(object):
            @timer(m, {"kind": "method"})
            async def b(self, arg1, arg2=None):
                return arg1 == "b_arg", arg2 == "arg_2"

        b = B()
        results = await b.b("b_arg", arg2="arg_2")
        self.assertTrue(all(results))

        m_method = m.get({"kind": "method"})
        self.assertEqual(m_method["count"], 1)

        # Only Summary metric type can be used with @timer, others should
        # raise an exception.
        with self.assertRaises(Exception) as cm:
            m = Counter("metric_label", "metric help")

            @timer(m)
            async def c():
                return

        self.assertIn(
            "timer decorator expects a Summary metric but got:", str(cm.exception)
        )
Exemplo n.º 5
0
    async def test_timer(self):
        m = Summary('metric_label', 'metric help')

        # decorator should work methods as well as functions
        @timer(m, {'kind': 'function'})
        async def a():
            return

        await a()

        m_function = m.get({'kind': 'function'})
        self.assertEqual(m_function['count'], 1)

        # decorator should work methods as well as functions
        class B(object):
            @timer(m, {'kind': 'method'})
            async def b(self, arg1, arg2=None):
                return arg1 == 'b_arg', arg2 == 'arg_2'

        b = B()
        results = await b.b('b_arg', arg2='arg_2')
        self.assertTrue(all(results))

        m_method = m.get({'kind': 'method'})
        self.assertEqual(m_method['count'], 1)

        # Only Summary metric type can be used with @timer, others should
        # raise an exception.
        with self.assertRaises(Exception) as cm:
            m = Counter('metric_label', 'metric help')

            @timer(m)
            async def c():
                return

        self.assertIn("timer decorator expects a Summary metric but got:",
                      str(cm.exception))
Exemplo n.º 6
0
class TestSummary(unittest.TestCase):

    def setUp(self):
        self.data = {
            'name': "http_request_duration_microseconds",
            'doc': "Request duration per application",
            'const_labels': {"app": "my_app"},
        }

        self.s = Summary(**self.data)

    def test_add(self):
        data = (
            {
                'labels': {'handler': '/static'},
                'values': range(0, 500, 50)
            },
            {
                'labels': {'handler': '/p'},
                'values': range(0, 1000, 100)
            },
            {
                'labels': {'handler': '/p/login'},
                'values': range(0, 10000, 1000)
            }
        )

        for i in data:
            for j in i['values']:
                self.s.add(i['labels'], j)

        for i in data:
            self.assertEqual(len(i['values']),
                             self.s.values[i['labels']]._observations)

    def test_get(self):
        labels = {'handler': '/static'}
        values = [3, 5.2, 13, 4]

        for i in values:
                self.s.add(labels, i)

        data = self.s.get(labels)
        correct_data = {
            'sum': 25.2,
            'count': 4,
            0.50: 4.0,
            0.90: 5.2,
            0.99: 5.2,
        }

        self.assertEqual(correct_data, data)

    def test_add_get_without_labels(self):
        labels = None
        values = [3, 5.2, 13, 4]

        for i in values:
            self.s.add(labels, i)

        self.assertEqual(1, len(self.s.values))

        correct_data = {
            'sum': 25.2,
            'count': 4,
            0.50: 4.0,
            0.90: 5.2,
            0.99: 5.2,
        }
        self.assertEqual(correct_data, self.s.get(labels))

    def test_add_wrong_types(self):
        labels = None
        values = ["3", (1, 2), {'1': 2}, True]

        for i in values:
            with self.assertRaises(TypeError) as context:
                self.s.add(labels, i)
        self.assertEqual("Summary only works with digits (int, float)",
                         str(context.exception))
Exemplo n.º 7
0
class TestSummary(unittest.TestCase):
    def setUp(self):
        self.data = {
            "name": "http_request_duration_microseconds",
            "doc": "Request duration per application",
            "const_labels": {
                "app": "my_app"
            },
        }

        self.s = Summary(**self.data)

    def test_add(self):
        data = (
            {
                "labels": {
                    "handler": "/static"
                },
                "values": range(0, 500, 50)
            },
            {
                "labels": {
                    "handler": "/p"
                },
                "values": range(0, 1000, 100)
            },
            {
                "labels": {
                    "handler": "/p/login"
                },
                "values": range(0, 10000, 1000)
            },
        )

        for i in data:
            for j in i["values"]:
                self.s.add(i["labels"], j)

        for i in data:
            self.assertEqual(len(i["values"]),
                             self.s.values[i["labels"]]._observations)

    def test_get(self):
        labels = {"handler": "/static"}
        values = [3, 5.2, 13, 4]

        for i in values:
            self.s.add(labels, i)

        data = self.s.get(labels)
        correct_data = {
            "sum": 25.2,
            "count": 4,
            0.50: 4.0,
            0.90: 5.2,
            0.99: 5.2
        }

        self.assertEqual(correct_data, data)

    def test_add_get_without_labels(self):
        labels = None
        values = [3, 5.2, 13, 4]

        for i in values:
            self.s.add(labels, i)

        self.assertEqual(1, len(self.s.values))

        correct_data = {
            "sum": 25.2,
            "count": 4,
            0.50: 4.0,
            0.90: 5.2,
            0.99: 5.2
        }
        self.assertEqual(correct_data, self.s.get(labels))

    def test_add_wrong_types(self):
        labels = None
        values = ["3", (1, 2), {"1": 2}, True]

        for i in values:
            with self.assertRaises(TypeError) as context:
                self.s.add(labels, i)
        self.assertEqual("Summary only works with digits (int, float)",
                         str(context.exception))