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()
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?
Prometheus Grafana Templating order by count - Stack Overflow
Extracting variables from Prometheus labels, sorting by value - Configuration - Grafana Labs Community Forums
prometheus - Sort in promql such that all metrics with one lebel appear first in result - Stack Overflow
grafana doesn't sort results returned by prometheus correctly.
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()
Use sort_by_label and sort_by_label_desc functions. For example, the following query sorts time series returned by query by metric names:
sort_by_label(query, "__name__")
The __name__ refers to metric name. See these docs for more details.
Try using sort_by_label function from VictoriaMetrics (I'm the author of this Prometheus-like system) in the following way:
sort_by_label({__name__=~"foo|bar|baz"}, "__name__")
It should sort the returned time series by their metric names.
If you need to sort time series with idenrical metric names by their value, then try the following MetricsQL query:
sort_by_label(
sort({__name__=~"foo|bar|baz"}),
"__name__"
)
It uses an additional function - sort - for sorting time series by values, before sorting time series by metric names.
You can use the operator "or" to join the PromQLs, like the following:
{__name__=~"cpu_usage_value} or {__name__=~"memory_usage_value"}
This will join the two metrics listing the "cpu_usage_value" elements first and then the "memory_usage_vale" ones.
But...
As shown in the "or" operator documentation:
vector1 or vector2 results in a vector that contains all original elements (label sets + values) of vector1 and additionally all elements of vector2 which do not have matching label sets in vector1
Prometheus will list only the elements of the "memory_usage_value" metric which do not have matching label sets in the "cpu_usage_value" one.
To workaround this, add an extra label to the "memory_usage_value" metric, using the "label_replace" function, like the following:
{__name__=~"cpu_usage_value} or label_replace({__name__=~"memory_usage_value"}, "foo", "boo", "", "")