首页 > 解决方案 > 千分尺和普罗米修斯计时器作为速率

问题描述

根据 Micrometer 的文档https://micrometer.io/docs/concepts#_server_side,框架(Micrometer)应处理将计时器指标从绝对量转换为速率

下面的代码模拟了一个虚拟计时器:

@Service
public class AppService {
    private Timer timer = Metrics.timer("foobar");

    public String test() {
        timer.record(() -> {
            try {
                Thread.sleep((long) (Math.random() * 1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        return "foo";
    }
}

但是在 Prometheus 中,我只看到单调增加的指标foobar_seconds_sumfoobar_seconds_count而不是将它们视为速率

在此处输入图像描述

也许我误解或忽略了文档中的某些内容?

标签: prometheusspring-boot-actuatormicrometer

解决方案


这就是 Prometheus Summary 的工作方式,您可以使用以下方法计算平均事件大小:

  rate(foobar_seconds_sum[5m])
/
  rate(foobar_seconds_count[5m])

使用 Prometheus 的客户端库是愚蠢的,应该将更复杂的数学运算留给 Prometheus。


推荐阅读