使用python测试prometheus的实现
作者:quietguoguo 发布时间:2023-08-31 15:24:05
标签: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中所配置的相一致。
来源:https://blog.51cto.com/quietguoguo/4982239


猜你喜欢
- 一、前言本次安装tensorflow是基于Python的,安装Python的过程不做说明(既然决定按,Python肯定要先了解啊):本次教程
- 一、导入所需的库import randomimport cv2from matplotlib import pyplot as pltimp
- 初学python,抓取搜狗微信公众号文章存入mysqlmysql表:代码:import requestsimport jsonimport
- 一、web.py简介web.py是一款轻量级的Python web开发框架,简单、高效、学习成本低,特别适合作为python web开发的入
- 目录1.查询字符串数据(query string):2. 提取请求体数据2.1 表单类型请求体数据(Form Data)2.2 非表单类型请
- 先了解几个事件对象属性target 指事件源对象,点击嵌套元素最里层的某元素,该元素就是target。IE6/7/8对应的是srcEleme
- WGAN与GAN的不同去除sigmoid使用具有动量的优化方法,比如使用RMSProp要对Discriminator的权重做修整限制以确保l
- 本文将演示如何使用SQL 7.0服务器的Enterprise Manager创建、配置一个示例数据库。我们还会演示如何增加具有合适权限的用户
- 本章我们将介绍Django 管理工具及如何使用 Django 来创建项目,第一个项目我们以 HelloWorld 来命令项目。一、Djang
- 本文实例讲述了基于pako.js实现gzip的压缩和解压功能。分享给大家供大家参考,具体如下:<!DOCTYPE html>&l
- 一、介绍线程是什么?线程有啥用?线程和进程的区别是什么?线程是操作系统能够进行运算调度的最小单位。被包含在进程中,是进程中的实际运作单位。一
- 背景一直以来,中式占卜都是基于算命先生手工实现,程序繁琐(往往需要沐浴、计算天时、静心等等流程)。准备工作复杂(通常需要铜钱等道具),计算方
- 先介绍一下redis的pub/sub功能:Pub/Sub功能(means Publish, Subscribe)即发布及订阅功能。基于事件的
- 我们大致会在下列几种情况下用到: 一、文件操作(FileSystemObject)<%@ IMPORT
- 目录LAMP架构1.lamp介绍2.web服务工作流程web服务器的资源分为俩种:静态和动态资源web服务器如何处理客户端的请求2.1cgi
- 表单是让用户与我们的网页应用程序交互的基本元素。Flask 本身并不会帮助我们处理表单,但是 Flask-WTF 扩展让我们在我们的 Fla
- js中应用正则表达式转换大小写,代码很简单,看代码:以下首字母大写,其它字母小写<script type="text/jav
- 本文程序针对Python选课系统进行开发,供大家参考,具体内容如下角色:学校、学员、课程、讲师要求:1. 创建北京、上海 2 所学校2. 创
- 我已安装了WINDOWS NT OPTON PACK 中的SMTP Service,请问如何使用它的发送邮件功能?在安装了SMTP Serv
- Python下实现定时任务的方式有很多种方式。下面介绍几种循环sleep:这是一种最简单的方式,在循环里放入要执行的任务,然后sleep一段