As far as I know, You can only use the function sort() to sort metrics by value.

  • According to this PR, Prometheus does not intend to provide the function sort_by_label().
  • According to this Issue, Grafana displays the query results from Prometheus without sorting.
  • According to this Issue, Grafana supports sorting by value when displaying legend.

In Grafana 7, Prometheus metrics can be transformed from time series format to table format using the Transform module, so that you can sort the metrics by any label or value.


In December 2023, prometheus v2.49 finally added sort_by_label() and sort_by_label_desc()

Answer from LeoHsiao on Stack Overflow
🌐
Promlabs
training.promlabs.com › training › understanding-promql › advanced-querying › sorting-and-topk-bottomk
Understanding PromQL | Prometheus Trainings by PromLabs
In the Table view of the Prometheus expression browser, it is often useful to sort the output series by their sample value. You can achieve this by using the sort() (ascending sort) and sort_desc() (descending sort) functions. For example, to show all demo service request rates sorted by…
🌐
Reddit
reddit.com › r/grafana › prometheus sort by help (screenshot to explain)
r/grafana on Reddit: Prometheus sort by help (screenshot to explain)
December 4, 2020 -

Hello,

I have this table in Grafana and the data is perfect but I can get it to sort by instance name, if fact I'm not sure what method it's using to sort. If I enable prometheus at the bottom in blue all the data is correct but the instance order messes up, if I turn it off they are all in order.

The first column is the instance with server01 to server15, I need it to sort in numerical order, is this possible?

Discussions

