使用 python 代码导出 Prometheus 指标
Export Prometheus metrics using python code
有人告诉我是否有 python 代码可以在 Kubernetes 集群中收集 Prometheus 指标?我有 3 个节点与 Kubernetes 集群连接,Prometheus 已经安装并且所有节点都已启动并使用 Kubernetes 集群连接,我只想知道如何使用 python 代码导出这些指标?非常感谢
您可以按照此 guide and this guide. Essentially you will be using 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)
有人告诉我是否有 python 代码可以在 Kubernetes 集群中收集 Prometheus 指标?我有 3 个节点与 Kubernetes 集群连接,Prometheus 已经安装并且所有节点都已启动并使用 Kubernetes 集群连接,我只想知道如何使用 python 代码导出这些指标?非常感谢
您可以按照此 guide and this guide. Essentially you will be using 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)