使用python测试prometheus的实现
为了更直观的了解prometheus如何工作,本文使用prometheus的python库来做一些相应的测试。
python库的github地址是https://github.com/prometheus
根据提示,使用pip安装prometheus_client
pip3 install prometheus_client
然后根据文档中的示例文件并简单修改,运行一个client
文件命名为prometheus_python_client.py
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
pytho3 prometheus_python_client.py 8080 &
此时可以访问本机的8080端口,可以看到相应的metric
curl 127.0.0.1:8080/metrics
得到如图所示结果

为了能监控到这个端口为8080的目标,需要在prometheus的配置文件prometheus.yml进行一些修改
在scrape_configs块部分加上一个新的job
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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Python cookbook(数据结构与算法)通过公共键对字典列表排序算法示例
这篇文章主要介绍了Python cookbook(数据结构与算法)通过公共键对字典列表排序算法,结合实例形式分析了Python基于operator模块中的itemgetter()函数对字典进行排序的相关操作技巧,需要的朋友可以参考下2018-03-03
Python的json模块中json.load()和json.loads()的区别
这篇文章主要介绍了Python的json模块中json.load()和json.loads()的区别,json.load用于从一个文件对象中读取JSON数据并将其解析为Python对象,而json.loads用于解析一个JSON格式的字符串并将其转换为Python对象,根据你的具体需求选择使用哪个方法,需要的朋友可以参考下2024-12-12


最新评论