Search code examples
grafanagrafana-lokilogqlgrafana-dashboard

How to Sort Values in Descending Order in a Grafana Stat Dashboard with a Loki Query?


I am currently using a Loki query to fetch the top 10 error messages from my systems and display them on a Grafana stat dashboard. The query I am using is:

topk(10, sum by(message)(count_over_time({job="systemd-journal"} |~ `ERROR:` | regexp `(?P<message>ERROR:.*)` [7d])))

The output I receive successfully retrieves the top 10 error messages, but the values are not sorted in any particular order, as can be seen in the attached screenshot. Dashboard Settings

I expected the topk function to automatically sort the values in a descending order, but that doesn't seem to be happening. I have looked through the Grafana and Loki documentation but could not find a straightforward way to sort the values within the stat panel in Grafana. I am seeking a method or a query adjustment that would enable me to display these values sorted in a descending order.

To provide more information on the transformation drop down "Sort by": Drop Down 1


Solution

  • There are two ways to accomplish your task:

    1. Sort using logql. You can use functions sort and sort_desc over your query, to provided a sorted result to Grafana to visualize. AFAIK, Grafana preserves order of data in dataset unless instructed otherwise.
      You query will be
    sort_desc(
     topk(10,
          sum by(message) (
            count_over_time(
              {job="systemd-journal"} |~ `ERROR:` | regexp `(?P<message>ERROR:.*)` 
              [7d]))))
    
    1. You can use Transformation Sort by on your panel. It will sort data based on selected column (in your case something like Value #A I believe)

    For this approach to work you need to set Type of query (under query itself in collapsible menu) to Instant. Also if you'll see a single datapoint on panel after that, in panel options: Value options>Show select All values.