首页 > 解决方案 > Prometheus/PromQL/Grafana:右侧范围向量可能不存在时的减法

问题描述

我有两个计数器指标:always_existssometimes_exists.

我想从减法接收一个结果向量always_exists - sometimes_exists,即使指标sometimes_exists不存在(即查询不返回任何内容)。在这种情况下,我希望结果等于always_exists - 0。这可能吗?

标签: prometheusgrafanapromql

解决方案


尝试以下查询:

(always_exists - sometimes_exists) or (always_exists unless sometimes_exists)

它使用orunless运算符。在https://prometheus.io/docs/prometheus/latest/querying/operators/#logical-set-binary-operators查看有关这些运算符的更多详细信息

PS 查询可以简化为sum(always_exists, -sometimes_exists)VictoriaMetrics 因为MetricsQL支持聚合函数中的多个参数,例如sum. (我是 VictoriaMetrics 的核心开发者)


推荐阅读