How can I group labels in a Prometheus request?
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
Is it possible to execute a query or even label_replace
one that ties the "misc" groups together?
(I understand that the performance of the metric needs to be improved and I updated the application to fix it. However, this left me with this question if I wanted to fix the metrics via query later)
source to share
Yes, you can use tag swap to group all 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 the group to new_group, the outer one overwrites those that match "misc group. +" With "misc", and then we sum over label "new_group". The reason for using the new label is because the series is no longer unique if we just overwrite the "group" label and the sum doesn't work.
source to share