class incrementCounter(object): """docstring for incrementCounter""" def __init__(self, counter): super(incrementCounter, self).__init__() self.counterName = counter self.pushgatewayUrl = 'http://pushgateway:9091' self.c = Counter(self.counterName, 'description for counter') self.p = Pusher('pushgateway', self.pushgatewayUrl) self.registry = Registry() self.registry.register(self.c) def inc(self): self.counterValue = None #c = counter if self.counterName is not None: if self.counterValue == None: self.c.inc({'type': 'hit'}) else: self.c.add({'type': 'hit'}, self.counterValue) else: return False self.p.add(self.registry) return True
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))
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()
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))
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))
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'])
def __init__(self, counter): super(incrementCounter, self).__init__() self.counterName = counter self.pushgatewayUrl = 'http://pushgateway:9091' self.c = Counter(self.counterName, 'description for counter') self.p = Pusher('pushgateway', self.pushgatewayUrl) self.registry = Registry() self.registry.register(self.c)
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'])
def inc(self, arg=None): self.gaugeValue = arg if self.gaugeName is not None: p = Pusher('pushgateway', self.pushgatewayUrl) registry = Registry() c = Gauge(self.gaugeName, 'description for gauge', {}) registry.register(c) if self.gaugeValue == None: c.inc({'type': 'hit'}) else: c.add({'type': 'hit'}, self.gaugeValue) else: return False p.add(registry) return True
def test_deregister(self): r = Registry() r.register(Collector(**self.data)) r.deregister(self.data['name']) self.assertEqual(0, len(r.collectors))
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()
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))
class TestTextExporter(unittest.TestCase): 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() def test_counter(self): # Add some metrics data = ( ({'data': 1}, 100), ({'data': "2"}, 200), ({'data': 3}, 300), ({'data': 1}, 400), ) c = Counter("test_counter", "Test Counter.", {'test': "test_counter"}) self.registry.register(c) for i in data: c.set(i[0], i[1]) headers = {'accept': 'text/plain; version=0.0.4'} url = urllib.parse.urljoin(TEST_URL, TEST_METRICS_PATH[1:]) r = requests.get(url, headers=headers) valid_data = """# HELP test_counter Test Counter. # TYPE test_counter counter test_counter{data="1",test="test_counter"} 400 test_counter{data="2",test="test_counter"} 200 test_counter{data="3",test="test_counter"} 300 """ 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) def test_gauge(self): # Add some metrics data = ( ({'data': 1}, 100), ({'data': "2"}, 200), ({'data': 3}, 300), ({'data': 1}, 400), ) g = Gauge("test_gauge", "Test Gauge.", {'test': "test_gauge"}) self.registry.register(g) for i in data: g.set(i[0], i[1]) headers = {'accept': 'text/plain; version=0.0.4'} url = urllib.parse.urljoin(TEST_URL, TEST_METRICS_PATH[1:]) r = requests.get(url, headers=headers) valid_data = """# HELP test_gauge Test Gauge. # TYPE test_gauge gauge test_gauge{data="1",test="test_gauge"} 400 test_gauge{data="2",test="test_gauge"} 200 test_gauge{data="3",test="test_gauge"} 300 """ 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) def test_summary(self): # Add some metrics data = [3, 5.2, 13, 4] label = {'data': 1} s = Summary("test_summary", "Test Summary.", {'test': "test_summary"}) self.registry.register(s) for i in data: s.add(label, i) headers = {'accept': 'text/plain; version=0.0.4'} url = urllib.parse.urljoin(TEST_URL, TEST_METRICS_PATH[1:]) r = requests.get(url, headers=headers) valid_data = """# HELP test_summary Test Summary. # TYPE test_summary summary test_summary_count{data="1",test="test_summary"} 4 test_summary_sum{data="1",test="test_summary"} 25.2 test_summary{data="1",quantile="0.5",test="test_summary"} 4.0 test_summary{data="1",quantile="0.9",test="test_summary"} 5.2 test_summary{data="1",quantile="0.99",test="test_summary"} 5.2 """ 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) 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) def tearDown(self): self.server.shutdown() self.server.socket.close()
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)
def test_get(self): r = Registry() c = Collector(**self.data) r.register(c) self.assertEqual(c, r.get(c.name))
def test_register_summary(self): r = Registry() r.register(Summary(**self.data)) self.assertEqual(1, len(r.collectors))
def test_register_gauge(self): r = Registry() r.register(Gauge(**self.data)) self.assertEqual(1, len(r.collectors))
def test_register_counter(self): r = Registry() r.register(Counter(**self.data)) self.assertEqual(1, len(r.collectors))
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:
import sys sys.path.append( os.path.dirname( os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))))) from prometheus.pusher import Pusher from prometheus.registry import Registry from prometheus.collectors import Gauge PUSHGATEWAY_URL = "http://127.0.0.1:9091" if __name__ == "__main__": job_name = "example" p = Pusher(job_name, PUSHGATEWAY_URL) registry = Registry() g = Gauge("up_and_down", "Up and down counter.", {}) registry.register(g) user = input("Hi! enter your username: "******"Enter a positive or negative number: ")) g.add({ 'user': user, }, n) # Push to the pushgateway p.add(registry) except ValueError: