Prometheus:合并二元运算中缺失标签的值

asp*_*yct 3 monitoring prometheus

我有两个具有匹配标签的指标,两个计数器:

accounts_created_total{provider="auth0"} 738
accounts_created_total{provider="google} 980

accounts_deleted_total{provider="auth0"} 65
Run Code Online (Sandbox Code Playgroud)

我想根据这两个指标计算现有帐户的数量。我想出了这个:

accounts_created_total - accounts_deleted_total

# which results in

{provider="auth0"} 673

# Note the missing provider="Google"
Run Code Online (Sandbox Code Playgroud)

不幸的是,没有account_deleted_totalfor provider="Google",所以我只能得到结果provider="auth0"

有没有办法告诉普罗米修斯“弥补”丢失的标签?这通常相当于coalesceSQL 中的 a。

Mic*_*bez 9

您可以使用OR 二元运算符完成时间序列:

矢量 1 或矢量 2 产生一个矢量,其中包含矢量 1 的所有原始元素(标签集 + 值)以及矢量 2 中在矢量 1 中没有匹配标签集的所有元素。

假设您想要默认accounts_deleted_total为 0,以下表达式用作accounts_created_total第二个向量来提取标签,并乘以 0 确保重置该值:

accounts_deleted_total OR (accounts_created_total * 0)
Run Code Online (Sandbox Code Playgroud)

在 的情况下autho0,标签存在于accounts_deleted_total,第二部分将不会被使用;相反,对于google,第二部分将产生

{provider="google"} 0
Run Code Online (Sandbox Code Playgroud)

最后,您可以在表达式中使用它:

accounts_created_total - (accounts_deleted_total OR (accounts_created_total * 0))
Run Code Online (Sandbox Code Playgroud)

在您的具体情况下,由于您使用相同的指标来提取标签,您甚至可以将表达式简化为:

(accounts_created_total - accounts_deleted_total) OR accounts_created_total
Run Code Online (Sandbox Code Playgroud)