Posts

Showing posts with the label Grafana

Handling 'no data' in Grafana subtraction query using LogQL with Loki

 In Grafana with Loki, you can handle 'no data' cases when performing subtraction queries using LogQL. Subtraction queries allow you to calculate the difference between two metrics or log stream counts. When dealing with 'no data' scenarios, you might want to set a default value or display a meaningful result. Here's how you can approach it: Suppose you have two LogQL queries: `queryA` and `queryB`. To handle 'no data' situations, you can use the `if` function to check if one or both of these queries result in 'no data' and assign a default value in such cases. Here's a general template: ```LogQL if(   <condition_for_no_data>,   <default_value>,   <subtraction_query> ) ``` For example, if you want to subtract `queryA` from `queryB` and display '0' when there's 'no data' in either query, you can use the following expression: ```LogQL if(   (count_over_time(queryA[5m]) == 0) or (count_over_time(queryB[5m]) == 0