Python实战:为Prometheus开发自定义Exporter

在当今的微服务架构和容器化部署环境中,监控系统的重要性不言而喻。Prometheus作为一款开源的系统监控和警报工具,以其强大的功能和灵活性受到了广泛的欢迎。然而,Prometheus本身并不直接监控所有类型的服务或应用,这就需要我们为其开发自定义的Exporter。本文将带你走进实战,了解如何使用Python为Prometheus开发一个自定义的Exporter。

1. Prometheus Exporter基础

在Prometheus的架构中,Exporter负责从目标系统中抓取监控数据,并通过HTTP接口以特定的格式(通常是文本格式)暴露给Prometheus。Prometheus定期从这些Exporter的HTTP端点抓取数据,并进行存储、分析和警报。

2. 准备工作

在开始编写代码之前,你需要确保你的环境中已经安装了Python和必要的库。我们将使用prometheus_client库来生成Prometheus可以理解的监控数据。

你可以通过pip安装:

pip install prometheus_client

3. prometheus_client常见指标

  • Gauge(仪表盘):表示一个可以任意上下波动的度量,例如内存用量或队列中的项目数。
from prometheus_client import start_http_server, Gauge

gauge = Gauge('example_gauge', 'An example gauge')

gauge.set(123.45)  # 设置一个固定值
  • Counter(计数器):表示一个单向递增的计数器,通常用来统计请求的数量或处理的字节数。
from prometheus_client import Counter

counter = Counter('example_counter', 'An example counter')

counter.inc()  # 递增计数器
counter.inc(10) # 递增计数器,数值为10
  • Histogram(直方图):用于统计观察值的分布情况,例如请求的响应时间。
from prometheus_client import Histogram

histogram = Histogram('example_histogram', 'An example histogram')

with histogram.time():  # 用于记录代码块的执行时间
pass # 模拟一些操作
  • Summary(摘要):用于记录观察值的摘要信息,例如请求的响应大小。
from prometheus_client import Summary

summary = Summary('example_summary', 'An example summary')

summary.observe(123.4)  # 观察一个值
  • Info(信息):记录关于目标的信息,通常不用于监控,但可以用来记录软件版本等静态信息。
from prometheus_client import Info

info = Info('example_info', 'An example info')

info.info({'version': '1.2.3'})  # 记录信息
  • GaugeHistogram(仪表盘直方图):用于记录一段时间内观察值的直方图。
from prometheus_client import GaugeHistogram

gauge_histogram = GaugeHistogram('example_gauge_histogram', 'An example gauge histogram')

with gauge_histogram.time():  # 记录代码块的执行时间
pass # 模拟一些操作
  • Enum(枚举):用于记录有限数量的标签值。
from prometheus_client import Enum

enum = Enum('example_enum', 'An example enum', ['value1', 'value2'])

enum.labels('value1')  # 选择一个枚举值

为了使 Prometheus 抓取这些指标,你需要提供一个 HTTP 服务端点。prometheus_client 库已经包含了一个简单的 HTTP 服务器,可以自动为你的指标提供 /metrics 端点。

from prometheus_client import start_http_server

if __name__ == '__main__':
start_http_server(8000)
# 你的指标收集和更新逻辑

4.示例

from prometheus_client import start_http_server, Gauge, Counter, Summary, Histogram, Info
import time
import random a = Gauge('a', 'Description of gauge')
b = Gauge('b', 'Description of gauge',['label1', 'label2'])
c = Counter('c', 'Description of counter')
s = Summary('request_latency_seconds', 'Description of summary')
h = Histogram('request_latency_seconds2', 'Description of histogram')
i = Info('my_build_version', 'Description of info') def process_request():
"""A dummy function that takes some time."""
a.set(random.randint(1,100))
b.labels(label1='k1', label2='f1').set(random.randint(100, 200))
b.labels(label1='k1', label2='f2').set(random.randint(200, 300))
b.labels(label1='k2', label2='f2').set(random.randint(400, 500))
c.inc()
s.observe(4.7)
h.observe(8.8)
i.info({'version': '1.2.3', 'buildhost': 'foo@bar'})
time.sleep(1) if __name__ == '__main__':
# 启动 HTTP 服务器,默认监听在 8000 端口
start_http_server(8000) # 循环处理请求
while True:
process_request()

