== Grafana's problem with the order of dashboard panel legends and Prometheus [[I tweeted something a bit obscure https://twitter.com/thatcks/status/1568028419312746498]]: > Although it's purely to deal with a missing Grafana feature, I do wish > that Prometheus had some way to sort the returned query results by the > value of some label. (I could hope for Grafana to add some way to deal > with this, but good luck with that.) It's common to build Grafana dashboards with graph panels (including bar graphs) that have multiple things in them. Generally when you do this, you include a legend with labels. The legend labels come from the query or queries you make, as covered in [[the query options for Prometheus as a datasource https://grafana.com/docs/grafana/latest/datasources/prometheus/#options]] (the same is true for Loki as a datasource). If each separate thing on the panel comes from a separate query, it's easy to put the legend labels in any order that you want, because the legend order comes from the order of the queries, and Grafana lets you freely re-order them. You might have this if you're graphing various memory metrics from a Linux server, for example, because [[the Prometheus host agent https://github.com/prometheus/node_exporter]] exposes each different thing from /proc/meminfo as a separate metric (and some things aren't exposed directly anyway, such as the amount of swap space used; you get only 'total swap' and 'free swap'). However, often you generate multiple time series from a single query and given them legend labels based on the value of some label from the metric; for example, all disk writes are together under one metric name, ``node_disk_written_bytes_total'', and it has a 'device' label. In this case, ~~the order of legend labels is the order that Prometheus returns all the time series in~~. This is not necessarily the order that you actually want (although I believe it's stable), and also isn't necessarily the same between different queries about the same things (so, for example, different queries about different disk metrics may return results in different order, resulting in graphs with different order of disks in the legend). (I don't think Prometheus documents the order that query results are returned in. Experimentally, as of the current 2.38.0, Prometheus seems to order the labels alphabetically by name and then sort alphabetically by the resulting combination of label names and label values.) Grafana provides no feature to change this order, although it could probably easily offer a 'sort by label name (ascending/descending)' option in the query options, or perhaps offer it as a general transformation. Prometheus currently provides no documented feature to control the order that time series are returned in based on label values, which isn't surprising since it mostly doesn't make any promises about the order in the first place. Prometheus does provide [[sort() https://prometheus.io/docs/prometheus/latest/querying/functions/#sort]] and [[``sort_desc()'' https://prometheus.io/docs/prometheus/latest/querying/functions/#sort_desc]] to sort the time series by value. Although this can be useful to force an order, there can be three issues with it. The first is that it's generally not going to be consistent from panel to panel in the same dashboard. Second, the order can change when the dashboard refreshes (or when you explicitly look at a different time range). And third, I'm not sure how this interacts with actual graphs, as opposed to 'value over entire time interval' bar graphs, since the order may be different at every different step across the range.