【问题标题】:Label replace over a range vector in Prometheus在 Prometheus 中的范围向量上进行标签替换
【发布时间】:2020-03-21 02:27:36
【问题描述】:

我想在 10 分钟内找到所有以“sendsms”开头的 pod 的警报总数。

我可以使用label_replace() 在即时向量上执行此操作。但是当我想对超过 10 分钟的数据执行此操作时,它无法工作,因为 label_replace 仅适用于即时向量。

用一个例子来解释问题:

ALERTS{alertname="CPUThrottlingHigh",pod="sendsms-dbed"} 10
ALERTS{alertname="CPUThrottlingHigh",pod="sendsms-ebed"} 20
ALERTS{alertname="CPUThrottlingHigh",pod="sendsms-fbed"} 30
ALERTS{alertname="CPUThrottlingHigh",pod="sendmail-gbed"} 60
ALERTS{alertname="CPUThrottlingHigh",pod="sendmail-hbed"} 70
ALERTS{alertname="CPUThrottlingHigh",pod="sendmail-ibed"} 80

使用标签替换我可以使用正则表达式添加一个新标签,然后我可以对它进行分组并获得结果。

label_replace(ALERTS{alertname="CPUThrottlingHigh", "podname", "$1", "pod", "([a-z-A-Z]+)-.*")

ALERTS{alertname="CPUThrottlingHigh",pod="sendsms-dbed", podname="sendsms"} 10
ALERTS{alertname="CPUThrottlingHigh",pod="sendsms-dbed", podname="sendsms"} 10

如何在 10 分钟内完成 ALERTS 并计算总和?

我想要最近 10 分钟的结果

ALERTS{alertname="CPUThrottlingHigh",podname="sendsms"} 60
ALERTS{alertname="CPUThrottlingHigh",podname="sendmail"} 210

目标:找出最近 1 周内创建警报数量最多的 pod。

【问题讨论】:

  • 为什么不直接使用正则表达式选择器?
  • pod 名称中有一个 uuid。我想获取所有 pod 的数据,而不仅仅是 sendms(也来自 sendmail,在同一个查询中)。看起来仅使用正则表达式选择器是不可能的。如果您认为可以,可以分享一个示例查询吗?
  • pod=~”(sendsms|sendmail)-.*”
  • 我想获取所有 pod 的数据,而不仅仅是 sendms。让我编辑问题。
  • 这样的 pod 大概有 1000 个。不只是这两个。我的目标是找到产生最多警报的 pod。

标签: kubernetes prometheus promql


【解决方案1】:

我可以在求和后通过 label_replace 解决这个问题

查询

sort_desc(sum by (pod_set) (label_replace(sort_desc(sum by (namespace, pod) (avg_over_time(ALERTS{alertname=~"(KubeDeploymentReplicasMismatch|KubePodNotReady|KubePodCrashLooping|KubeJobFailed)", alertstate="firing"}[1w]))), "pod_set", "$1", "pod", "([a-z-A-Z]+)-.*" )))

结果

{pod_set="sendsms"} 62
{pod_set="emailspreprocessor"}  32
{pod_set="sendmail"}    21

【讨论】:

    猜你喜欢
    • 2021-01-20
    • 2022-07-22
    • 2022-01-22
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    相关资源
    最近更新 更多