I want to set an alert in Prometheus alertmanager that has to be triggered between a given time Eg: Condition to be checked between 09:15 to 15:30
Asked
Active
Viewed 3,948 times
1 Answers
4
Alerts in Prometheus are evaluated periodically, and you can't really set a schedule for them.
I think it can be acheived with some PromQL Kong Fu:
scalar(clamp(hour() > 9 and hour() < 15, 1, 1)) * <alert_promql>
hour() > 9 and hour() < 15
Define a range of time based on hour of the day (you can add minutes too)
clamp(..., 1, 1)
ensure that the value will be 1 and nothing else
*
- This is where the magic happens.
If we get any value from the previous function it will be 1 so multiplying by 1 has no effect on the second expression.
Otherwise, there is no series on the first expression, so the multiplication will return no results anyway.

aclowkay
- 3,577
- 5
- 35
- 66
-
1Important to note that `clamp()` was not introduced until 2.26 https://promlabs.com/blog/2021/04/01/whats-new-in-prometheus-2-26#clamping-an-input-vector-into-a-given-range – kellyfj May 03 '22 at 19:48
-
There's a highly relevant question in https://stackoverflow.com/questions/56427808/how-do-i-use-the-hour-function-of-promql which argues in favour of using `ON() (1 < hour() < 3)`. – GhostLyrics Nov 30 '22 at 01:00