标签替换普罗米修斯中的范围向量

Alo*_*ngh 1 kubernetes prometheus promql

我想找到 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
Run Code Online (Sandbox Code Playgroud)
ALERTS{alertname="CPUThrottlingHigh",pod="sendmail-gbed"} 60
ALERTS{alertname="CPUThrottlingHigh",pod="sendmail-hbed"} 70
ALERTS{alertname="CPUThrottlingHigh",pod="sendmail-ibed"} 80
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

如何在 10 分钟内为 ALERTS 执行此操作并计算总和?

我想要在过去 10 分钟内得到这样的结果

ALERTS{alertname="CPUThrottlingHigh",podname="sendsms"} 60
ALERTS{alertname="CPUThrottlingHigh",podname="sendmail"} 210
Run Code Online (Sandbox Code Playgroud)

目标:找到过去 1 周内创建最多警报的 Pod。

Alo*_*ngh 5

我能够通过在求和后做 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]+)-.*" )))
Run Code Online (Sandbox Code Playgroud)

结果

{pod_set="sendsms"} 62
{pod_set="emailspreprocessor"}  32
{pod_set="sendmail"}    21
Run Code Online (Sandbox Code Playgroud)