欢迎光临散文网 会员登陆 & 注册

[示例]使用python编写prometheus_export

2020-02-24 16:09 作者:究极完美豆芽  | 我要投稿

环境:

python3.7

import subprocess
import prometheus_client
import time

request_count = prometheus_client.Counter('request_count', "启动后到现在收集了几数据")

job_count = prometheus_client.Gauge("job_count", "统计数量", ["user_name"])


# 执行命令
def popen(cmd):
   try:
       result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE,
                               encoding="utf-8", shell=True, )
       return result.stdout
   except Exception as e:
       print(e)
       # 返回-1时为出错
       return -1


def process_request(sleepTime):
   request_count.inc()  # 数值+1
   job_count.labels(user_name="root").set(11)
   job_count.labels(user_name="testuser").set(22)
   time.sleep(sleepTime)  # 睡眠


if __name__ == '__main__':
   prometheus_client.start_http_server(8080)
   print("启动完成")
   while True:
       process_request(2)  # 两秒一次收集一次数据


[示例]使用python编写prometheus_export的评论 (共 条)

分享到微博请遵守国家法律