36

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

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

是否可以进行查询甚至label_replace将“杂项”组组合在一起?

(我意识到指标基数需要改进,我已经更新了应用程序来修复它。但是它给我留下了这个问题,如果我想稍后通过查询来修复指标)

4

3 回答 3

42

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.+"
  )
)

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.

于 2017-07-18T10:02:05.653 回答
27

它更容易

sum by (group) (my_metric)
于 2020-05-14T12:06:26.943 回答
12

您可以使用正则表达式查询:

my_metric{group=~"misc group.+"}

这将为您group提供以“misc group”开头的所有内容。

于 2017-07-18T02:57:41.333 回答