使用python代码导出Prometheus指标

问题描述 投票:0回答:1

有人告诉我Kubernetes集群中是否有Python代码来收集Prometheus指标?我有3个与Kubernetes集群连接的节点,已经安装了Prometheus,并且所有节点都已使用Kubernetes集群建立并连接,我只想知道如何使用python代码导出这些指标?非常感谢

python kubernetes prometheus metrics
1个回答
1
投票

您可以遵循此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)
© www.soinside.com 2019 - 2024. All rights reserved.