1d 的时间总和,限制为最后 7d,计数器作为值
Sum over time of 1d, limit to last 7d, counter as a value
假设我有指标 purchases_total。这是一个计数器(不断增加)。
我想在 Grafana 中创建一个 table,其中:
- 显示最近 7 天
- 超过 1d 的总和
我尝试进行此查询,但它returns 毫无意义:
sum_over_time(sum(increase(purchases_total{deviceType='ios'}[2m])/2)[7d:1d])
P.S 2m 是一个抓取间隔。此外,我将 1d 的 "Min step" 放入查询配置(在图例输入字段和分辨率输入字段之间)以限制 table 视图(在 Grafana 中)。
任何建议将不胜感激!谢谢
您的问题不是 100% 清楚,但我认为您正在寻找 sum(increase(purchases_total{deviceType='ios'}[1d])
然后使用 query_range API 和 start/end 的 1d 步骤涵盖7天。
以下 PromQL 查询应 return 前一天的购买数量:
last_over_time(
increase(purchases_total{deviceType='ios'}[1d])[1d:1d]
)
它使用 subquery feature over last_over_time and increase 函数。需要外部 last_over_time(...[1d:1d])
以便将计算与 UTC 时区中的日期之间的边界对齐。
查询 returns 结果在过去移动了一天。这可以通过在查询中添加 offset -1d
来解决:
last_over_time(
increase(purchases_total{deviceType='ios'}[1d] offset -1d)[1d:1d]
)
另请注意,由于外推,Prometheus 可能 return 来自 increase()
的整数计数器的小数结果。参见 this issue for details. Additionally, Prometheus ignores the difference between the last sample on the previous day and the first sample on the current day when calculating increase()
. This may lead to incorrect results for slow-changing counters. See this comment and this article for details. Both issues should be addressed soon by Prometheus developers according to this design doc。
同时可以使用 VictoriaMetrics,它提供 increase()
功能而没有这些问题。
假设我有指标 purchases_total。这是一个计数器(不断增加)。 我想在 Grafana 中创建一个 table,其中:
- 显示最近 7 天
- 超过 1d 的总和
我尝试进行此查询,但它returns 毫无意义:
sum_over_time(sum(increase(purchases_total{deviceType='ios'}[2m])/2)[7d:1d])
P.S 2m 是一个抓取间隔。此外,我将 1d 的 "Min step" 放入查询配置(在图例输入字段和分辨率输入字段之间)以限制 table 视图(在 Grafana 中)。
任何建议将不胜感激!谢谢
您的问题不是 100% 清楚,但我认为您正在寻找 sum(increase(purchases_total{deviceType='ios'}[1d])
然后使用 query_range API 和 start/end 的 1d 步骤涵盖7天。
以下 PromQL 查询应 return 前一天的购买数量:
last_over_time(
increase(purchases_total{deviceType='ios'}[1d])[1d:1d]
)
它使用 subquery feature over last_over_time and increase 函数。需要外部 last_over_time(...[1d:1d])
以便将计算与 UTC 时区中的日期之间的边界对齐。
查询 returns 结果在过去移动了一天。这可以通过在查询中添加 offset -1d
来解决:
last_over_time(
increase(purchases_total{deviceType='ios'}[1d] offset -1d)[1d:1d]
)
另请注意,由于外推,Prometheus 可能 return 来自 increase()
的整数计数器的小数结果。参见 this issue for details. Additionally, Prometheus ignores the difference between the last sample on the previous day and the first sample on the current day when calculating increase()
. This may lead to incorrect results for slow-changing counters. See this comment and this article for details. Both issues should be addressed soon by Prometheus developers according to this design doc。
同时可以使用 VictoriaMetrics,它提供 increase()
功能而没有这些问题。