使用 prometheus python 库编写自定义指标的方法(完整代码)


Posted in Python onJune 29, 2020

虽然 prometheus 已有大量可直接使用的 exporter 可供使用,以满足收集不同的监控指标的需要。例如,node exporter 可以收集机器 cpu,内存等指标,cadvisor 可以收集容器指标。然而,如果需要收集一些定制化的指标,还是需要我们编写自定义的指标。

本文讲述如何使用 prometheus python 客户端库和 flask 编写 prometheus 自定义指标。

安装依赖库

我们的程序依赖于flask 和prometheus client 两个库,其 requirements.txt 内容如下:

flask==1.1.2
prometheus-client==0.8.0

运行 flask

我们先使用 flask web 框架将 /metrics 接口运行起来,再往里面添加指标的实现逻辑。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask
app = Flask(__name__)

@app.route('/metrics')
def hello():
 return 'metrics'

if __name__ == '__main__':
 app.run(host='0.0.0.0', port=5000)

打开浏览器,输入 http://127.0.0.1:5000/metrics,按下回车后浏览器显示 metrics 字符。

编写指标

Prometheus 提供四种指标类型,分别为 Counter,Gauge,Histogram 和 Summary。

Counter

Counter 指标只增不减,可以用来代表处理的请求数量,处理的任务数量,等。

可以使用 Counter 定义一个 counter 指标:

counter = Counter('my_counter', 'an example showed how to use counter')

其中,my_counter 是 counter 的名称,an example showed how to use counter 是对该 counter 的描述。

使用 counter 完整的代码如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask, Response
from prometheus_client import Counter, generate_latest
app = Flask(__name__)
counter = Counter('my_counter', 'an example showed how to use counter')

@app.route('/metrics')
def hello():
 counter.inc(1)
 return Response(generate_latest(counter), mimetype='text/plain')

if __name__ == '__main__':
 app.run(host='0.0.0.0', port=5000)

访问 http://127.0.0.1:5000/metrics,浏览器输出:

# HELP my_counter_total an example showed how to use counter
# TYPE my_counter_total counter
my_counter_total 6.0
# HELP my_counter_created an example showed how to use counter
# TYPE my_counter_created gauge
my_counter_created 1.5932468510424378e+09

在定义 counter 指标时,可以定义其 label 标签:

counter = Counter('my_counter', 'an example showed how to use counter', ['machine_ip'])

在使用时指定标签的值:

counter.labels('127.0.0.1').inc(1)

这时浏览器会将标签输出:

my_counter_total{machine_ip="127.0.0.1"} 1.0

Gauge

Gauge 指标可增可减,例如,并发请求数量,cpu 占用率,等。

可以使用 Gauge 定义一个 gauge 指标:

registry = CollectorRegistry()
gauge = Gauge('my_gauge', 'an example showed how to use gauge', ['machine_ip'], registry=registry)

为使得 /metrics 接口返回多个指标,我们引入了 CollectorRegistry ,并设置 gauge 的 registry 属性。

使用 set 方法设置 gauge 指标的值:

gauge.labels('127.0.0.1').set(2)

访问 http://127.0.0.1:5000/metrics,浏览器增加输出:

# HELP my_gauge an example showed how to use gauge
# TYPE my_gauge gauge
my_gauge{machine_ip="127.0.0.1"} 2.0

Histogram

Histogram 用于统计样本数值落在不同的桶(buckets)里面的数量。例如,统计应用程序的响应时间,可以使用 histogram 指标类型。

使用 Histogram 定义一个 historgram 指标:

buckets = (100, 200, 300, 500, 1000, 3000, 10000, float('inf'))
histogram = Histogram('my_histogram', 'an example showed how to use histogram', ['machine_ip'], registry=registry, buckets=buckets)

如果我们不使用默认的 buckets,可以指定一个自定义的 buckets,如上面的代码所示。

使用 observe() 方法设置 histogram 的值:

histogram.labels('127.0.0.1').observe(1001)

访问 /metrics 接口,输出:

