python开发prometheus exporter

本文介绍了如何使用Python的prometheus_client库创建自定义的监控指标,包括Counter和Gauge类型的Metrics。通过示例展示了如何启动HTTP服务器暴露指标,并在Prometheus中配置自定义exporter以收集这些指标。
摘要由CSDN通过智能技术生成

Prometheus提供4种类型Metrics:Counter, Gauge, Summary和Histogram

  • Counter可以增长,并且在程序重启的时候会被重设为0,常被用于任务个数,总处理时间,错误个数等只增不减的指标。
  • Gauge与Counter类似,唯一不同的是Gauge数值可以减少,常被用于温度、利用率等指标。
  • Summary/Histogram概念比较复杂,对于我来说目前没有使用场景,暂无了解。

当Prometheus自带的exporter无法满足实际需求时,需要我们自定义开发监控项
参考
Prometheus-使用python开发exporter
prometheus自定义监控指标——实战
python Django 实现自定义prometheus export

一、简单的示例

1、安装prometheus_client

pip install prometheus_client

2、简单示例

import time
from prometheus_client import Gauge,start_http_server

#custom_test_metric{ labelkey1="labelvalue1",labelkey2="labelvalue2" } 123.0
#指标名:custom_test_metric   标签:labelkey1,labelkey2
g = Gauge('custom_test_metric', 'Description of gauge', ['labelkey1','labelkey2'])

if __name__ == '__main__':
    start_http_server(8006)  # 8006端口启动
    while True:
        #将上一次的值去除
        g.clear()
        #设置标签以及监控数据的值
        g.labels(labelkey1="labelvalue1",labelkey2="labelvalue2").set("123")
        time.sleep(10)

3、访问ip:8086端口,custom_test_metric{labelkey1="labelvalue1",labelkey2="labelvalue2"} 123.0
在这里插入图片描述

4、如果要返回多个指标,新增Gauge实例再传值即可

import time
from prometheus_client import Gauge,start_http_server

g = Gauge('custom_test_metric', 'Description of gauge', ['labelkey1','labelkey2'])
h = Gauge('custom_test_metric2', 'Description of gauge', ['labelkey1','labelkey2'])

if __name__ == '__main__':
    start_http_server(8006)  # 8006端口启动
    while True:
        g.labels(labelkey1="labelvalue1",labelkey2="labelvalue2").set("123")
        h.labels(labelkey1="labelvalue1", labelkey2="labelvalue2").set("124")
        time.sleep(10)

再访问端口,可以看到有新增的指标了
在这里插入图片描述

5、在prometheus中配置自定义的exporter

vim /usr/local/prometheus/prometheus.yml

增加一个job_name

  - job_name: 'exporter-python'
    static_configs:
    - targets: ['10.0.0.107:8006']

在这里插入图片描述

然后重启prometheus

systemctl restart prometheus.service

访问prometheus查询页面进行查询,可以查到我们刚才自定义的两个指标
在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Prometheus exporter是一种将应用程序指标公开为Prometheus所能够接受的格式的服务。它是由Prometheus社区维护的开放源代码软件。可以使用各种编程语言和库来编写Exporter,以便在Prometheus上监视应用程序的所有方面。Exporter将应用程序的度量标准转换为Prometheus所需的格式,使得Prometheus能够定期抓取度量标准,进行存储和查询。以下是使用Python编写Prometheus exporter的一个例子: ```python from prometheus_client import start_http_server, Metric, REGISTRY import random import time class CustomCollector(object): def __init__(self): pass def collect(self): # 模拟获取应用程序指标 metric = Metric('custom_metric', 'Custom metric description', 'gauge') metric.add_sample('custom_metric', value=random.randint(0, 10), labels={}) yield metric if __name__ == '__main__': start_http_server(8000) REGISTRY.register(CustomCollector()) while True: time.sleep(1) ``` 上述Python代码中,我们定义了一个名为CustomCollector的类来生成我们自己的指标。稍后,我们将该类注册到Prometheus的默认注册表中。在collect()函数中,我们模拟生成一个名为custom_metric的指标。我们使用gauge类型来定义它,并添加了一些随机值。在主函数中,我们启动了一个HTTP服务器,监听端口号8000。然后,我们注册我们的CustomCollector类,并定期地生成一些随机的度量标准,以便Prometheus可以抓取和存储。最后,我们进入了一个无限循环,防止程序退出,以便我们可以继续生成度量标准。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值