Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select largest label value in Prometheus query

I have a Prometheus and Grafana instance set up where I want to query a specific property called "upsAdvBatteryRecommendedReplaceDate".

Prometheus successfully returns a response when querying this property as can be seen here: upsAdvBatteryRecommendedReplaceDate in Prometheus

However, the result consists of two time series (one with the upsAdvBatteryRecommendedReplaceDate="12/11/2021" and the other with upsAdvBatteryRecommendedReplaceDate="12/10/2021").

This becomes a problem as soon as I set up a singlestat in Grafana displaying the upsAdvBatteryRecommendedReplaceDate label of the upsAdvBatteryRecommendedReplaceDate query. This will lead to a "Multiple Series Error" as can be seen here: upsAdvBatteryRecommendedReplaceDate in Grafana

It works as expected if I change the Grafana time range to e.g. "Last 15 minutes" as it can be seen here: upsAdvBatteryRecommendedReplaceDate in Grafana with proper replacement date

So my question is: Is there a way to select/extract only the time series with the most recent/latest label value for "upsAdvBatteryRecommendedReplaceDate" so that, in this case, only the time series where upsAdvBatteryRecommendedReplaceDate="12/11/2021" shows up?

Thank you!

like image 655
Patrick Avatar asked Feb 21 '26 00:02

Patrick


2 Answers

Maybe this will help other people out as well: I resolved it by simply checking the "Instant" checkbox of the singlestat in the grafana dashboard.

like image 130
Patrick Avatar answered Feb 24 '26 14:02

Patrick


Prometheus is a system for working with numbers, and "12/10/2021" is a string rather than a number. If you can get the data exposed as a unix timestamp then you can use the max aggregator to get the highest value.

like image 25
brian-brazil Avatar answered Feb 24 '26 13:02

brian-brazil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!