jbuddy
jbuddy

Reputation: 300

Select the most frequent metric value

I need to create a PromQL query to select the most frequent metric value and display it in a Grafana panel; something like (Find most frequent value in SQL column)

In the following example, for metric status with label job,

I want to display in a panel status 1 (given count for 1 = 2, count for 2 = 1, count for 3 = 1) (and possibly use that 1 in Grafana, i.e., as a value in the Values Mapping section of a panel such as Polystat).

status(job="a") = 1
status(job="b") = 2
status(job="c") = 1
status(job="d") = 3

Upvotes: 0

Views: 1392

Answers (2)

Michał Politowski
Michał Politowski

Reputation: 4385

The query max(count_values("value",status)) == ignoring(value) group_right count_values("value",status) will give you {value="1"} 2 (and unlike topk may give you more rows if several metrics have the same maximal count).

Then in Grafana you just need to display the label from the result.

Upvotes: 1

bjakubski
bjakubski

Reputation: 1757

Try:

count_values("val", status)

You can try to apply topk(1, ...) on that, but it is tricky (what if two top values have the same number of occurrences?).

Upvotes: 2

Related Questions