'PromQL - Count how many times a metric had the same value
Is there any function that shows how many times a specific metric had the same value on Prometheus?
I have a metric that constantly returns the integer 1024: exec_nsa_server_brokers_on_KM
When something goes wrong, this value could be 1023 or even 1022. What I'd like achieve is to have a query that returns how any times that metric returned 1024 or 1023.
I have tried some of the count functions but looks like they work on different scenarios.
Solution 1:[1]
You can use either subqueries or recording rules for that. The subquery version would be something along the lines of:
sum_over_time(count_values without() ("value", up)[10m:])
(replacing up with exec_nsa_server_brokers_on_KM in your case; and optionally using avg_over_time instead to get a ratio instead of an absolute value).
This would give you an approximation of the number of times each timeseries had a given value over the past 10 minutes (or whatever time range you're interested in).
The recording rule alternative might be more efficient if you're calculating this value often enough (e.g. on a refreshing dashboard) and would consist simply in recording the value of count_values without() ("value", up) into a new metric and then calculating sum_over_time() / avg_over_time() on top of that metric instead.
Solution 2:[2]
You need count_eq_over_time or count_ne_over_time functions from MetricsQL. For example, the following query would return the number of raw samples with 1024 value over the last hour:
count_eq_over_time(exec_nsa_server_brokers_on_KM[1h], 1024)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alin Sînp?lean |
| Solution 2 | valyala |
