How can i turn no data to zero in Loki

Hi all , Im looking for a way to turn the null/no data return to zero in logql. Im trying to get the percent of the request just like

sum(count_over_time({app="xxx"}|="{"|json|route="POST /api/a" , status=~`200|400|401|403|429|404` [1m])) / sum(count_over_time({app="xxx"}|="{"|json|route="POST /api/a" [1m]))

But when there is no 200|400|401|403|429|404 of /api/a the return of sum(count_over_time({app="xxx"}|="{"|json|route="POST /api/a" , status=~200|400|401|403|429|404 [1m])) is no data ,and i wanna turn it to zero. So the total result can be 0%

8 Likes

This is a good question, I’m not sure if this is currently possible but will ask the team for options here!

2 Likes

I am also interested in this. Any way to make a logql query return zero when theres no data ?

It’d be really helpful to get an answer on this. Now that we can alert based on Loki LogQL metric queries, sending alerts based on error logs seems like a common use case. But if your query returns no data when there are no error logs, it’s impossible to get your alert to work properly.

2 Likes

this is a useful feature, like the or on() vector(0) in prometheus query, which will help on alerting and also some calculation.

Any progress on it?

2 Likes

Came up with kinda ugly solution. Basic idea is that you should negate your regexp first, and than subtract this value from sum without conditions. Something like this:
sum(x) - sum ( x | code !~"^5…")

I agree that this is a useful feature.
For example if I display graph line of number of errors in logs - if there are none I want it to be displayed as 0 and that it won’t disappear from the graph.

I used a Grafana transformation which seems to work

Add field from calculation → Binary operation
Select the query and do + 0
I then hide the original query

It would be easier if we could do this in the original query though

Agreed! I opened a discussion on Github as well to follow this: How can i turn no data to zero in Loki · Discussion #39742 · grafana/grafana · GitHub

If we have an option to join with a vector of 0 and do a sum, would be good.

I would like this too!

[doc] logql: logql engine support exec vector(0) grama #7044

The transformation hack no longer appears to work.

I’ve arrived here looking for a solution. If anyone is too, nowadays is quite simple:
In grafana / Edit panel / Standard options / No value = 0

To handle cases in LogQL where a query returns null or “no data” and convert it to zero, you can use the or operator along with a conditional expression. Here’s how you can modify your query to achieve that:

Modified Query

logql

sum(count_over_time({app=“xxx”}|=“{”|json|route=“POST /api/a”, status=~“200|400|401|403|429|404”[1m])) or 0


Complete Percent Calculation

To ensure the entire percentage calculation correctly handles cases where the numerator might be zero (resulting in "no data"), you can wrap your entire division in an `or` statement:

logql
(sum(count_over_time({app="xxx"}|="{"|json|route="POST /api/a", status=~"200|400|401|403|429|404"[1m])) or 0) / 
(sum(count_over_time({app="xxx"}|="{"|json|route="POST /api/a"[1m])) or 1) * 100

1. **Handling No Data:** The `or 0` part ensures that if the `sum(count_over_time(...))` returns `no data`, it will instead return `0`.
2. **Preventing Division by Zero:** The second part of the division uses `or 1`. This means that if there are no requests at all (the denominator is `0`), it will default to `1` instead. This prevents division by zero, and if the numerator is also `0`, the entire expression will yield `0%`.
3. **Calculating Percentage:** Finally, multiplying by `100` at the end gives you the percentage value.

In Loki, you can use the fill() function to convert no data (null values) to zero. Apply it to your query like this:

scss

Copy code
sum(rate(your_query[1m])) or vector(0).
This will display zero where there’s no data.