获取警报处于某种状态的秒数

Getting the seconds an alert was in a state

给出如下所示的 ALERTS 查询:

timestamp(ALERTS{alertstate="firing"})

你怎么知道它开火了多少秒?

计算过去 hour/day/whatever 触发警报的秒数(或等效的时间百分比)非常简单:

sum_over_time(ALERTS[1h:1s])

或者,出于效率原因,分辨率较低:

sum_over_time(ALERTS[1h:10s]) * 10

弄清楚警报何时开始触发(或者等价地,自上次开始触发以来已经触发了多长时间)有点复杂:

ALERTS{alertstate="firing"}
  * ignoring(alertstate)
(
  time() - max_over_time(timestamp(ALERTS{alertstate="pending"})[1h:10s])
    or ignoring(alertstate)
  ALERTS{alertstate="firing"} * 3600
)

即自上次处于状态 "pending" 以来的时间当且仅当它当前正在开火。请注意,其中有一个 1h 范围和默认值 3600,这意味着它的最大值为 3600 秒。