使用python代码导出Prometheus度量

2024-04-26 22:18:33 发布

您现在位置:Python中文网/ 问答频道 /正文

有人告诉我Kubernetes集群中是否有收集普罗米修斯度量的python代码吗?我有3个节点与Kubernetes集群连接,Prometheus已经安装,所有节点都使用Kubernetes集群连接,我只想知道如何使用python代码导出这些度量?非常感谢


Tags: 代码节点度量集群kubernetesprometheus
1条回答
网友
1楼 · 发布于 2024-04-26 22:18:33

您可以跟随这个guide和这个guide。基本上,您将使用prometheus python client library导出度量

import prometheus_client as prom
import random
import time

req_summary = prom.Summary('python_my_req_example', 'Time spent processing a request')


@req_summary.time()
def process_request(t):
   time.sleep(t)


if __name__ == '__main__':

   counter = prom.Counter('python_my_counter', 'This is my counter')
   gauge = prom.Gauge('python_my_gauge', 'This is my gauge')
   histogram = prom.Histogram('python_my_histogram', 'This is my histogram')
   summary = prom.Summary('python_my_summary', 'This is my summary')
   prom.start_http_server(8080)

   while True:
       counter.inc(random.random())
       gauge.set(random.random() * 15 - 5)
       histogram.observe(random.random() * 10)
       summary.observe(random.random() * 10)
       process_request(random.random() * 5)

       time.sleep(1)

相关问题 更多 >