如何在prometheus中创建自定义指标?

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

我正在建立一个监控PoC以满足未来的需求。 PoC是在我的计算机上本地开发的。要监控指标我使用Prometheus和Grafana。我想计算收到的文件数和处理它的时间。为此,我需要创建自定义指标。

我正在使用python 2.7.5。现在我已经连接了普罗米修斯和目标。我收到指标但不知道如何创建我想要的指标。

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')


def thr():
    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)

我希望收到的文件总数“计算接收的文件数”度量标准。处理文件所花费的时间(即2s)和处理文件所花费的时间总和(50s)。

python prometheus metrics
1个回答
1
投票

您的用例不需要所有这些指标。只需在prometheus中注册summary指标,例如:

from prometheus_client import Summary
import time
# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
    """A dummy function that takes some time."""
    time.sleep(t)

然后你有request_processing_seconds_countrequest_processing_seconds_sum指标。

© www.soinside.com 2019 - 2024. All rights reserved.