如何从 PromQL (Prometheus) 中的范围向量计算超过阈值的指标

How to count over threshold metrics from range vector in PromQL (Prometheus)

我定义了 latency 它可以查询为标量的指标,如下所示:

latency{name="Controller/products/show",percentiles="95"}   0.9935112

然后,我做了查询。输出是一个范围向量。

latency{name="Controller/products/show",percentiles="95"}[10m]

输出:

element:
latency{name="Controller/products/show",percentiles="95"}

value:
0.9429009 @1584497778.164
0.9150374 @1584497838.164
0.9085548 @1584497898.164
0.9006939 @1584497958.164
0.9390876 @1584498018.164
0.9593425 @1584498138.164
0.96289706 @1584498198.164
0.98113775 @1584498258.164
0.9935112 @1584498318.164

我想计算向量范围值中的 0.95 个值。

例如,从上述矢量范围值中得到4

有人有解决办法吗?

Prometheus subquery 可用于此任务:

count_over_time((latency{name="Controller/products/show",percentiles="95"} > 0.95)[10m:50s])

请注意,冒号后的 step 值(上例中的 50s )必须小于所选指标的抓取间隔,因为 Prometheus 在常规点评估括号内的查询它们之间配置的 step 间隔。

更新: 当使用 count_gt_over_time() function from MetricsQL 时,这个任务也可以在没有子查询的情况下解决。例如,以下查询 returns 过去 10 分钟内值超过 0.95 的原始样本数:

count_gt_over_time(latency{name="Controller/products/show",percentiles="95"}, 0.95)