PromQL Query Using Hour Function

1,462 views
Skip to first unread message

Chad Thielen

unread,
Feb 8, 2021, 4:33:51 PM2/8/21
to Prometheus Users
Hello,

I'm having some trouble writing a query for an alerting rule that we only want to run at certain times of the day. This is what I original had, which works, but not in the way we now need:

> count(up{job="calculator"} == 0) and count(hour(vector(time())) >= 1 and hour(vector(time())) < 13)

Now our problem is that we need the instance label from the up metric to persist to the alert so we can include it in the pager duty incident, otherwise our engineers don't know what instance is down without checking prometheus first. This is where I'm having problems, using count strips all those labels and just basically gives a true or false of if the alert is firing.

Any suggestions?

Chad Thielen

unread,
Feb 9, 2021, 10:49:40 AM2/9/21
to Prometheus Users
I managed to figure this out. The key is to use the on() function which ignores the labels. The query now looks like this:

up{job="calculator"} == 0 and on() count(hour(vector(time())) >= 1 and hour(vector(time())) < 13)

Reply all
Reply to author
Forward
0 new messages