Prometheus Grafana Templating order by count - Stack Overflow
I am trying to put a dropdown for each API end point which will show the QPS and Latency of http requests (RED metrics). I used Grafana's templating and used the following prometheus query. More on stackoverflow.com
🌐 stackoverflow.com
Extracting variables from Prometheus labels, sorting by value - Configuration - Grafana Labs Community Forums
I’ve got a couple of related questions regarding variables and sorting. I’d like to display a fairly large set of single-stat panels, using repeat. These are essentially red/green panels based on the value of a Prometheus query (green = 0, red = > 0). To improve the usability of this, I’d ... More on community.grafana.com
🌐 community.grafana.com
0
September 4, 2018
prometheus - Sort in promql such that all metrics with one lebel appear first in result - Stack Overflow
It uses an additional function - sort - for sorting time series by values, before sorting time series by metric names. ... Sign up to request clarification or add additional context in comments. ... Save this answer. ... Show activity on this post. You can use the operator "or" to join the PromQLs, ... More on stackoverflow.com
🌐 stackoverflow.com
grafana doesn't sort results returned by prometheus correctly.
The following query works well on the prometheus query GUI, the records are always displayed in descending order, sort_desc(sum(rate(my_call_count[1h])*3600) by (tenant)) · But it doesn't work as expected when the above query is used in the grafana table. The data displayed on grafana table ... More on github.com
🌐 github.com
3
December 14, 2017
🌐
KodeKloud Notes
notes.kodekloud.com › promql › functions
Functions - KodeKloud
$ sort(node_filesystem_avail_bytes) node_filesystem_avail_bytes{device="gvfsd-fuse", fstype="fuse.gvfsd-fuse", instance="node1"} 0 node_filesystem_avail_bytes{device="tmpfs", fstype="tmpfs", instance="node1"} 5238784 node_filesystem_avail_bytes{device="/dev/sda2", fstype="vfat", instance="node1"} 531341312 node_filesystem_avail_bytes{device="tmpfs", fstype="tmpfs", instance="node1"} 725422080 node_filesystem_avail_bytes{device="tmpfs", fstype="tmpfs", instance="node1"} 726319104 node_filesystem_avail_bytes{device="tmpfs", fstype="tmpfs", instance="node1"} 726319104 node_filesystem_avail_bytes{
🌐
SigNoz
signoz.io › guides › how to sort legends by series name in prometheus and grafana
How to Sort Legends by Series Name in Prometheus and Grafana | SigNoz
September 24, 2024 - For small to medium-sized datasets, the impact is negligible. However, sorting large datasets with many series can potentially impact rendering time. Yes. You can use PromQL functions like sort() and sort_desc() to control the order of data before it reaches Grafana in case of instant queries.
🌐
Google Groups
groups.google.com › g › prometheus-users › c › J39e5hGxFY4
Sort values in grafana
Also, whatever the sorting returned by Prometheus is, the position of a line in a graph is only dependent on the x/y values of its points (unless you stack, then it could affect the ordering of which line is stacked ontop of another).
🌐
HatchJS
hatchjs.com › home › promql sort by value: how to order time series data by their values
PromQL Sort by Value: How to Order Time Series Data by Their Values
January 5, 2024 - To use PromQL sort by value, you can use the `sort` function with the `by` parameter. The `by` parameter takes a list of expressions, and the results of the query will be sorted by the first expression in the list. You can also use the `desc` keyword to sort the results in descending order. For example, the following query will sort the results of the `up` metric by the value of the `value` label in descending order:
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › functions
Query functions | Prometheus
Same as sort_by_label, but sorts in descending order. sqrt(v instant-vector) calculates the square root of all float samples in v. Histogram samples in the input vector are ignored silently. This function has to be enabled via the feature flag --enable-feature=promql-experimental-functions.
Find elsewhere
🌐
Logz.io
logz.io › home › blog › how to › an intro to promql: basic concepts & examples
An Intro to PromQL: Basic Concepts & Examples | Logz.io
September 2, 2023 - You can also grab similar metrics by type, grouping them together in a PromQL query to display them together. It makes things far easier to view, without switching between the two. Similarly, PromQL lets you group multiple labels together, then sorting according to those groupings.
🌐
Grafana
grafana.com › blog › inside-promql-a-closer-look-at-the-mechanics-of-a-prometheus-query
Inside PromQL: A closer look at the mechanics of a Prometheus query | Grafana Labs
October 9, 2024 - A set of date/time functions, such as month, year, and timestamp, that either work the same as above or, if given no parameter, operate on the current time that PromQL is looking at. Histogram functions that only work on native histogram samples. Functions that operate on a range vector, like rate, max_over_time, and resets. These compute a result from the whole set of points in the time range window for each series. label_join and label_replace, which work on series and not on samples, and are handled specially. Sorting functions that take an instant vector and sort by either values or labels.
🌐
Google Groups
groups.google.com › g › prometheus-developers › c › 5T1BDDGynUg
topk() sorting
January 15, 2021 - I wonder though when topk() returns time series how does it sort result ? It looks like it is sorting by one of the labels or all of them. Is it possible to tell it some way to sort it some way to put the highest series on top ? Here is example what I get with Grafana showing MySQL commands ...
🌐
Grafana
community.grafana.com › configuration
Extracting variables from Prometheus labels, sorting by value - Configuration - Grafana Labs Community Forums
September 4, 2018 - I’ve got a couple of related questions regarding variables and sorting. I’d like to display a fairly large set of single-stat panels, using repeat. These are essentially red/green panels based on the value of a Prometheus query (green = 0, red = > 0). To improve the usability of this, I’d ...
🌐
MetricFire
metricfire.com › blog › getting-started-with-promql
Getting started with PromQL | MetricFire
September 13, 2023 - 9. Frequently, in the case of many labels, it is necessary to correctly group and aggregate them. PromQL has built-in functionality for this issue. Let's demonstrate it using the same metric and rate() calculation as in the previous example. As we can see, the resulting data is sorted by the corresponding categories in the query.
🌐
GitHub
github.com › grafana › grafana › issues › 10208
grafana doesn't sort results returned by prometheus correctly. · Issue #10208 · grafana/grafana
December 14, 2017 - The following query works well on the prometheus query GUI, the records are always displayed in descending order, sort_desc(sum(rate(my_call_count[1h])*3600) by (tenant)) But it doesn't work as expected when the above query is used in th...
Author: grafana
🌐
Grafana
community.grafana.com › prometheus
Grafana did not display the records in the expected order in table - Prometheus - Grafana Labs Community Forums
December 29, 2017 - I created a table on grafana, and the metrics for the table is as below, sort_desc(avg(my_test_count) by (tenantid)) I expect that the records should be displayed in decending order based on the metrics value, but actu…
🌐
GitHub
github.com › grafana › grafana › issues › 6561
Prometheus topk and sort_desc provides additional metrics · Issue #6561 · grafana/grafana
November 14, 2016 - I'm submitting a : Bug Report Grafana Version: 3.1.1 DataSource: Prometheus (v. 1.3.1) OS: Ubuntu 16.04 LTS Query: sort_desc(topk(5, bsnAPIfNoOfUsers)) Expected Result: Top 5 Access Points by Stations Associated, Sorted from Top to Bottom Result: Pie Chart visualizes anywhere from 5 - 13 Access Points in the list, Table Lists All APs
Author: grafana
🌐
Medium
valyala.medium.com › promql-tutorial-for-beginners-9ab455142085
PromQL tutorial for beginners and humans | by Aliaksandr Valialkin | Medium
September 15, 2023 - PromQL tutorial for beginners and humans PromQL is a query language for Prometheus monitoring system. It is designed for building powerful yet simple queries for graphs, alerts or derived time series …