Пример #1
0
    def test_deregister(self):
        r = Registry()
        r.register(Collector(**self.data))

        r.deregister(self.data['name'])

        self.assertEqual(0, len(r.collectors))
Пример #2
0
    def test_register_wrong_type(self):
        r = Registry()

        with self.assertRaises(TypeError) as context:
            r.register("This will fail")
        self.assertEqual(
            "Can't register instance, not a valid type of collector",
            str(context.exception))
Пример #3
0
    def test_register_sames(self):
        r = Registry()

        r.register(Collector(**self.data))

        with self.assertRaises(ValueError) as context:
            r.register(Collector(**self.data))

        self.assertEqual("Collector already exists or name colision",
                         str(context.exception))
Пример #4
0
def start_http(port):
    # Create the registry
    registry = Registry()

    try:
        # We make this to set the registry in the handler
        def handler(*args, **kwargs):
            PrometheusMetricHandler(registry, *args, **kwargs)

        server = HTTPServer(('', port), handler)
        server.serve_forever()
    except KeyboardInterrupt:
        server.socket.close()
Пример #5
0
    def setUp(self):
        # Create the registry
        self.registry = Registry()

        # Handler hack
        def handler(*args, **kwargs):
            TestPrometheusMetricHandler(self.registry, *args, **kwargs)

        self.server = HTTPServer(('', TEST_PORT), handler)

        # Start a server
        thread = threading.Thread(target=self.server.serve_forever)
        thread.start()
Пример #6
0
    def test_register(self):

        q = 100
        collectors = [
            Collector('test' + str(i), 'Test' + str(i)) for i in range(q)
        ]

        r = Registry()

        for i in collectors:
            r.register(i)

        self.assertEqual(q, len(r.collectors))
Пример #7
0
    def test_get_all(self):
        q = 100
        collectors = [
            Collector('test' + str(i), 'Test' + str(i)) for i in range(q)
        ]

        r = Registry()

        for i in collectors:
            r.register(i)

        result = r.get_all()

        self.assertTrue(isinstance(result, list))
        self.assertEqual(q, len(result))
Пример #8
0
    def test_push_job_ping(self):
        job_name = "my-job"
        p = Pusher(job_name, TEST_URL)
        registry = Registry()
        c = Counter("total_requests", "Total requests.", {})
        registry.register(c)

        c.inc({
            'url': "/p/user",
        })

        # Push to the pushgateway
        p.replace(registry)

        # Check the objects that setted the server thread
        self.assertEqual(Pusher.PATH.format(job_name), self.request['path'])
Пример #9
0
    def test_push_delete(self):
        job_name = "my-job"
        p = Pusher(job_name, TEST_URL)
        registry = Registry()
        counter = Counter("counter_test", "A counter.", {'type': "counter"})
        registry.register(counter)

        counter_data = (({'c_sample': '1', 'c_subsample': 'b'}, 400), )

        [counter.set(c[0], c[1]) for c in counter_data]
        valid_result = b'[\n\x0ccounter_test\x12\nA counter.\x18\x00"=\n\r\n\x08c_sample\x12\x011\n\x10\n\x0bc_subsample\x12\x01b\n\x0f\n\x04type\x12\x07counter\x1a\t\t\x00\x00\x00\x00\x00\x00y@'

        # Push to the pushgateway
        p.delete(registry)

        # Check the object that setted the server thread
        self.assertEqual("DELETE", self.request['method'])
        self.assertEqual(valid_result, self.request['body'])
        ram_metric.set({
            'type': "virtual",
        }, ram.used)
        ram_metric.set({'type': "virtual", 'status': "cached"}, ram.cached)
        ram_metric.set({'type': "swap"}, swap.used)

        # Add cpu metrics
        for c, p in enumerate(psutil.cpu_percent(interval=1, percpu=True)):
            cpu_metric.set({'core': c}, p)


if __name__ == "__main__":

    # Create the registry
    registry = Registry()

    # Create the thread that gathers the data while we serve it
    thread = threading.Thread(target=gather_data, args=(registry, ))
    thread.start()

    # Set a server to export (expose to prometheus) the data (in a thread)
    try:
        # We make this to set the registry in the handler
        def handler(*args, **kwargs):
            PrometheusMetricHandler(registry, *args, **kwargs)

        server = HTTPServer(('', PORT_NUMBER), handler)
        server.serve_forever()

    except KeyboardInterrupt:
