如果两个 PromQL 结果相同,则从 PromQL 查询返回 1 或 0

问题描述 投票:0回答:1

我有两个 Prometheus 指标,

第一个PromQL

sum by (cluster) (
    cnp_pg_replication_slots_active{
       role="primary",
       cluster="p-vpt7bgc20z"
    } == 1
)  

这给了我这样的结果

{cluster="p-vpt7bgc20z"}    2

第二个PromQL

sum by (cluster) (
    cnp_collector_up { 
        role="replica",
        cluster="p-vpt7bgc20z"
    }
)

这也给了我这样的结果

{cluster="p-vpt7bgc20z"}    2

现在我想如果两个结果相同则返回 1,如果有任何不匹配则返回 0。我怎样才能存档?

如果我写

sum by (cluster) (
    cnp_pg_replication_slots_active{
       role="primary",
       cluster="p-vpt7bgc20z"
    } == 1
)  == sum by (cluster) (
    cnp_collector_up { 
        role="replica",
        cluster="p-vpt7bgc20z"
    }
)

它给我的结果为,但我希望结果为布尔值 1 和 0。

{cluster="p-vpt7bgc20z"}    2
prometheus promql thanos
1个回答
0
投票

在 promQL 中,您可以在比较运算符后使用

bool
修饰符来返回 0 或 1 而不是过滤。 例如,
metric > bool 100

查询中使用的演示可以在这里看到。

有关此事的文档此处

您的查询将是

sum by (cluster) (
    cnp_pg_replication_slots_active{
       role="primary",
       cluster="p-vpt7bgc20z"
    } == 1
)  == bool sum by (cluster) (
    cnp_collector_up { 
        role="replica",
        cluster="p-vpt7bgc20z"
    }
)
© www.soinside.com 2019 - 2024. All rights reserved.