如何在Prometheus查询中对标签进行分组? [英] How can I group labels in a Prometheus query?

查看:1933
本文介绍了如何在Prometheus查询中对标签进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的指标带有以下标签:

If I have a metric with the following labels:

my_metric{group="group a"}  100
my_metric{group="group b"}  100
my_metric{group="group c"}  100
my_metric{group="misc group a"}  1
my_metric{group="misc group b"}  2
my_metric{group="misc group c"}  1
my_metric{group="misc group d"}  1

是否可以进行查询,甚至可以将'misc'组组合在一起的label_replace?

Is it possible to do a query or even a label_replace that combines the 'misc' groups together?

(我意识到指标基数需要改进,并且我已经更新了应用程序以对其进行修复.但是,这让我想到了是否要稍后通过查询修复指标的问题)

(I realize that the metric cardinality needs to be improved, and I've updated the app to fix it. However it left me with this question for if I wanted to fix the metrics via a query later)

推荐答案

是的,您可以使用标签替换将所有杂项组合在一起:

Yes, you can you use label replace to group all the misc together:

sum by (new_group) (
  label_replace(
    label_replace(my_metric, "new_group", "$1", "group", ".+"),
    "new_group", "misc", "group", "misc group.+"
  )
)

内部label_replace将所有值从group复制到new_group,外部将那些与"misc group.+"匹配的值覆盖为"misc",然后通过"new_group"标签求和.使用新标签的原因是,如果我们只是改写组"标签,则该系列将不再是唯一的,并且总和将不起作用.

The inner label_replace copies all values from group into new_group, the outer overwrites those which match "misc group.+" with "misc", and we then sum by the "new_group" label. The reason for using a new label is the series would no longer be unique if we just overwrote the "group" label, and the sum wouldn't work.

这篇关于如何在Prometheus查询中对标签进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