How can I group labels in a Prometheus query?

前端 未结 3 625
梦毁少年i
梦毁少年i 2021-02-18 13:16

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{gro         


        
相关标签:
3条回答
  • 2021-02-18 14:00

    You can use a regex query:

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

    That will give you everything where group starts with "misc group".

    0 讨论(0)
  • 2021-02-18 14:02

    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.

    0 讨论(0)
  • 2021-02-18 14:12

    It's even easier

    sum by (group) (my_metric)
    
    0 讨论(0)
提交回复
热议问题