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.
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":
There are two ways to accomplish your task:
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.sort_desc(
topk(10,
sum by(message) (
count_over_time(
{job="systemd-journal"} |~ `ERROR:` | regexp `(?P<message>ERROR:.*)`
[7d]))))
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.