使用Python和Flask编写Prometheus监控
来源:互联网 发布:笑傲江湖ol 知乎 编辑:程序博客网 时间:2024/06/12 06:41
使用Python和Flask编写Prometheus监控
Installation
pip install flaskpip install prometheus_python
Metrics
Prometheus提供4种类型Metrics:Counter
, Gauge
, Summary
和Histogram
Counter
Counter可以增长,并且在程序重启的时候会被重设为0,常被用于任务个数,总处理时间,错误个数等只增不减的指标。
import prometheus_clientfrom prometheus_client import Counterfrom prometheus_client.core import CollectorRegistryfrom flask import Response, Flaskapp = Flask(__name__)requests_total = Counter("request_count", "Total request cout of the host")@app.route("/metrics")def requests_count(): requests_total.inc() # requests_total.inc(2) return Response(prometheus_client.generate_latest(requests_total), mimetype="text/plain")@app.route('/')def index(): requests_total.inc() return "Hello World"if __name__ == "__main__": app.run(host="0.0.0.0")
运行改脚本,访问youhost:5000/metrics
# HELP request_count Total request cout of the host# TYPE request_count counterrequest_count 3.0
Gauge
Gauge与Counter类似,唯一不同的是Gauge数值可以减少,常被用于温度、利用率等指标。
import randomimport prometheus_clientfrom prometheus_client import Gaugefrom flask import Response, Flaskapp = Flask(__name__)random_value = Gauge("random_value", "Random value of the request")@app.route("/metrics")def r_value(): random_value.set(random.randint(0, 10)) return Response(prometheus_client.generate_latest(random_value), mimetype="text/plain")if __name__ == "__main__": app.run(host="0.0.0.0")
运行改脚本,访问youhost:5000/metrics
# HELP random_value Random value of the request# TYPE random_value gaugerandom_value 3.0
Summary/Histogram
Summary/Histogram概念比较复杂,一般exporter很难用到,暂且不说。
PLUS
LABELS
使用labels来区分metric的特征
from prometheus_client import Counterc = Counter('requests_total', 'HTTP requests total', ['method', 'clientip'])c.labels('get', '127.0.0.1').inc()c.labels('post', '192.168.0.1').inc(3)c.labels(method="get", clientip="192.168.0.1").inc()
REGISTRY
from prometheus_client import Counter, Gaugefrom prometheus_client.core import CollectorRegistryREGISTRY = CollectorRegistry(auto_describe=False)requests_total = Counter("request_count", "Total request cout of the host", registry=REGISTRY)random_value = Gauge("random_value", "Random value of the request", registry=REGISTRY)
Reference
- prometheus
- client_python
- prometheus_client_example
Example
kafka-logsize-exporter
一个使用Prometheus监控Kafka logsize的练手案例
阅读全文
0 0
- 使用Python和Flask编写Prometheus监控
- Springboot使用 prometheus监控
- Prometheus监控工具初使用
- 使用Prometheus监控服务器性能
- 使用prometheus + Hawkular 监控你的应用
- 使用Prometheus+Grafana 监控MySQL/MONGODB
- Python flask使用templates 和static文件
- Python flask使用templates 和static文件
- 使用 Python 和 Flask 设计 RESTful API
- 使用 Python 和 Flask 设计 RESTful API
- 使用 Python 和 Flask 设计 RESTful API
- python使用flask和bootstrap制作网站
- 使用Prometheus监控Kafka logsize(kafka-logsize-exporter)
- Prometheus 系统监控方案
- Prometheus 监控Rabbitmq
- kubernetes监控--Prometheus
- Prometheus 系统监控方案
- 监控利器之 Prometheus
- configure: error: Cannot find libmysqlclient under /usr
- 如何解决sql注入的问题
- [组合数]求组合数的几种方法总结
- 简单的思维导图怎么画
- Mybatis开端
- 使用Python和Flask编写Prometheus监控
- Ubuntu开机报错:could not update ICEauthority file /home/user/.ICEauthority
- Linux初学
- 复习计划(第一次筛选)
- centos X64下安装php5.5.6
- IP地址验证和排序
- Android Studio 使用kotlin建立第一个KotlinActivity配置
- 重建二叉树
- centos安装