def test_one_summary_format_with_const_labels(self):
        data = {
            'name': "logged_users_total",
            'doc': "Logged users in the application",
            'const_labels': {
                "app": "my_app"
            },
        }

        labels = {'handler': '/static'}
        values = [3, 5.2, 13, 4]

        s = Summary(**data)

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

        tmp_valid_data = [
            (labels, {
                0.5: 4.0,
                0.9: 5.2,
                0.99: 5.2,
                "sum": 25.2,
                "count": 4
            }),
        ]
        valid_result = self._create_protobuf_object(data, tmp_valid_data,
                                                    pmp.SUMMARY,
                                                    data['const_labels'])

        f = BinaryFormatter()

        result = f.marshall_collector(s)
        self.assertTrue(self._protobuf_metric_equal(valid_result, result))
    def test_wrong_format(self):
        data = {
            'name': "logged_users_total",
            'doc': "Logged users in the application",
            'const_labels': {
                "app": "my_app"
            },
        }

        f = BinaryFormatter()

        c = Collector(**data)

        with self.assertRaises(TypeError) as context:
            f.marshall_collector(c)

        self.assertEqual('Not a valid object format', str(context.exception))
    def test_gauge_format_with_const_labels(self):
        data = {
            'name': "logged_users_total",
            'doc': "Logged users in the application",
            'const_labels': {
                "app": "my_app"
            },
        }
        g = Gauge(**data)

        gauge_data = (
            ({
                'country': "sp",
                "device": "desktop"
            }, 520),
            ({
                'country': "us",
                "device": "mobile"
            }, 654),
            ({
                'country': "uk",
                "device": "desktop"
            }, 1001),
            ({
                'country': "de",
                "device": "desktop"
            }, 995),
            ({
                'country': "zh",
                "device": "desktop"
            }, 520),
        )

        # Construct the result to compare
        valid_result = self._create_protobuf_object(data, gauge_data,
                                                    pmp.GAUGE,
                                                    data['const_labels'])

        # Add data to the collector
        for i in gauge_data:
            g.set_value(i[0], i[1])

        f = BinaryFormatter()

        result = f.marshall_collector(g)

        self.assertTrue(self._protobuf_metric_equal(valid_result, result))
    def test_counter_format(self):

        data = {
            'name': "logged_users_total",
            'doc': "Logged users in the application",
            'const_labels': None,
        }
        c = Counter(**data)

        counter_data = (
            ({
                'country': "sp",
                "device": "desktop"
            }, 520),
            ({
                'country': "us",
                "device": "mobile"
            }, 654),
            ({
                'country': "uk",
                "device": "desktop"
            }, 1001),
            ({
                'country': "de",
                "device": "desktop"
            }, 995),
            ({
                'country': "zh",
                "device": "desktop"
            }, 520),
        )

        # Construct the result to compare
        valid_result = self._create_protobuf_object(data, counter_data,
                                                    pmp.COUNTER)

        # Add data to the collector
        for i in counter_data:
            c.set_value(i[0], i[1])

        f = BinaryFormatter()

        result = f.marshall_collector(c)

        self.assertTrue(self._protobuf_metric_equal(valid_result, result))
Exemplo n.º 5
0
    def test_no_metric_instances_present_binary(self):
        """ Check marshalling a collector with no metrics instances present """

        c = Counter(
            name=self.counter_metric_name,
            doc=self.counter_metric_help,
            const_labels=self.const_labels,
        )

        f = BinaryFormatter()

        result = f.marshall_collector(c)
        self.assertIsInstance(result, pmp.MetricFamily)

        # Construct the result expected to receive when the counter
        # collector is marshalled.
        expected_result = pmp.create_counter(self.counter_metric_name,
                                             self.counter_metric_help, [])

        self.assertEqual(result, expected_result)
