Prometheus query to count unique label values

徘徊边缘 提交于 2020-08-22 01:55:15

问题


I want to count number of unique label values. Kind of like

select count (distinct a) from hello_info

For example if my metric 'hello_info' has labels a and b. I want to count number of unique a's. Here the count would be 3 for a = "1", "2", "3".

hello_info(a="1", b="ddd")
hello_info(a="2", b="eee")
hello_info(a="1", b="fff")
hello_info(a="3", b="ggg")

回答1:


count(count by (a) (hello_info))

First you want an aggregator with a result per value of a, and then you can count them.




回答2:


Other example: If you want to count the number of apps deployed in a kubernetes cluster based on different values of a label( ex:app):

count(count(kube_pod_labels{app=~".*"}) by (app))


来源:https://stackoverflow.com/questions/51882134/prometheus-query-to-count-unique-label-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!