为了更直观的了解prometheus如何工作,本文使用prometheus的python库来做一些相应的测试。
python库的github地址是https://github.com/prometheus
根据提示,使用pip安装prometheus_client
1
|
pip3 install prometheus_client |
然后根据文档中的示例文件并简单修改,运行一个client
文件命名为prometheus_python_client.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
|
from prometheus_client import start_http_server, Summary import random import time import sys # Create a metric to track time spent and requests made. REQUEST_TIME = Summary ( 'request_processing_seconds' , 'Time spent processing request' ) # Decorate function with metric. @REQUEST_TIME .time ( ) def process_request(t): """A dummy function that takes some time.""" time.sleep (t) if __name__ = = '__main__' : try : if sys.argv[ 1 ].isdigit(): port = sys.argv[ 1 ] else : port = 8080 except : port = 8080 # Start up the server to expose the metrics. start_http_server ( 8080 ) # Generate some requests. while True : process_request (random.random ( )) |
在后台运行client
1
|
pytho3 prometheus_python_client.py 8080 & |
此时可以访问本机的8080端口,可以看到相应的metric
1
|
curl 127.0.0.1:8080/metrics |
得到如图所示结果
为了能监控到这个端口为8080的目标,需要在prometheus的配置文件prometheus.yml进行一些修改
在scrape_configs块部分加上一个新的job
1
2
3
4
5
6
7
8
9
10
11
12
13
|
scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: "prometheus" # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ["localhost:9090"] - job_name: 'python-client' scrape_interval: 5s static_configs: - targets: ['localhost:8080'] labels: group: 'python-client-group' |
重启prometheus,并访问其web页面,在Expression中输入一个python client的metric并执行
可以看到对应的结果正如在scrape_configs中所配置的相一致。
到此这篇关于使用python测试prometheus的实现的文章就介绍到这了,更多相关python测试prometheus内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.51cto.com/quietguoguo/4982239