forked from SpiderClub/haipproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter.py
43 lines (34 loc) · 1.04 KB
/
exporter.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
import time
from prometheus_client import start_http_server
from prometheus_client.core import (
CounterMetricFamily, REGISTRY
)
from haipproxy.utils import get_redis_conn
from examples.zhihu.configs import (
REDIS_HOST, REDIS_PORT,
REDIS_PASS, REDIS_DB,
TOTAL_SUCCESS_REQUESTS
)
redis_args = {
'host': REDIS_HOST,
'port': REDIS_PORT,
'password': REDIS_PASS,
'db': REDIS_DB
}
class CustomCollector:
def __init__(self):
self.redis_con = get_redis_conn(**redis_args)
def collect(self):
try:
redis_count = int(self.redis_con.get(TOTAL_SUCCESS_REQUESTS).decode())
except AttributeError:
redis_count = 0
yield CounterMetricFamily('total_success_requests',
'successful requests', value=redis_count)
if __name__ == '__main__':
port = 7000
print('starting server http://127.0.0.1:{}/metrics'.format(port))
REGISTRY.register(CustomCollector())
start_http_server(port, addr='0.0.0.0')
while True:
time.sleep(3)