Skip to content

Commit 1f712d0

Browse files
committed
add prometheus monitor
1 parent 5b0cf58 commit 1f712d0

File tree

5 files changed

+142
-10
lines changed

5 files changed

+142
-10
lines changed

haipproxy/config/settings.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
REDIS_HOST = '127.0.0.1'
6161
REDIS_PORT = 6379
6262
REDIS_PASSWORD = '123456'
63-
REDIS_DB = 3
63+
REDIS_DB = 0
6464

6565
# scheduler settings
6666
TIMER_RECORDER = 'haipproxy:scheduler:task'
@@ -142,11 +142,10 @@
142142
# https://github.com/getsentry/sentry
143143
# disable it by setting USE_SENTRY = False
144144
USE_SENTRY = True
145-
SENTRY_DSN = 'http://009f7f5f50794deeb24791a39b86f254:56a9a5f5d3fc42a18bf87aa4341f8f3f@127.0.0.1:9000/2'
145+
SENTRY_DSN = 'http://82c130028fa942f29add1e0aa0ff9cbd:cffa174304d248b9aa2bdb385d3b01b8@127.0.0.1:9000/6'
146146

147147
# prometheus for monitoring, for more information see
148148
# https://github.com/prometheus/prometheus
149-
# disable it by setting use_prom = False
150-
USE_PROM = False
149+
# you have to config prometheus first if you want to monitor haipproxy status
151150
EXPORTER_LISTEN_HOST = '0.0.0.0'
152-
EXPORTER_LISTEN_PORT = 8000
151+
EXPORTER_LISTEN_PORT = 7001

haipproxy/monitor/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .exporter import exporter_start

haipproxy/monitor/exporter.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import time
2+
3+
from prometheus_client import start_http_server
4+
from prometheus_client.core import (
5+
CounterMetricFamily, GaugeMetricFamily,
6+
REGISTRY
7+
)
8+
9+
from haipproxy.config.settings import (
10+
DATA_ALL, INIT_HTTP_QUEUE,
11+
SPIDER_COMMON_TASK, SPIDER_AJAX_TASK,
12+
SPIDER_GFW_TASK, SPIDER_AJAX_GFW_TASK,
13+
TEMP_ZHIHU_QUEUE, VALIDATED_ZHIHU_QUEUE,
14+
TTL_ZHIHU_QUEUE, SPEED_ZHIHU_QUEUE,
15+
TTL_VALIDATED_RESOURCE, LOWEST_SCORE,
16+
LONGEST_RESPONSE_TIME, EXPORTER_LISTEN_HOST,
17+
EXPORTER_LISTEN_PORT,)
18+
from haipproxy.utils import get_redis_conn
19+
20+
21+
class CustomCollector:
22+
def __init__(self):
23+
self.conn = get_redis_conn()
24+
25+
def collect(self):
26+
start_time = int(time.time()) - TTL_VALIDATED_RESOURCE * 60
27+
28+
pipe = self.conn.pipeline(False)
29+
pipe.scard(DATA_ALL)
30+
pipe.llen(INIT_HTTP_QUEUE)
31+
pipe.scard(TEMP_ZHIHU_QUEUE)
32+
pipe.zrevrangebyscore(VALIDATED_ZHIHU_QUEUE, '+inf', LOWEST_SCORE)
33+
pipe.zrevrangebyscore(TTL_ZHIHU_QUEUE, '+inf', start_time)
34+
pipe.zrangebyscore(SPEED_ZHIHU_QUEUE, 0, 1000*LONGEST_RESPONSE_TIME)
35+
pipe.llen(SPIDER_COMMON_TASK)
36+
pipe.llen(SPIDER_AJAX_TASK)
37+
pipe.llen(SPIDER_GFW_TASK)
38+
pipe.llen(SPIDER_AJAX_GFW_TASK)
39+
r = pipe.execute()
40+
available_proxies = len(set(r[3]) & set(r[4]) & set(r[5]))
41+
42+
yield CounterMetricFamily('total_proxies', 'total proxies', r[0])
43+
yield GaugeMetricFamily('init_proxies', 'total init proxies', r[1])
44+
yield GaugeMetricFamily('to_validated_proxies', 'total proxies to validate', r[2])
45+
yield GaugeMetricFamily('scored_proxies', 'high score proxies', len(r[3]))
46+
yield GaugeMetricFamily('alive_proxies', 'proxies validated recently', len(r[4]))
47+
yield GaugeMetricFamily('speed_proxies', 'high speed proxies', len(r[5]))
48+
yield GaugeMetricFamily('available_proxies', 'proxies of high quality', available_proxies)
49+
yield GaugeMetricFamily('total_common_task', 'common task num', r[6])
50+
yield GaugeMetricFamily('total_ajax_task', 'ajax task num', r[7])
51+
yield GaugeMetricFamily('total_gfw_task', 'gfw task num', r[8])
52+
yield GaugeMetricFamily('total_ajax_gfw_task', 'ajax gfw task num', r[9])
53+
54+
55+
def exporter_start():
56+
print('starting server http://{}:{}/metrics'.format(
57+
EXPORTER_LISTEN_HOST, EXPORTER_LISTEN_PORT))
58+
REGISTRY.register(CustomCollector())
59+
start_http_server(EXPORTER_LISTEN_PORT, addr=EXPORTER_LISTEN_HOST)
60+
while True:
61+
time.sleep(5)
62+
63+

