34

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")
emperorspride188
  • 549
  • 1
  • 5
  • 11

2 Answers2

60
count(count by (a) (hello_info))

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

brian-brazil
  • 24,975
  • 4
  • 64
  • 67
  • Doesn't seem to work for me, the first count returns a matrix of metrics (where each value is a vector too), and running count on it returns a nonsensical value such as "1.3" ... any ideas? – tutuDajuju May 13 '19 at 13:19
  • I think this query returns a time series rather than a number of all values ever recorded for a particular label. – mac13k Jun 16 '20 at 12:21
8

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))
Ferrandinand
  • 332
  • 2
  • 9