Get only unique values

34 views
Skip to first unread message

Александр Мороз

unread,
Mar 3, 2020, 6:15:37 PM3/3/20
to Prometheus Users
I'm trying to understand how to get only unique values from prometheus

For example:

from this
cool_metic{label="foo",instance="machine1"..} 5555
cool_metic{label="foo",instance="machine2"..} 5555
cool_metic{label="bar",instance="machine3"..} 3333
cool_metic{label="foo",instance="machine4"..} 5555
cool_metic{label="bar",instance="machine5"..} 3333
cool_metic{label="xxx",instance="machine6"..} 1111

i need to get something like this
cool_metic{label="foo"..} 5555
cool_metic{label="bar"..} 3333
cool_metic{label="xxx"..} 1111

I found a page on stackoverflow where it is done through avg(sum(.... but it seems to me this is not the best solution and maybe there is a more correct way to do it.

Thanks.

Julien Pivotto

unread,
Mar 3, 2020, 6:34:48 PM3/3/20
to Александр Мороз, Prometheus Users
On 03 Mar 15:15, Александр Мороз wrote:
> I'm trying to understand how to get only unique values from prometheus
>
> For example:
>
> from this
> cool_metic{label="foo",instance="machine1"..} 5555
> cool_metic{label="foo",instance="machine2"..} 5555
> cool_metic{label="bar",instance="machine3"..} 3333
> cool_metic{label="foo",instance="machine4"..} 5555
> cool_metic{label="bar",instance="machine5"..} 3333
> cool_metic{label="xxx",instance="machine6"..} 1111
>
> i need to get something like this
> cool_metic{label="foo"..} 5555
> cool_metic{label="bar"..} 3333
> cool_metic{label="xxx"..} 1111


This is a very interesting challenge!!

max without(instance) (cool_metic) + 0*count(topk by(__values) (1,count_values("__values",cool_metic) without(instance)))without(__values)


If you do not want to exclude the instance label:
cool_metic+0*count(topk by(__values) (1,count_values("__values",cool_metic) without()))without(__values)

>
> I found a page on stackoverflow
> <https://stackoverflow.com/questions/53193459/prometheus-query-equivalent-to-sql-distinct>
> where it is done through avg(sum(.... but it seems to me this is not the
> best solution and maybe there is a more correct way to do it.
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/5cb378a8-84b0-4550-91af-bfe313541ff1%40googlegroups.com.


--
(o- Julien Pivotto
//\ Open-Source Consultant
V_/_ Inuits - https://www.inuits.eu
signature.asc

Frozenaa

unread,
Mar 3, 2020, 6:44:33 PM3/3/20
to Prometheus Users


среда, 4 марта 2020 г., 2:34:48 UTC+3 пользователь Julien Pivotto написал:

max without(instance) (cool_metic) + 0*count(topk by(__values) (1,count_values("__values",cool_metic) without(instance)))without(__values)

Oh, thanks, I'll try tomorrow.

I was hoping that there is a simpler solution than described on stackoverflow, but this one looks even more complicated)

Julien Pivotto

unread,
Mar 3, 2020, 7:04:01 PM3/3/20
to Frozenaa, Prometheus Users
If it is just to display you can deal with value as labels just use

(1,count_values("__values",cool_metic) without(instance)
--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/e012630c-eb9c-4b1b-95ab-7476b6bf816f%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages