本文介绍了Prometheus-通过正则表达式进行汇总和重新标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有以下Promql查询,可让我查询每个K8S Pod使用的内存:

I currently have the following Promql query which allow me to query the memory used by each of my K8S pods:

sum(container_memory_working_set_bytes{image!="",name=~"^k8s_.*"}) by (pod_name)

广告连播的名称后面是由K8S定义的哈希:

The pod's name is followed by a hash defined by K8S:

weave-net-kxpxc
weave-net-jjkki
weave-net-asdkk

哪个都属于同一个应用程序:weave-net

Which all belongs to the same app: weave-net

我想汇总属于同一应用程序的所有Pod的内存.

What I would like is to aggregate the memory of all pods which belongs to the same app.

因此,查询将求和所有weave-net个pod的内存,并将结果放入名为weave的应用程序中.这样的结果将是:

So, the query would sum the memory of all weave-net pods and place the result in an app called weave. Such as the result would be:

{pod_name="weave-net"}            10

代替

{pod_name="weave-net-kxpxc"}       5
{pod_name="weave-net-jjkki"}       3
{pod_name="weave-net-asdkk"}       2

是否有可能这样做,如果可以,怎么办?

Is it even possible to do so, and if yes, how ?

推荐答案

您可以使用 label_replace

sum(label_replace(container_memory_working_set_bytes{image!="",name=~"^k8s_.*"}, "pod_set", "$1", "pod_name", "(.*)-.{5}")) by (pod_set)

您将包括一个与第一个组($ 1)匹配的新标签(pod_set),该新标签与pod_name标签上的正则表达式匹配.然后,对新标签pod_set求和.

You will be including a new label (pod_set) that matches the first group ($1) from matching the regex over the pod_name label. Then you sum over the new label pod_set.

[已编辑]

这篇关于Prometheus-通过正则表达式进行汇总和重新标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 00:25