如何使用 PromQL 的 hour() 函数?

Lav*_*air 6 prometheus promql prometheus-alertmanager

我正在尝试在 Prometheus 的alert.rules 文件中设置一个警报,该警报仅在特定时间段内触发。我已经在 expr 标签内测试了下面的代码块,没有时间限制,并且它工作得非常好。
正如PromQL 文档:hour()所述,hour()根据当前 UTC 返回 0 到 23 之间的值。

- alert: test_down 
        expr: absent(container_memory_usage_bytes{name="test_ap"}) and hour() > 5 and hour() < 22
        for: 30s
        labels:
          severity: critical
        annotations:
          summary: "test_ap down"
          description: "test_ap is down for more than 30 seconds."
Run Code Online (Sandbox Code Playgroud)

但在这里,不会触发任何警报通知。有谁知道为什么没有任何东西被解雇以及我该如何解决这个问题?

编辑:我已经解决了。我不明白为什么我必须像我这样做的方式那样做,但以下方法有效:
替换and hour() > 5 and hour() < 22and ON() hour() > 5 < 22

小智 9

在本例中,ON() 它是连接操作,它忽略左侧部分的匹配标签。否则,普罗米修斯会期望左侧和右侧有相同的标签集。您可以在这篇博文中阅读更多内容。

  • 我在 https://prometheus.io/docs/prometheus/latest/querying/operators/ 的 Prometheus 查询文档中没有看到任何提及此运算符的信息。您有记录此“ON()”的链接吗?谢谢! (4认同)