python - 如何在普罗米修斯中创建自定义指标?

标签 python prometheus metrics

我正在为 future 的需求设置一个监控 PoC。 PoC 是在我的计算机上本地开发的。为了监控指标,我使用 Prometheus 和 Grafana。
我想计算收到的文件数量和处理它所花费的时间。为此,我需要创建自定义指标。

我正在使用 python 2.7.5。现在我已经链接了 Prometheus 和目标。我收到指标但不知道如何创建我想要的指标。

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)

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

最佳答案

您的用例不需要所有这些指标。只需注册 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可用的指标。

关于python - 如何在普罗米修斯中创建自定义指标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55626390/

相关文章:

prometheus - Prometheus 时间序列中的实际时间戳与抓取时间戳

monitoring - 结合 2 个不同实例的状态配置 Prometheus 警报规则

python-3.x - PIL 逊线性系数keras

python - 掩码值如何影响 Keras 中的指标?

python - Tkinter - 如何使用停止按钮停止循环?

python函数随机循环if语句

python - 检测视频中穿着红色的人

python - 使用sklearn在python中进行时空克里金法?

kubernetes - 了解 Kubernetes 中 pod 的当前内存利用率

java - Spring 执行器自定义指标(平均、最小、最大响应时间)