I have the following prometheus query used for alerting: sum…
- User
I have the following prometheus query used for alerting: sum (rate(grpc_server_handled_total{app="myapp"}[1m])) < 100
For some reason it doesn't trigger if the value is zero, because it sees no data. How can I fix this?
- Assistant
You can use the
oroperator in Prometheus query to handle the case when there's no data. Combine it with theabsent()function to check if there's no data for your metric. Modify your query as follows:sum(rate(grpc_server_handled_total{app="myapp"}[1m])) < 100 or absent(grpc_server_handled_total{app="myapp"})This query will trigger when the sum of the rate is less than 100 or when there's no data for the
grpc_server_handled_total{app="myapp"}metric. - User
How does this differ from using or on() vector(0) after the original query?
- Assistant
Using
or on() vector(0)after the original query will also include a constant value 0 in the query result. This is useful when you want to fill any missing data with a default value (in this case, 0). Here is how the query would look like:sum(rate(grpc_server_handled_total{app="myapp"}[1m])) < 100 or on() vector(0)Both methods will handle the case when there's no data for your metric, and both can be used for alerting purposes. However, the main difference between these two approaches is in what they consider as the "default" value when there's no data:
- Using
absent(), the query will trigger the alert if there's no data for the metric in question, explicitly checking for the absence of the metric. - Using
or on() vector(0), the query will include a constant value of 0 and compare it against the condition, essentially treating "no data" as 0 and checking the condition based on that.
In this specific case, where you want to trigger an alert if the value is less than 100, both options will address the issue and trigger the alert if there's no data. However, using makes it clearer that you want to check for the absence of data, whereas using can be seen as an assumption that "no data" should be equivalent to a value of 0. Either one can be used, depending on your preferences.
- Using