# HELP my_histogram an example showed how to use histogram
# TYPE my_histogram histogram
my_histogram_bucket{le="100.0",machine_ip="127.0.0.1"} 0.0
my_histogram_bucket{le="200.0",machine_ip="127.0.0.1"} 0.0
my_histogram_bucket{le="300.0",machine_ip="127.0.0.1"} 0.0
my_histogram_bucket{le="500.0",machine_ip="127.0.0.1"} 0.0
my_histogram_bucket{le="1000.0",machine_ip="127.0.0.1"} 0.0
my_histogram_bucket{le="3000.0",machine_ip="127.0.0.1"} 1.0
my_histogram_bucket{le="10000.0",machine_ip="127.0.0.1"} 1.0
my_histogram_bucket{le="+Inf",machine_ip="127.0.0.1"} 1.0
my_histogram_count{machine_ip="127.0.0.1"} 1.0
my_histogram_sum{machine_ip="127.0.0.1"} 1001.0
# HELP my_histogram_created an example showed how to use histogram
# TYPE my_histogram_created gauge
my_histogram_created{machine_ip="127.0.0.1"} 1.593260699767071e+09

由于我们设置了 histogram 的样本值为 1001,可以看到,从 3000 开始,xxx_bucket 的值为 1。由于只设置一个样本值,故 my_histogram_count 为 1 ,且样本总数 my_histogram_sum 为 1001。
读者可以自行试验几次,慢慢体会 histogram 指标的使用,远比看网上的文章理解得快。

Summary

Summary 和 histogram 类型类似,可用于统计数据的分布情况。

定义 summary 指标:

summary = Summary('my_summary', 'an example showed how to use summary', ['machine_ip'], registry=registry)

设置 summary 指标的值:

summary.labels('127.0.0.1').observe(randint(1, 10))

访问 /metrics 接口,输出:

# HELP my_summary an example showed how to use summary
# TYPE my_summary summary
my_summary_count{machine_ip="127.0.0.1"} 4.0
my_summary_sum{machine_ip="127.0.0.1"} 16.0
# HELP my_summary_created an example showed how to use summary
# TYPE my_summary_created gauge
my_summary_created{machine_ip="127.0.0.1"} 1.593263241728389e+09

附:完整源代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from random import randint
from flask import Flask, Response
from prometheus_client import Counter, Gauge, Histogram, Summary, \
 generate_latest, CollectorRegistry
app = Flask(__name__)
registry = CollectorRegistry()
counter = Counter('my_counter', 'an example showed how to use counter', ['machine_ip'], registry=registry)
gauge = Gauge('my_gauge', 'an example showed how to use gauge', ['machine_ip'], registry=registry)
buckets = (100, 200, 300, 500, 1000, 3000, 10000, float('inf'))
histogram = Histogram('my_histogram', 'an example showed how to use histogram',
  ['machine_ip'], registry=registry, buckets=buckets)
summary = Summary('my_summary', 'an example showed how to use summary', ['machine_ip'], registry=registry)

@app.route('/metrics')
def hello():
 counter.labels('127.0.0.1').inc(1)
 gauge.labels('127.0.0.1').set(2)
 histogram.labels('127.0.0.1').observe(1001)
 summary.labels('127.0.0.1').observe(randint(1, 10))
 return Response(generate_latest(registry), mimetype='text/plain')

if __name__ == '__main__':
 app.run(host='0.0.0.0', port=5000)

参考资料

https://github.com/prometheus/client_python
https://prometheus.io/docs/concepts/metric_types/
https://prometheus.io/docs/instrumenting/writing_clientlibs/
https://prometheus.io/docs/instrumenting/exporters/
https://pypi.org/project/prometheus-client/
https://prometheus.io/docs/concepts/metric_types/
http://www.coderdocument.com/docs/prometheus/v2.14/best_practices/histogram_and_summary.html
https://prometheus.io/docs/practices/histograms/

总结

