在 Prometheus 和 Grafana 中对每小时收到的总请求进行建模

Modelling Total Requests received per hour in Prometheus and Grafana

我正在尝试使用 Grafana 和 Prometheus 计数器绘制每小时请求总数的图表。

所以我有一个计数器,它会在每次请求时递增 http_requests

我正在使用 increase(http_requests[60m]) 计算给定时刻 T 过去 60 分钟内的总请求数。

但这给了我一条趋势线,我希望得到一个直方图。

例如

10:00-11:00 - 100(按counter_value_at_11 - counter_value_at_10计算)

现在假设当前时间是 11:30,所以我希望通过给我 (count_now - count_at_11).

来获得存储桶 11:00-12:00 的计数

1.) 可以使用计数器对此类数据建模吗?

2.) 我愿意在 Prometheus 中使用其他指标类型,如果它们支持此类建模的话

直方图可以在Visualization -> Draw Modes中找到,有Bars toggle可以激活。

对于每小时的数据分桶,您可以在“查询”部分进行设置,方法是将最小步长值添加到“1h”。

Example

以下 PromQL 查询 returns per-hour 增加 http_requests 指标:

last_over_time(increase(http_requests[1h])[1h:1h])

此查询使用 subqueries functionality for wrapping increase() function into last_over_time() 函数。

returned 数字在过去移动了一个小时,例如它显示在接下来的一小时内 10:00 - 11:00 的计数器增加 - 11:00 - 12:00。可以通过在查询中添加 offset -1h 来删除此时间偏移:

last_over_time(increase(http_requests[1h] offset -1h)[1h:1h])

Prometheus 默认不支持负偏移,所以这个查询 returns negative offset is disabled, use --enable-feature=promql-negative-offset to enable it 错误,除非 Prometheus 以 --enable-feature=promql-negative-offset command-line 标志运行(顺便说一句,其他 Prometheus-like 系统,例如 VictoriaMetrics 支持开箱即用的负偏移量。

另请注意,Prometheus 在 increase() 函数中存在以下问题:

  • increase() 整数计数器可以 return 由于外推而得到小数结果。有关详细信息,请参阅 this issue
  • increase(http_requests[1h]) 没有考虑前一小时的最后一个原始样本与当前小时的第一个原始样本之间的计数器增加。有关详细信息,请参阅 this article and this comment。这可能会导致 increase() 个结果低于 slow-moving 个计数器。

根据此设计文档,这两个问题都将在 Prometheus 中得到修复。同时可以使用其他 Prometheus-like 系统,例如 VictoriaMetrics - 它们没有这些问题。