执行后,访问8000端口:

# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 83.0
python_gc_objects_collected_total{generation="1"} 305.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable objects found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 41.0
python_gc_collections_total{generation="1"} 3.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="10",patchlevel="11",version="3.10.11"} 1.0
# HELP a Description of gauge
# TYPE a gauge
a 61.0
# HELP b Description of gauge
# TYPE b gauge
b{label1="k1",label2="f1"} 113.0
b{label1="k1",label2="f2"} 233.0
b{label1="k2",label2="f2"} 401.0
# HELP c_total Description of counter
# TYPE c_total counter
c_total 4.0
# HELP c_created Description of counter
# TYPE c_created gauge
c_created 1.7274026703582432e+09
# HELP request_latency_seconds Description of summary
# TYPE request_latency_seconds summary
request_latency_seconds_count 4.0
request_latency_seconds_sum 18.8
# HELP request_latency_seconds_created Description of summary
# TYPE request_latency_seconds_created gauge
request_latency_seconds_created 1.7274026703582432e+09
# HELP request_latency_seconds2 Description of histogram
# TYPE request_latency_seconds2 histogram
request_latency_seconds2_bucket{le="0.005"} 0.0
request_latency_seconds2_bucket{le="0.01"} 0.0
request_latency_seconds2_bucket{le="0.025"} 0.0
request_latency_seconds2_bucket{le="0.05"} 0.0
request_latency_seconds2_bucket{le="0.075"} 0.0
request_latency_seconds2_bucket{le="0.1"} 0.0
request_latency_seconds2_bucket{le="0.25"} 0.0
request_latency_seconds2_bucket{le="0.5"} 0.0
request_latency_seconds2_bucket{le="0.75"} 0.0
request_latency_seconds2_bucket{le="1.0"} 0.0
request_latency_seconds2_bucket{le="2.5"} 0.0
request_latency_seconds2_bucket{le="5.0"} 0.0
request_latency_seconds2_bucket{le="7.5"} 0.0
request_latency_seconds2_bucket{le="10.0"} 4.0
request_latency_seconds2_bucket{le="+Inf"} 4.0
request_latency_seconds2_count 4.0
request_latency_seconds2_sum 35.2
# HELP request_latency_seconds2_created Description of histogram
# TYPE request_latency_seconds2_created gauge
request_latency_seconds2_created 1.7274026703582432e+09
# HELP my_build_version_info Description of info
# TYPE my_build_version_info gauge
my_build_version_info{buildhost="foo@bar",version="1.2.3"} 1.0

参考资料

https://www.5axxw.com/wiki/content/p095bn

https://blog.csdn.net/nujnus9221/article/details/139009361

Python实战:为Prometheus开发自定义Exporter的更多相关文章

  1. Golang 基于Prometheus Node_Exporter 开发自定义脚本监控

    Golang 基于Prometheus Node_Exporter 开发自定义脚本监控 公司是今年决定将一些传统应用从虚拟机上迁移到Kubernetes上的,项目多而乱,所以迁移工作进展缓慢,为了建立 ...

  2. React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发

    React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发   2016/09/23 |  React Native技术文章 |  Sky丶清|  4 条评论 |  1 ...

  3. 基于Flask框架的Python web程序的开发实战 <一> 环境搭建

    最近在看<Flask Web开发基于Python的Web应用开发实战>Miguel Grinberg著.安道译 这本书,一步步跟着学习Flask框架的应用,这里做一下笔记 电脑只安装一个P ...

  4. 学习参考《Flask Web开发:基于Python的Web应用开发实战(第2版)》中文PDF+源代码

    在学习python Web开发时,我们会选择使用Django.flask等框架. 在学习flask时,推荐学习看看<Flask Web开发:基于Python的Web应用开发实战(第2版)> ...

  5. Python 3网络爬虫开发实战》中文PDF+源代码+书籍软件包

    Python 3网络爬虫开发实战>中文PDF+源代码+书籍软件包 下载:正在上传请稍后... 本书书籍软件包为本人原创,在这个时间就是金钱的时代,有些软件下起来是很麻烦的,真的可以为你们节省很多 ...

  6. Python 3网络爬虫开发实战中文 书籍软件包(原创)

    Python 3网络爬虫开发实战中文 书籍软件包(原创) 本书书籍软件包为本人原创,想学爬虫的朋友你们的福利来了.软件包包含了该书籍所需的所有软件. 因为软件导致这个文件比较大,所以百度网盘没有加速的 ...

  7. Python 3网络爬虫开发实战中文PDF+源代码+书籍软件包(免费赠送)+崔庆才

    Python 3网络爬虫开发实战中文PDF+源代码+书籍软件包+崔庆才 下载: 链接:https://pan.baidu.com/s/1H-VrvrT7wE9-CW2Dy2p0qA 提取码:35go ...

  8. 《Python 3网络爬虫开发实战中文》超清PDF+源代码+书籍软件包

    <Python 3网络爬虫开发实战中文>PDF+源代码+书籍软件包 下载: 链接:https://pan.baidu.com/s/18yqCr7i9x_vTazuMPzL23Q 提取码:i ...

  9. Python 3网络爬虫开发实战书籍

    Python 3网络爬虫开发实战书籍,教你学会如何用Python 3开发爬虫   本书介绍了如何利用Python 3开发网络爬虫,书中首先介绍了环境配置和基础知识,然后讨论了urllib.reques ...

  10. Python自动化运维开发实战 一、初识Python

    导语 都忘记是什么时候知道python的了,我是搞linux运维的,早先只是知道搞运维必须会shell,要做一些运维自动化的工作,比如实现一些定时备份数据啊.批量执行某个操作啊.写写监控脚本什么的. ...

