-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
67 lines (49 loc) · 1.72 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from wsgiref.simple_server import make_server
import logging
import yaml
from prometheus_client import make_wsgi_app
from rbd_prober.prober import RBDProber, PrometheusExporter
def home():
status = '200 OK'
header = ('Content-Type', 'text/html')
output = b'''<html>
<head><title>RBD Prober</title></head>
<body>
<h1>RBD Prober</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>'''
return status, header, output
def not_found():
status = '404 Not Found'
header = ('Content-Type', 'application/openmetrics-text')
output = b''
return status, header, output
def wsgi():
def exporter_app(environ, start_response):
if environ['PATH_INFO'] == '/metrics':
app = make_wsgi_app()
return app(environ, start_response)
if environ['PATH_INFO'] == '/':
status, header, output = home()
else:
status, header, output = not_found()
start_response(status, [header])
return [output]
return exporter_app
with open("./config.yaml") as config_file:
configs = yaml.full_load(config_file)
# exporer config
exporter_host = configs['exporter_host']
exporter_port = configs['exporter_port']
level = logging.getLevelName(configs.get('log_level', 'info').upper())
logging.basicConfig(level=level)
# init exporter metrics
PrometheusExporter.getInstance().init_metrics(configs['histogram_buckets'])
for config in configs['probs']:
rbd_prober = RBDProber(**config)
rbd_prober.start()
app = wsgi()
httpd = make_server(exporter_host, exporter_port, app)
logging.info(f"Listening on address: {exporter_host}:{exporter_port}")
httpd.serve_forever()