Пример #11
0
    def test_all(self):
        format_times = 10

        counter_data = (
            ({'c_sample': '1'}, 100),
            ({'c_sample': '2'}, 200),
            ({'c_sample': '3'}, 300),
            ({'c_sample': '1', 'c_subsample': 'b'}, 400),
        )

        gauge_data = (
            ({'g_sample': '1'}, 500),
            ({'g_sample': '2'}, 600),
            ({'g_sample': '3'}, 700),
            ({'g_sample': '1', 'g_subsample': 'b'}, 800),
        )

        summary_data = (
            ({'s_sample': '1'}, range(1000, 2000, 4)),
            ({'s_sample': '2'}, range(2000, 3000, 20)),
            ({'s_sample': '3'}, range(3000, 4000, 13)),
            ({'s_sample': '1', 's_subsample': 'b'}, range(4000, 5000, 47)),
        )

        registry = Registry()
        counter = Counter("counter_test", "A counter.", {'type': "counter"})
        gauge = Gauge("gauge_test", "A gauge.", {'type': "gauge"})
        summary = Summary("summary_test", "A summary.", {'type': "summary"})

        self.registry.register(counter)
        self.registry.register(gauge)
        self.registry.register(summary)

        # Add data
        [counter.set(c[0], c[1]) for c in counter_data]
        [gauge.set(g[0], g[1]) for g in gauge_data]
        [summary.add(i[0], s) for i in summary_data for s in i[1]]

        registry.register(counter)
        registry.register(gauge)
        registry.register(summary)

        valid_data = """# HELP counter_test A counter.
# TYPE counter_test counter
counter_test{c_sample="1",c_subsample="b",type="counter"} 400
counter_test{c_sample="1",type="counter"} 100
counter_test{c_sample="2",type="counter"} 200
counter_test{c_sample="3",type="counter"} 300
# HELP gauge_test A gauge.
# TYPE gauge_test gauge
gauge_test{g_sample="1",g_subsample="b",type="gauge"} 800
gauge_test{g_sample="1",type="gauge"} 500
gauge_test{g_sample="2",type="gauge"} 600
gauge_test{g_sample="3",type="gauge"} 700
# HELP summary_test A summary.
# TYPE summary_test summary
summary_test_count{s_sample="1",s_subsample="b",type="summary"} 22
summary_test_count{s_sample="1",type="summary"} 250
summary_test_count{s_sample="2",type="summary"} 50
summary_test_count{s_sample="3",type="summary"} 77
summary_test_sum{s_sample="1",s_subsample="b",type="summary"} 98857.0
summary_test_sum{s_sample="1",type="summary"} 374500.0
summary_test_sum{s_sample="2",type="summary"} 124500.0
summary_test_sum{s_sample="3",type="summary"} 269038.0
summary_test{quantile="0.5",s_sample="1",s_subsample="b",type="summary"} 4235.0
summary_test{quantile="0.5",s_sample="1",type="summary"} 1272.0
summary_test{quantile="0.5",s_sample="2",type="summary"} 2260.0
summary_test{quantile="0.5",s_sample="3",type="summary"} 3260.0
summary_test{quantile="0.9",s_sample="1",s_subsample="b",type="summary"} 4470.0
summary_test{quantile="0.9",s_sample="1",type="summary"} 1452.0
summary_test{quantile="0.9",s_sample="2",type="summary"} 2440.0
summary_test{quantile="0.9",s_sample="3",type="summary"} 3442.0
summary_test{quantile="0.99",s_sample="1",s_subsample="b",type="summary"} 4517.0
summary_test{quantile="0.99",s_sample="1",type="summary"} 1496.0
summary_test{quantile="0.99",s_sample="2",type="summary"} 2500.0
summary_test{quantile="0.99",s_sample="3",type="summary"} 3494.0
"""

        headers = {'accept': 'text/plain; version=0.0.4'}
        url = urllib.parse.urljoin(TEST_URL, TEST_METRICS_PATH[1:])
        r = requests.get(url, headers=headers)

        self.assertEqual("text/plain; version=0.0.4; charset=utf-8",
                             r.headers['content-type'])
        self.assertEqual(200, r.status_code)
        self.assertEqual(valid_data, r.text)
Пример #12
0
    def test_get(self):
        r = Registry()
        c = Collector(**self.data)
        r.register(c)

        self.assertEqual(c, r.get(c.name))
Пример #13
0
    def test_register_summary(self):
        r = Registry()
        r.register(Summary(**self.data))

        self.assertEqual(1, len(r.collectors))
Пример #14
0
    def test_register_gauge(self):
        r = Registry()
        r.register(Gauge(**self.data))

        self.assertEqual(1, len(r.collectors))
Пример #15
0
    def test_register_counter(self):
        r = Registry()
        r.register(Counter(**self.data))

        self.assertEqual(1, len(r.collectors))