随机推荐

  1. highcharts中的仪表盘样式

    仪表盘的样式如下: 是双表盘展示 左边的图中minorTickInterval的值为null,右边的minorTickInterval的值为"auto" 左边的图中lineColo ...

  2. PHP开源项目之YOURLS

    YOURLS是一个开源的PHP的程序,可以利用它来构建属于自己的URL缩短服务,YOURLS还可以集成到WordPress博客中使用. YOURLS 的主要功能: 公开的(Public 任何人都可以用 ...

  3. 使用 BenchmarkDotNet 对 .NET 代码进行性能基准测试

    前言 在软件开发领域,性能基准测试是确保软件系统高效.稳定运行的重要环节.它可以帮助你评估应用程序的性能,了解其在不同条件下的响应时间.吞吐量.资源利用率等.通过基准测试,你可以确定系统在处理特定工作 ...

  4. 利用 word VBA 将投标文件偏离参数表列数据拷贝至技术偏差表中

    使用 vba 将正偏离参数表的第一列信息复制粘贴至对应的技术偏离表的第4列中.需要同时打开两个 word 文件,在技术偏差表中打开 VBE(可以用ctrl + f11 快捷键),插入模块. 忽略格式的 ...

  5. AtCoder Beginner Contest 378

    Contest Link 还得加练. A & B & C & D 不具备任何思维含量. Submission A Submission B Submission C Submi ...

  6. Ubuntu实现SSH外网连接内网(反向隧道)

    应用场景: 如果你有Linux云主机(腾讯.华为等),且公司有一台只有内网IP (或动态IP) 的Linux工作机:你计划在家里工作时,通过家里的电脑连接公司的工作机 (且不想使用类似Teamview ...

  7. DevEco Studio:状态管理与事件处理

    DevEco Studio:状态管理与事件处理 引言 在现代前端开发中,状态管理和事件处理是构建交互式应用的核心.DevEco Studio,作为华为推出的开发环境,支持开发者使用TypeScript ...

  8. word常规操作

    为何写标 招标: A公司要买100台电脑 [需求] 投标: 电脑公司看到招标后,就会投标:自我介绍(公司,产品,售后) [自我介绍满足需求] 中标: A公司选择XX公司 [选择] 保密价格内容 不能透 ...

  9. cas3.5配置LDAP域控

    一. 安装cas3.5 点击下载 CAS 3.5.2 :CAS 解压缩下载的 cas-server-3.5.2-release.zip,在 %CAS%\modules文件夹中找到cas-server- ...

  10. 【Python】【爬虫】【爬狼】001_urllib_get_获取响应结果页面代码

    情况说明 本节课我们要处理的网站是 www.yhdmp.cc 注意:腾讯报毒该网址.问题不大,基本这种盗版动漫的网站都会报毒吧.如果不放心可以自己找个其他的网站爬,我这个也是随便找的. 该网站搜索提交 ...