PromQL avg_over_time for non-zero values

泪湿孤枕 提交于 2021-02-11 08:42:08

问题


I am trying to get avg_over_time value from gauge metric, but I would want average only from the non-zero values of the metric (or values higher than zero, to be exact).

Example:

avg_over_time(foo[2d] > 0)

But I alwas get parse error: binary expression must contain only scalar and instant vector types

I tried setting up recording rule

expr: foo > 0

But unfortunately with the same result.

Is this possible in PromQL?


回答1:


You can use a sub-query with Prometheus version above 2.7:

avg_over_time((foo > 0)[2d:])



回答2:


While it is possible to use Prometheus subquery such as avg_over_time((foo > 0)[2d:]) as Michael already mentioned, the proposed query may either miss certain data points or double-count certain datapoints depending on the resolution value after the colon in square brackets. For example, avg_over_time((foo > 0)[2d:1h]) would take into account only a single raw data point per each hour when calculating avg_over_time, while avg_over_time((foo>0)[2d:1s]) would count the same raw datapoints multiple times if scrape interval for foo exceeds 1s. By default the resolution value equals to step value passed in the query to /api/v1/query_range, so the end result of the query may be quite different depending on the step query arg passed to /api/v1/query_range.

If you need to calculate the exact share of raw datapoints exceeding the given threshold independently of step query arg, then take a look at share_gt_over_time(foo[2d], threshold) function from MetricsQL. This function may be useful for calculating SLO/SLI over uptime and/or latencies.



来源:https://stackoverflow.com/questions/59016284/promql-avg-over-time-for-non-zero-values

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