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

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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值