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

原文地址:https://www.cnblogs.com/hixiaowei/p/13702733.html