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頁面

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM