如何使用Python编写Prometheus监控
- 更新时间:2020-03-11 16:38:05
- 编辑:漕茂典
要使用python编写Prometheus监控,需要你先开启Prometheus集群。可以参考//www.jb51.net/article/148895.htm 安装。在python中实现服务器端。在Prometheus中配置请求网址,Prometheus会定期向该网址发起申请获取你想要返回的数据。
使用Python和Flask编写Prometheus监控
Installation
pip install flask pip install prometheus_client
Metrics
Prometheus提供4种类型Metrics:Counter
, Gauge
, Summary
和Histogram
Counter
Counter可以增长,并且在程序重启的时候会被重设为0,常被用于任务个数,总处理时间,错误个数等只增不减的指标。
import prometheus_client from prometheus_client import Counter from prometheus_client.core import CollectorRegistry from flask import Response, Flask app = 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 counter request_count 3.0
Gauge
Gauge与Counter类似,唯一不同的是Gauge数值可以减少,常被用于温度、利用率等指标。
import random import prometheus_client from prometheus_client import Gauge from flask import Response, Flask app = 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 gauge random_value 3.0
Summary/Histogram
Summary/Histogram概念比较复杂,一般exporter很难用到,暂且不说。
LABELS
使用labels来区分metric的特征
from prometheus_client import Counter c = 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()
使用Python和asyncio编写Prometheus监控
from prometheus_client import Counter, Gauge from prometheus_client.core import CollectorRegistry REGISTRY = 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)
import prometheus_client from prometheus_client import Counter,Gauge from prometheus_client.core import CollectorRegistry from aiohttp import web import aiohttp import asyncio import uvloop import random,logging,time,datetime asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) routes = web.RouteTableDef() # metrics包含 requests_total = Counter("request_count", "Total request cout of the host") # 数值只增 random_value = Gauge("random_value", "Random value of the request") # 数值可大可小 @routes.get('/metrics') async def metrics(request): requests_total.inc() # 计数器自增 # requests_total.inc(2) data = prometheus_client.generate_latest(requests_total) return web.Response(body = data,content_type="text/plain") # 将计数器的值返回 @routes.get("/metrics2") async def metrics2(request): random_value.set(random.randint(0, 10)) # 设置值任意值,但是一定要为 整数或者浮点数 return web.Response(body = prometheus_client.generate_latest(random_value),content_type="text/plain") # 将值返回 @routes.get('/') async def hello(request): return web.Response(text="Hello, world") # 使用labels来区分metric的特征 c = Counter('requests_total', 'HTTP requests total', ['method', 'clientip']) # 添加lable的key, c.labels('get', '127.0.0.1').inc() #为不同的label进行统计 c.labels('post', '192.168.0.1').inc(3) #为不同的label进行统计 c.labels(method="get", clientip="192.168.0.1").inc() #为不同的label进行统计 g = Gauge('my_inprogress_requests', 'Description of gauge',['mylabelname']) g.labels(mylabelname='str').set(3.6) #value自己定义,但是一定要为 整数或者浮点数 if __name__ == '__main__': logging.info('server start:%s'% datetime.datetime.now()) app = web.Application(client_max_size=int(2)*1024**2) # 创建app,设置最大接收图片大小为2M app.add_routes(routes) # 添加路由映射 web.run_app(app,host='0.0.0.0',port=2222) # 启动app logging.info('server close:%s'% datetime.datetime.now())
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对码农之家的支持。如果你想了解更多相关内容请查看下面相关链接
相关教程
-
python实现flappy bird小游戏
这篇文章主要为大家详细介绍了python实现flappy bird小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
发布时间:2019-06-03
-
python多进程执行方法apply_async使用说明
这篇文章主要介绍了python多进程执行方法apply_async使用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
发布时间:2021-05-02
-
编写高质量代码:改善Python程序的91个建议
大小:54.9 MBPython编码电子书
-
Python机器学习(第2版)
《Python机器学习》是一本关于Python的电子书资源,涉及Python、机器学习等相关内容,本文提供大小为198MB的超清第2PDF格式电子书下载,希望大家能够喜欢。
大小:59.6 MBPython电子书
-
Python可以这样学
《Python可以这样学》对Python内部工作原理进行了一定深度的剖析,案例代码使用Python 3.5.1实现和Python 3.6.0,适当介绍了Python代码优化、系统编程和安全编程的有关知识,满足不同层次读者的需要,适合作为Python程序员的开发指南。
大小:22 MBPython入门电子书
-
Python强化学习实战(含源码)
Python强化学习实战:应用OpenAI Gym和TensorFlow精通强化学习和深度强化学习 PDF+源码 增强学习是这种关键的深度学习方式 ,在智能化体及预测分析等行业有很多运用。这书共13章,包括增强学习的
大小:98.5 MBPython电子书
-
机器人Python极客编程入门与实战
大小:25.8MBPython编程电子书
-
Python数据科学:技术详解与商业实践
本书共19章,第1章介绍数据科学中涉及的基本领域;第2~3章介绍与数据工作紧密相关的Python语言基础;第4章讲解描述性统计分析在宏观业务领域的分析;
大小:154 MBPython电子书
-
零基础入门学习Python(含视频、源码)
本书是一本Python 3.7编程轻松入门教程,前半部分首先讲解基础的Python 3语法知识,后半部分则围绕着Python 3在爬虫、界面开发和游戏开发上的应用,循序渐进,欢迎下载
大小:16.8 MBPython电子书
-
Python语言程序设计基础
本书提出了以理解和运用计算生态为目标的Python语言教学思想,不仅系统讲解了Python语言语法,同时介绍了从数据理解到图像处理的14个Python函数库,向初学Python语言的读者展示了全新的编程语
大小:48.4 MBPython编程电子书
-
面向ArcGIS的Python脚本编程
Python作为一种高级程序设计语言,凭借其简洁、易读及可扩展性日渐成为程序设计领域备受推崇的语言。使用Python作为ArcGIS的脚本语言将大大提升ArcGIS数据处理的效率,更好地实现ArcGIS内部的
大小:49.9 MBPython电子书