monitor_booter.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
This module is used to start prometheus exporter.
3+
4+
If you have your own metrics to monitor, just custom them
5+
in haipproxy.monitor.exporter
6+
"""
7+
from haipproxy.monitor import exporter_start
8+
9+
10+
if __name__ == '__main__':
11+
exporter_start()

requirements.txt

+63-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,66 @@
1-
Scrapy==1.5.0
2-
requests==2.18.4
3-
redis==2.10.6
1+
amqp==2.3.2
2+
asn1crypto==0.24.0
3+
attrs==17.4.0
4+
Automat==0.6.0
5+
Babel==2.6.0
6+
beautifulsoup4==4.6.0
7+
billiard==3.5.0.3
8+
bs4==0.0.1
9+
cached-property==1.4.0
10+
cachetools==2.0.1
11+
celery==4.1.0
12+
certifi==2018.1.18
13+
cffi==1.11.4
14+
chardet==3.0.4
15+
click==6.7
16+
constantly==15.1.0
17+
cryptography==2.1.4
18+
cssselect==1.0.3
19+
docker==2.7.0
20+
docker-compose==1.19.0
21+
docker-pycreds==0.2.2
22+
dockerpty==0.4.1
23+
docopt==0.6.2
24+
flower==0.9.1
25+
google-auth==1.4.1
26+
hyperlink==17.3.1
27+
idna==2.6
28+
incremental==17.5.0
29+
ipaddress==1.0.19
30+
jsonschema==2.6.0
31+
kombu==4.2.1
32+
kubernetes==5.0.0
33+
lxml==4.1.1
34+
oauthlib==2.0.7
35+
parsel==1.3.1
36+
prometheus-client==0.2.0
37+
pyasn1==0.4.2
38+
pyasn1-modules==0.2.1
39+
pycparser==2.18
40+
PyDispatcher==2.0.5
41+
PyMySQL==0.7.11
42+
pyOpenSSL==17.5.0
43+
python-dateutil==2.7.2
44+
pytz==2018.4
45+
PyYAML==3.12
46+
queuelib==1.4.2
47+
raven==6.8.0
48+
redis==2.10.5
49+
requests==2.13.0
50+
requests-oauthlib==0.8.0
51+
rsa==3.4.2
452
schedule==0.5.0
53+
Scrapy==1.5.0
554
scrapy-splash==0.7.2
6-
click==6.7
55+
selenium==3.11.0
756
service-identity==17.0.0
8-
raven==6.8.0
57+
six==1.11.0
58+
SQLAlchemy==1.1.15
59+
texttable==0.9.1
60+
tornado==4.2
61+
Twisted==17.9.0
62+
urllib3==1.22
63+
vine==1.1.4
64+
w3lib==1.19.0
65+
websocket-client==0.47.0
66+
zope.interface==4.4.3

0 commit comments

Comments
 (0)