python编写prometheus exporter参考

import prometheus_client
from prometheus_client import Counter, Gauge
from prometheus_client.core import CollectorRegistry
from flask import Response, Flask
from psutil import virtual_memory
from psutil import cpu_times

app = Flask(__name__)

REGISTRY = CollectorRegistry(auto_describe=False)
mem_percent = Gauge(
    "system_memory_percent",
    "Total system memory percent.",
    registry=REGISTRY)
cpu_per = Gauge(
    "system_cpu_percent",
    "Total system cpu percent.",
    registry=REGISTRY)


@app.route('/metrics')
def r_value():
    mem_percent.set(virtual_memory().percent)
    cpu_per.set(cpu_times().system)
    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',debug=True)

请求metrics页面

查看prometheus页面

 

 

猜你喜欢

转载自www.cnblogs.com/z-ye/p/12741652.html