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