def setUp(self):
     self.registry = prometheus_redis_client.Registry()
     self.registry.set_redis(redis.from_url('redis://localhost:6379'))
     self.some_gauge = prometheus_redis_client.Gauge('some_gauge',
                                                     'Some gauge.',
                                                     registry=self.registry)
     self.some_gauge.set(42)
     self.some_labelled_gauge = prometheus_redis_client.Gauge(
         'some_labelled_gauge',
         'Some labelled gauge.', ['labelred', 'labelblue'],
         registry=self.registry)
     self.some_labelled_gauge.labels('pink', 'indigo').set(1)
     self.some_labelled_gauge.labels('pink', 'royal').set(2)
     self.some_labelled_gauge.labels('carmin', 'indigo').set(3)
     self.some_labelled_gauge.labels('carmin', 'royal').set(4)
     self.t = SomeTestCase()
Exemplo n.º 2
0
    def test_interface_with_labels(self):
        with MetricEnvironment() as redis:
            gauge = prom.Gauge(
                "test_gauge",
                "Gauge Documentation",
                ['name'],
                expire=4,
            )

            gauge.labels(name='test').set(12.3)

            gauge_index = int(redis.get(prom.DEFAULT_GAUGE_INDEX_KEY))
            group_key = gauge.get_metric_group_key()
            metric_key = "test_gauge:{}".format(
                base64.b64encode(
                    ('{"gauge_index": %s, "name": "test"}' % gauge_index
                     ).encode('utf-8')).decode('utf-8')).encode('utf-8')
            assert sorted(redis.smembers(group_key)) == sorted([
                metric_key,
            ])
            assert float(redis.get(metric_key)) == 12.3

            gauge.labels(name='test').inc(1.7)
            assert sorted(redis.smembers(group_key)) == sorted([
                metric_key,
            ])
            assert float(redis.get(metric_key)) == 14.0

            assert prom.REGISTRY.output() == (
                "# HELP test_gauge Gauge Documentation\n"
                "# TYPE test_gauge gauge\n"
                "test_gauge{gauge_index=\"%s\",name=\"test\"} 14"
            ) % gauge_index
Exemplo n.º 3
0
    def test_auto_clean(self):
        with MetricEnvironment() as redis:
            gauge = prom.Gauge(
                "test_gauge",
                "Gauge Documentation",
                expire=4,
            )
            gauge.set(12.3)

            group_key = gauge.get_metric_group_key()
            gauge_index = int(redis.get(prom.DEFAULT_GAUGE_INDEX_KEY))
            metric_key = "test_gauge:{}".format(
                base64.b64encode(
                    ('{"gauge_index": %s}' % gauge_index
                     ).encode('utf-8')).decode('utf-8')).encode('utf-8')
            assert float(redis.get(metric_key)) == 12.3

            # force stop refresh metrics
            prom.REGISTRY.refresher.cleanup_and_stop()

            # after expire timeout metric should be remove
            time.sleep(5)
            assert redis.get(metric_key) is None

            assert prom.REGISTRY.output() == (
                "# HELP test_gauge Gauge Documentation\n"
                "# TYPE test_gauge gauge")
            # ... and remove metric from group
            assert redis.smembers(group_key) == set()
Exemplo n.º 4
0
    def test_refresh(self):
        with MetricEnvironment() as redis:
            gauge = prom.Gauge(
                "test_gauge",
                "Gauge Documentation",
                expire=4,
            )

            gauge.set(12.3)

            gauge_index = int(redis.get(prom.DEFAULT_GAUGE_INDEX_KEY))
            metric_key = "test_gauge:{}".format(
                base64.b64encode(
                    ('{"gauge_index": %s}' % gauge_index
                     ).encode('utf-8')).decode('utf-8')).encode('utf-8')
            assert float(redis.get(metric_key)) == 12.3

            time.sleep(6)
            assert float(redis.get(metric_key)) == 12.3

            assert (prom.REGISTRY.output()
                    ) == ("# HELP test_gauge Gauge Documentation\n"
                          "# TYPE test_gauge gauge\n"
                          "test_gauge{gauge_index=\"%s\"} 12.3") % gauge_index