Search code examples
grafanapromqlgrafana-templating

Order repeated panels by metric value


I have a dashboard with a repeated panel, this panel is a single value panel that shows the time till expiration of SSL certificates using the probe_ssl_earliest_cert_expiry metric from Blackbox Exporter.

probe_ssl_earliest_cert_expiry{instance=~"$instance"} - time()

It works, but the panels aren't ascending, neither descending. What I'd like is these panels to be ordered ascending so that SSL certificates that are earliest to expire are listed first.

I tried using sort_desc(-(probe_ssl_earliest_cert_expiry{instance=~"$instance"} - time())) to get this to work but it didn't, the panels were still unsorted.

Update
I realised that Grafana sorts the repeating panels by the order that the $instance template variable is in that the panel is repeated by.

I used the probe_success metric for this template variable but I now use the probe_ssl_earliest_cert_expiry metric for this as well.

I feel like I'm a step closer, but using numerical sorting on this template variable doesn't seem to properly order based on the metric value.

Is there a way to do this in Grafana or are panels ordered based on something completely different?


Solution

  • I really have Marcus Efraimsson to thank for this, after cloning Grafana, coming to a conclusion that Grafana doesn't seem to support sorting based on metric value for template variables, but then learning about the query_result helper for template variables.

    An issue exists that kinda tries to do the same thing, but with a different end result. Marcus responded to this issue, referencing the query_result helper.

    https://github.com/grafana/grafana/issues/11674#issuecomment-384211739

    Grafana Template Variable Example

    What I ended up doing, I used query_result so that I could use sort(..) to sort by metric value. But this made it so that literally the whole metric was added as a variable option, labels, values, everything.

    I searched for a bit and found out that you can use the regex to modify options, and not only filter them. It turns out, Grafana even has an example for this exact scenario in their documentation.

    https://grafana.com/docs/reference/templating#filter-and-modify-prometheus-example

    Thank you so much Marcus for the nudge in the right direction!