Exemplo n.º 6
0
    def test_histogram_format_binary(self):

        h = Histogram(
            name=self.histogram_metric_name,
            doc=self.histogram_metric_help,
            buckets=self.histogram_metric_buckets,
        )

        # Add data to the collector
        for labels, values in self.histogram_metric_data_values:
            for value in values:
                h.add(labels, value)

        f = BinaryFormatter()

        result = f.marshall_collector(h)
        self.assertIsInstance(result, pmp.MetricFamily)
        self.assertEqual(len(result.metric), 1)

        # Construct the result to expected to receive when the histogram
        # collector is marshalled.
        expected_result = pmp.create_histogram(
            self.histogram_metric_name,
            self.histogram_metric_help,
            self.histogram_metric_data,
        )

        self.assertEqual(result, expected_result)

        ######################################################################

        # Check metric with constant labels
        h = Histogram(
            name=self.histogram_metric_name,
            doc=self.histogram_metric_help,
            const_labels=self.const_labels,
            buckets=self.histogram_metric_buckets,
        )

        # Add data to the collector
        for labels, values in self.histogram_metric_data_values:
            for value in values:
                h.add(labels, value)

        f = BinaryFormatter()

        result = f.marshall_collector(h)
        self.assertIsInstance(result, pmp.MetricFamily)
        self.assertEqual(len(result.metric), 1)

        # Construct the result to expected to receive when the histogram
        # collector is marshalled.
        expected_result = pmp.create_histogram(
            self.histogram_metric_name,
            self.histogram_metric_help,
            self.histogram_metric_data,
            const_labels=self.const_labels,
        )

        self.assertEqual(result, expected_result)

        ######################################################################

        # Check metric with timestamps
        with unittest.mock.patch.object(pmp.utils,
                                        "_timestamp_ms",
                                        return_value=TEST_TIMESTAMP):

            h = Histogram(
                name=self.histogram_metric_name,
                doc=self.histogram_metric_help,
                buckets=self.histogram_metric_buckets,
            )

            # Add data to the collector
            for labels, values in self.histogram_metric_data_values:
                for value in values:
                    h.add(labels, value)

            f = BinaryFormatter(timestamp=True)

            result = f.marshall_collector(h)
            self.assertIsInstance(result, pmp.MetricFamily)
            self.assertEqual(len(result.metric), 1)

            # Construct the result to expected to receive when the histogram
            # collector is marshalled.
            expected_result = pmp.create_histogram(
                self.histogram_metric_name,
                self.histogram_metric_help,
                self.histogram_metric_data,
                timestamp=True,
            )

        self.assertEqual(result, expected_result)
Exemplo n.º 7
0
    def test_summary_format_binary(self):

        s = Summary(name=self.summary_metric_name,
                    doc=self.summary_metric_help)

        # Add data to the collector
        for labels, values in self.summary_metric_data_values:
            for value in values:
                s.add(labels, value)

        f = BinaryFormatter()

        result = f.marshall_collector(s)
        self.assertIsInstance(result, pmp.MetricFamily)
        self.assertEqual(len(result.metric), 1)

        # Construct the result to expected to receive when the summary
        # collector is marshalled.
        expected_result = pmp.create_summary(self.summary_metric_name,
                                             self.summary_metric_help,
                                             self.summary_metric_data)

        self.assertEqual(result, expected_result)

        ######################################################################

        # Check metric with constant labels
        s = Summary(
            name=self.summary_metric_name,
            doc=self.summary_metric_help,
            const_labels=self.const_labels,
        )

        # Add data to the collector
        for labels, values in self.summary_metric_data_values:
            for value in values:
                s.add(labels, value)

        f = BinaryFormatter()

        result = f.marshall_collector(s)
        self.assertIsInstance(result, pmp.MetricFamily)
        self.assertEqual(len(result.metric), 1)

        # Construct the result to expected to receive when the summary
        # collector is marshalled.
        expected_result = pmp.create_summary(
            self.summary_metric_name,
            self.summary_metric_help,
            self.summary_metric_data,
            const_labels=self.const_labels,
        )

        self.assertEqual(result, expected_result)

        ######################################################################

        # Check metric with timestamps
        with unittest.mock.patch.object(pmp.utils,
                                        "_timestamp_ms",
                                        return_value=TEST_TIMESTAMP):

            s = Summary(name=self.summary_metric_name,
                        doc=self.summary_metric_help)

            # Add data to the collector
            for labels, values in self.summary_metric_data_values:
                for value in values:
                    s.add(labels, value)

            f = BinaryFormatter(timestamp=True)

            result = f.marshall_collector(s)
            self.assertIsInstance(result, pmp.MetricFamily)
            self.assertEqual(len(result.metric), 1)

            # Construct the result to expected to receive when the summary
            # collector is marshalled.
            expected_result = pmp.create_summary(
                self.summary_metric_name,
                self.summary_metric_help,
                self.summary_metric_data,
                timestamp=True,
            )

        self.assertEqual(result, expected_result)

        ######################################################################

        # Check metric with multiple metric instances

        input_summary_data = (
            ({
                "interval": "5s"
            }, [3, 5.2, 13, 4]),
            ({
                "interval": "10s"
            }, [1.3, 1.2, 32.1, 59.2, 109.46, 70.9]),
            ({
                "interval": "10s",
                "method": "fast"
            }, [5, 9.8, 31, 9.7, 101.4]),
        )

        managed_summary_data = (
            (
                {
                    "interval": "5s"
                },
                {
                    0.5: 4.0,
                    0.9: 5.2,
                    0.99: 5.2,
                    "sum": 25.2,
                    "count": 4
                },
            ),
            (
                {
                    "interval": "10s"
                },
                {
                    0.5: 32.1,
                    0.9: 59.2,
                    0.99: 59.2,
                    "sum": 274.15999999999997,
                    "count": 6,
                },
            ),
            (
                {
                    "interval": "10s",
                    "method": "fast"
                },
                {
                    0.5: 9.7,
                    0.9: 9.8,
                    0.99: 9.8,
                    "sum": 156.9,
                    "count": 5
                },
            ),
        )

        s = Summary(name=self.summary_metric_name,
                    doc=self.summary_metric_help)

        # Add data to the collector
        for labels, values in input_summary_data:
            for value in values:
                s.add(labels, value)

        f = BinaryFormatter()

        result = f.marshall_collector(s)
        self.assertIsInstance(result, pmp.MetricFamily)
        self.assertEqual(len(result.metric), 3)

        # Construct the result to expected to receive when the summary
        # collector is marshalled.
        expected_result = pmp.create_summary(self.summary_metric_name,
                                             self.summary_metric_help,
                                             managed_summary_data)

        self.assertEqual(result, expected_result)
