Prometheus get histogram counts as percentages of total

Viewed 4237

I have a histogram in Prometheus, and in Grafana I'm trying to get a graph of the distribution of counts for one of the labels as a percent over time. I'm currently trying something like

sum(rate(histogram_count{label1="value1"}[5m])) by (label2) 
/ 
sum(rate(histogram_count{label1="value1"}[5m]))

but it's not returning any values. Am I doing something wrong? I just want the sum of counts for each value of label2, divided by the total sum of counts.

1 Answers

The problem is that the labels don't match on both sides. You can use group_left to do a many-to-one match, and ignoring to ignore the mismatched label:

  sum by (label2)(rate(histogram_count{label1="value1"}[5m]))
/ ignoring (label2) group_left
  sum(rate(histogram_count{label1="value1"}[5m]))

For more information see https://www.robustperception.io/using-group_left-to-calculate-label-proportions

Related