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%


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


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

1 Like

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.


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?


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

1 Like

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