Exemplo n.º 8
0
    def test_gauge_format_binary(self):

        g = Gauge(name=self.gauge_metric_name, doc=self.gauge_metric_help)

        # Add data to the collector
        for labels, values in self.gauge_metric_data:
            g.set_value(labels, values)

        f = BinaryFormatter()

        result = f.marshall_collector(g)
        self.assertIsInstance(result, pmp.MetricFamily)

        # Construct the result to expected to receive when the gauge
        # collector is marshalled.
        expected_result = pmp.create_gauge(self.gauge_metric_name,
                                           self.gauge_metric_help,
                                           self.gauge_metric_data)

        self.assertEqual(result, expected_result)

        ######################################################################

        # Check metric with constant labels
        g = Gauge(
            name=self.gauge_metric_name,
            doc=self.gauge_metric_help,
            const_labels=self.const_labels,
        )

        # Add data to the collector
        for labels, values in self.gauge_metric_data:
            g.set_value(labels, values)

        f = BinaryFormatter()

        result = f.marshall_collector(g)
        self.assertIsInstance(result, pmp.MetricFamily)

        # Construct the result to expected to receive when the gauge
        # collector is marshalled.
        expected_result = pmp.create_gauge(
            self.gauge_metric_name,
            self.gauge_metric_help,
            self.gauge_metric_data,
            const_labels=self.const_labels,
        )

        self.assertEqual(result, expected_result)

        ######################################################################

        # Check metric with timestamps
        with unittest.mock.patch.object(pmp.utils,
                                        "_timestamp_ms",
                                        return_value=TEST_TIMESTAMP):

            g = Gauge(name=self.gauge_metric_name, doc=self.gauge_metric_help)

            # Add data to the collector
            for labels, values in self.gauge_metric_data:
                g.set_value(labels, values)

            f = BinaryFormatter(timestamp=True)

            result = f.marshall_collector(g)
            self.assertIsInstance(result, pmp.MetricFamily)

            # Construct the result to expected to receive when the gauge
            # collector is marshalled.
            expected_result = pmp.create_gauge(
                self.gauge_metric_name,
                self.gauge_metric_help,
                self.gauge_metric_data,
                timestamp=True,
            )

            self.assertEqual(result, expected_result)
    def test_summary_format(self):
        data = {
            'name': "logged_users_total",
            'doc': "Logged users in the application",
            'const_labels': {},
        }

        summary_data = (
            ({
                'interval': "5s"
            }, [3, 5.2, 13, 4]),
            ({
                'interval': "10s"
            }, [1.3, 1.2, 32.1, 59.2, 109.46, 70.9]),
            ({
                'interval': "10s",
                'method': "fast"
            }, [5, 9.8, 31, 9.7, 101.4]),
        )

        s = Summary(**data)

        for i in summary_data:
            for j in i[1]:
                s.add(i[0], j)

        tmp_valid_data = [
            ({
                'interval': "5s"
            }, {
                0.5: 4.0,
                0.9: 5.2,
                0.99: 5.2,
                "sum": 25.2,
                "count": 4
            }),
            ({
                'interval': "10s"
            }, {
                0.5: 32.1,
                0.9: 59.2,
                0.99: 59.2,
                "sum": 274.15999999999997,
                "count": 6
            }),
            ({
                'interval': "10s",
                'method': "fast"
            }, {
                0.5: 9.7,
                0.9: 9.8,
                0.99: 9.8,
                "sum": 156.9,
                "count": 5
            }),
        ]
        valid_result = self._create_protobuf_object(data, tmp_valid_data,
                                                    pmp.SUMMARY)

        f = BinaryFormatter()

        result = f.marshall_collector(s)
        self.assertTrue(self._protobuf_metric_equal(valid_result, result))