如何使用 PromQL 的 hour() 函数?
How do I use the hour() function of PromQL?
我正在尝试在 Prometheus 的 alert.rules 文件中设置一个仅在特定时间段触发的警报。
我已经在 expr-tag 内测试了下面的代码块,没有时间限制,它工作得很好。
正如 PromQL Documentation: hour() 所述,hour()
returns 一个介于 0 和 23 之间的值,具体取决于当前的 UTC。
- 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."
但是在这里,没有触发任何警报通知。有谁知道,为什么什么都没有被解雇,我该如何解决?
编辑: 我已经解决了。我不明白为什么我必须像我这样做的方式那样做,但以下工作:
将 and hour() > 5 and hour() < 22
替换为 and ON() hour() > 5 < 22
在这种情况下,ON() 是连接操作,它会忽略左侧匹配的标签。否则,Prometheus 会期望左右两侧有相同的标签集。你可以阅读更多 in this blogpost。
我正在尝试在 Prometheus 的 alert.rules 文件中设置一个仅在特定时间段触发的警报。
我已经在 expr-tag 内测试了下面的代码块,没有时间限制,它工作得很好。
正如 PromQL Documentation: hour() 所述,hour()
returns 一个介于 0 和 23 之间的值,具体取决于当前的 UTC。
- 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."
但是在这里,没有触发任何警报通知。有谁知道,为什么什么都没有被解雇,我该如何解决?
编辑: 我已经解决了。我不明白为什么我必须像我这样做的方式那样做,但以下工作:
将 and hour() > 5 and hour() < 22
替换为 and ON() hour() > 5 < 22
在这种情况下,ON() 是连接操作,它会忽略左侧匹配的标签。否则,Prometheus 会期望左右两侧有相同的标签集。你可以阅读更多 in this blogpost。