到此这篇关于使用 prometheus python 库编写自定义指标的文章就介绍到这了,更多相关prometheus python 库编写自定义指标内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python操作列表的函数使用代码详解
Dec 28 Python
PyTorch的深度学习入门之PyTorch安装和配置
Jun 27 Python
解决安装python3.7.4报错Can''t connect to HTTPS URL because the SSL module is not available
Jul 31 Python
使用Django搭建web服务器的例子(最最正确的方式)
Aug 29 Python
python:动态路由的Flask程序代码
Nov 22 Python
python调用c++返回带成员指针的类指针实例
Dec 12 Python
如何基于Python + requests实现发送HTTP请求
Jan 13 Python
Python单链表原理与实现方法详解
Feb 22 Python
Django单元测试中Fixtures用法详解
Feb 25 Python
在 Pycharm 安装使用black的方法详解
Apr 02 Python
Python restful框架接口开发实现
Apr 13 Python
python各种excel写入方式的速度对比
Nov 10 Python
使用keras时input_shape的维度表示问题说明
Jun 29 #Python
在Keras中CNN联合LSTM进行分类实例
Jun 29 #Python
使用keras实现BiLSTM+CNN+CRF文字标记NER
Jun 29 #Python
Python建造者模式案例运行原理解析
Jun 29 #Python
解决Keras中循环使用K.ctc_decode内存不释放的问题
Jun 29 #Python
Python根据指定文件生成XML的方法
Jun 29 #Python
keras在构建LSTM模型时对变长序列的处理操作
Jun 29 #Python
You might like
PHP 替换模板变量实现步骤
2009/08/24 PHP
php采用ajax数据提交post与post常见方法总结
2014/11/10 PHP
php计算整个目录大小的方法
2015/06/01 PHP
PHP实现的简单网络硬盘
2015/07/29 PHP
分享php邮件管理器源码
2016/01/06 PHP
php简单备份与还原MySql的方法
2016/05/09 PHP
php插入mysql数据返回id的方法
2018/05/31 PHP
javascript之对系统的toFixed()方法的修正
2007/05/08 Javascript
Javascript 日期处理之时区问题
2009/10/08 Javascript
IE6下focus与blur错乱的解决方案
2011/07/31 Javascript
jquery自动填充勾选框即把勾选框打上true
2014/03/24 Javascript
使用 js+正则表达式为关键词添加链接
2014/11/11 Javascript
javascript实现跨域的方法汇总
2015/06/25 Javascript
Javascript仿新浪游戏频道鼠标悬停显示子菜单效果
2015/08/21 Javascript
ANGULARJS中使用JQUERY分页控件
2015/09/16 Javascript
JS基于面向对象实现的多个倒计时器功能示例
2017/02/28 Javascript
jQuery遮罩层实例讲解
2017/05/11 jQuery
判断文字超过2行添加展开按钮,未超过则不显示,溢出部分显示省略号
2019/04/28 Javascript
前端vue-cli项目中使用img图片和background背景图的几种方法
2019/11/13 Javascript
jQuery实现滑动开关效果
2020/08/02 jQuery
[02:25]DOTA2英雄基础教程 虚空假面
2014/01/02 DOTA
深入解析Python中的变量和赋值运算符
2015/10/12 Python
Python argv用法详解
2016/01/08 Python
python爬虫系列Selenium定向爬取虎扑篮球图片详解
2017/11/15 Python
Python读取JSON数据操作实例解析
2020/05/18 Python
Python中关于logging模块的学习笔记
2020/06/03 Python
Python判断字符串是否为合法标示符操作
2020/09/03 Python
python中使用asyncio实现异步IO实例分析
2021/02/26 Python
英国办公用品商店:Office Outlet
2018/04/04 全球购物
学校党委副书记个人对照检查材料思想汇报
2014/09/28 职场文书
教师党员自我剖析材料
2014/09/29 职场文书
学习雷锋精神活动总结
2015/02/06 职场文书
深入浅出的讲解:信号调制到底是如何实现的
2022/02/18 无线电
中国古风插画师排行榜:夏达第一,第三是阴阳师姑获鸟皮肤创作者
2022/03/18 国漫
DE1107机评
2022/04/05 无线电
SQL Server中搜索特定的对象
2022/05/25 SQL Server