使用python测试prometheus的实现
作者:quietguoguo 时间:2023-08-31 15:24:05
为了更直观的了解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中所配置的相一致。
来源:https://blog.51cto.com/quietguoguo/4982239
标签:python,测试,prometheus
0
投稿
猜你喜欢
Python AES加密模块用法分析
2021-05-29 05:13:58
Python完全新手教程
2021-08-19 18:38:06
mysql通过查看跟踪日志跟踪执行的sql语句
2024-01-28 00:56:10
golang中interface接口的深度解析
2024-05-08 10:52:00
MySQL死锁问题分析及解决方法实例详解
2024-01-23 12:40:18
Python导入父文件夹中模块并读取当前文件夹内的资源
2023-08-27 09:03:43
python命令行工具Click快速掌握
2021-08-13 08:03:58
Python Multiprocessing多进程 使用tqdm显示进度条的实现
2021-04-03 19:15:08
PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)
2023-10-02 13:10:01
Ubuntu10下如何搭建MySQL Proxy读写分离探讨
2024-01-20 08:04:43
详解CSS的优先权
2008-05-11 18:57:00
python回归分析逻辑斯蒂模型之多分类任务详解
2021-08-01 11:54:15
Python读写zip压缩文件的方法
2021-10-08 02:28:11
使用Python导出Excel图表以及导出为图片的方法
2021-02-28 09:59:21
python写入中英文字符串到文件的方法
2022-11-21 23:11:03
spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25开发环境搭建图文教程
2024-01-23 12:27:19
收集的几个Python小技巧分享
2023-06-14 01:54:01
Langchain集成管理prompt功能详解
2022-12-13 22:56:31
js/jq仿window文件夹移动/剪切/复制等操作代码
2024-04-16 09:50:01
一篇文章彻底弄懂Python中的if __name__ == __main__
2023-04-27 08:42:14