当前位置: 首页 > article >正文

02. 上报自定义数据到 prometheus(使用 Python Client)

文章目录

  • pull 模式——prometheus 主动 pull 数据

pull 模式——prometheus 主动 pull 数据

  1. 安装 client
pip3 install prometheus-client
  1. prometheus 配置上报数据的 job
  - job_name: 'test_report_metrics'
    static_configs:
      - targets: ['localhost:9600']
    honor_labels: true

重启 prometheus,进入 Targets,看到新监控的节点。

  1. 创建一个 python 项目,内容如下:
import random
import time
from prometheus_client import Counter, start_http_server, Gauge

from prometheus.test import REQUEST_TIME

# 定义它需要2个参数,第一个是metrics的名字,第二个是metrics的描述信息
c = Counter('cc', 'A Counter')
g = Gauge('gg', 'A Gauge')


@REQUEST_TIME.time()
def report_metrics(t):
    # counter,只增不减
    c.inc()

    # gauge,任意值
    g.set(random.random())

    time.sleep(1)


if __name__ == '__main__':
    start_http_server(9600)
    while True:
        report_metrics(random.Random())

暴露端口 9600,并注册到 prometheus,即通过 pull 方式上报指标到 prometheus。

  1. 访问 http://localhost:9600/metrics 查看上报指标
    执行上面的 python 项目,前往 http://localhost:9600/metrics 查看上报指标

注:

  • 关于 Python client用法与原理可参考:https://yuerblog.cc/2019/01/03/prometheus-client-usage-and-principle/
  • Python client 官方文档:https://pypi.org/project/prometheus-client/

http://www.kler.cn/news/340804.html

相关文章:

  • 【C++】set和multiset(关联式容器、键值对,set和multiset的基本特性、主要用途及常用操作)
  • 搜维尔科技:Haption远程操作项目模拟项目
  • Spring Validation —— 参数校验框架
  • Linux系统中,文件和文件夹的权限和所有权核心概念
  • Window系统编程 - 文件操作
  • 国庆档不太热,影视股“凉”了?
  • Win10之Ubuntu22.04(主机)与Virtual-BOX(宿主win10)网络互通调试(七十九)
  • k8s 中存储之 PV 持久卷 与 PVC 持久卷申请
  • 实现std::sort,replace,fill,accumulate,equal等函数
  • MyBatis之TypeHandler的自定义实现
  • Golang | Leetcode Golang题解之第462题最小操作次数使数组元素相等II
  • GNU/Linux - tarball文件介绍介绍
  • C#中Json序列化的进阶用法
  • Spring中注入bean时的scope属性详解、往singleton中注入prototype属性的bean以及Spring使用注解实现AOP切面编程
  • qwt实现码流柱状图多色柱体显示
  • SAP将假脱机(Spool requests)内容转换为PDF文档[RSTXPDFT4]
  • GAMES202作业3
  • 27-云计算下一个十年技术Serverless
  • 阿里140滑块-滑块验证码逆向分析思路学习
  • class 031 位运算的骚操作