Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prometheus query equivalent to SQL DISTINCT

I have multiple Prometheus instances providing the same metric, such as:

my_metric{app="foo", state="active",   instance="server-1"}  20
my_metric{app="foo", state="inactive", instance="server-1"}  30
my_metric{app="foo", state="active",   instance="server-2"}  20
my_metric{app="foo", state="inactive", instance="server-2"}  30

Now I want to display this metric in a Grafana singlestat widget. When I use the following query...

sum(my_metric{app="foo", state="active"})

...it, of course, sums up all values and returns 40. So I tell Prometheus to sum it by instance...

sum(my_metric{app="foo", state="active"}) by (instance)

...which results in a "Multiple Series Error" in Grafana. Is there a way to tell Prometheus/Grafana to only use the first of the results?

like image 457
Ethan Leroy Avatar asked Jun 30 '26 10:06

Ethan Leroy


1 Answers

I don't know of a distinct, but I think this would work too:

topk(1, my_metric{app="foo", state="active"} by (instance))

Check out the second to last example in here: https://prometheus.io/docs/prometheus/latest/querying/examples/

like image 103
parliamentowl Avatar answered Jul 06 '26 21:07

parliamentowl



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!