使用千分尺、弹簧启动和普罗米修斯测量每秒请求数



我正在将微服务更新到 Spring boot 2,并将指标从 dropwizard 迁移到千分尺。我们使用普罗米修斯来存储指标,并使用 grafana 来显示它们。我想测量每秒对所有 URL 的请求。千分尺文档指出:

Timers are intended for measuring short-duration latencies, and the frequency of such events.

所以计时器似乎是完成这项工作的方式:

Timer.Sample sample = log ? Timer.start(registry)
//...code which executes request...
List<Tag> tags = Arrays.asList(
Tag.of("status", status),
Tag.of("uri", uri),
Tag.of("method", request.getMethod()));
Timer timer = Timer.builder(TIMER_REST)
.tags(tags)
.publishPercentiles(0.95, 0.99)
.distributionStatisticExpiry(Duration.ofSeconds(30))
.register(registry);
sample.stop(timer);

但它不会产生任何每秒速率,相反,我们有类似于以下内容的指标:

# TYPE timer_rest_seconds summary
timer_rest_seconds{method="GET",status="200",uri="/test",quantile="0.95",} 0.620756992
timer_rest_seconds{method="GET",status="200",uri="/test",quantile="0.99",} 0.620756992
timer_rest_seconds_count{method="GET",status="200",uri="/test",} 7.0
timer_rest_seconds_sum{method="GET",status="200",uri="/test",} 3.656080641
# HELP timer_rest_seconds_max  
# TYPE timer_rest_seconds_max gauge
timer_rest_seconds_max{method="GET",status="200",uri="/test",} 0.605290436

解决这个问题的正确方法是什么?每秒速率应该通过普罗米修斯查询计算还是通过弹簧致动器端点返回?

Prometheus 提供了一种称为 PromQL(Prometheus Query Language(的函数式查询语言,允许用户实时选择和聚合时间序列数据。你可以使用 rate(( 函数:

以下示例表达式返回过去 5 分钟内测量的 HTTP 请求的每秒速率,范围向量中的每个时间序列:

rate(http_requests_total{job="api-server"}[5m])

最新更新