Flask使用prometheus+grafana

本文介绍如何使用Python和Flask框架实现与Prometheus监控系统的集成。主要内容包括:编写Flask应用来收集请求计数并暴露给Prometheus,配置Prometheus以定期抓取这些指标,以及如何在Grafana中展示这些数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 程序编写

  1. 导包
from flask import Flask,Response
import prometheus_client
from prometheus_client import Counter
  1. 在global编写变量
requests_total_get_answer=Counter('get_answer_total','Get answer requested.')
  1. 在请求内部递增变量
requests_total_get_answer.inc()
  1. 写prometheus接口
@app.route('/metrics')
def requests_count():
    return Response(prometheus_client.generate_latest(requests_total_get_answer),mimetype="text/plain")

二. 配置prometheus

配置prometheus.yml

  - job_name: 'flask_smart_system' # Prometheus 任务名称,自定义
    metrics_path: '/metrics' # 指标获取路径
    scrape_interval: 5s # 抓取指标的间隔时间
    static_configs:
       - targets: ['localhost:5000']

三. 配置grafana

prometheus查询语句为rate(get_answer_total[$__rate_interval]),故新建dashboard-新建panal,添加变量即可。

四. 参考

使用Python和Flask编写Prometheus监控_JackLiu16的博客-CSDN博客

prometheus定义counter指标_蜜蜜不吃糖的博客-CSDN博客_counter prometheus

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值