时间:2021-05-22
要使用python编写Prometheus监控,需要你先开启Prometheus集群。可以参考//www.jb51.net/article/148895.htm 安装。在python中实现服务器端。在Prometheus中配置请求网址,Prometheus会定期向该网址发起申请获取你想要返回的数据。
使用Python和Flask编写Prometheus监控
Installation
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.0Gauge
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.0Summary/Histogram
Summary/Histogram概念比较复杂,一般exporter很难用到,暂且不说。
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()使用Python和asyncio编写Prometheus监控
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)import prometheus_clientfrom prometheus_client import Counter,Gaugefrom prometheus_client.core import CollectorRegistryfrom aiohttp import webimport aiohttpimport asyncioimport uvloopimport random,logging,time,datetimeasyncio.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())总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
1.Prometheus介绍Prometheus是一套开源的系统监控报警框,相比Nagios或者Zabbix拥有如下优点1.1易管理性Prometheus:Pr
1.Prometheus是什么Prometheus是一个具有活跃生态系统的开源系统监控和告警工具包。一言以蔽之,它是一套开源监控解决方案。Prometheus主
本文介绍SpringBoot如何使用Prometheus配合Grafana监控。1.关于PrometheusPrometheus是一个根据应用的metrics来
一、prometheus基本原理介绍prometheus是基于metric采样的监控,可以自定义监控指标,如:服务每秒请求数、请求失败数、请求执行时间等,每经过
本文介绍SpringBoot使用蚂蚁金服SOFA-Lookout配合Prometheus进行监控。1.SOFA-Lookout介绍上一篇已经介绍使用Promet