flask - exporter -gauge - python - 请求时间显示 + 自定义监控-pushgateway-flask

1.显示请求使用时间-使用gauge显示每次的请求时间

复制代码
[root@VM_0_111_centos exporters]# cat request_time.py |egrep -v '^#|^$'
import prometheus_client
from prometheus_client import Counter, Gauge
from prometheus_client.core import CollectorRegistry
from flask import Response, Flask
import pycurl
app = Flask(__name__)
REGISTRY = CollectorRegistry(auto_describe=False)
http_request_time = Gauge(
    "myhttp_request_time",
    "use gauge to demostrate how muny time the request.",
    registry=REGISTRY)
@app.route('/metrics')
def r_value():
    c = pycurl.Curl()
    c.setopt(c.URL, '10.0.0.111:55555/a.txt')
    c.setopt(c.CONNECTTIMEOUT, 3)
    c.setopt(c.TIMEOUT, 3)
    try:
        c.perform()
    except pycurl.error:
        http_code = 500
        http_total_time = 999
    else:
        http_code = c.getinfo(pycurl.HTTP_CODE)
        http_total_time = c.getinfo(pycurl.TOTAL_TIME)
    http_request_time.set(http_total_time)
    return Response(prometheus_client.generate_latest(REGISTRY),
                    mimetype="text/plain")
@app.route('/')
def index():
    return "Hello, Prometheus!"
if __name__ == "__main__":
    app.run(host='0.0.0.0',port='29091',debug=True)
复制代码

复制:https://www.cnblogs.com/z-ye/p/12741652.html

           https://blog.csdn.net/specter11235/article/details/87927202

 

3.以上作用是在本地生成一个小型网站,下一步是将选定的数据发送到pushgateway 

https://www.cnblogs.com/zqj-blog/p/11106724.html  prometheus自定义监控指标——实战

posted @   littlevigra  阅读(393)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
历史上的今天:
2019-09-20 kubernetes 简单的例子 - sidecar service deployment hello world nginx
2018-09-20 Docker 修改已有镜像(转)
点击右上角即可分享
微信分享提示