Search code examples
prometheuspromql

Group labels in a Prometheus query based on regex


Imagine I have metrics like this:

sample_metric{type="some-type1-1234"} 2
sample_metric{type="some-type1-5678"} 1
sample_metric{type="some-type2-9876"} 4
sample_metric{type="some-type2-4321"} 3

Now I would like to sum the metric values based on types that follow the same pattern. So, for the above example the desired result would be to have 2 sums:

type1: 3
type2: 7

This question is somewhat similar to this one, however in my case I don't know the groups in advance. I just know the pattern that they follow. Is there a way to achieve this with one query using regex?


Solution

  • I figured out how to do it, again with the help of the accepted answer from this post. Posting the answer for those who will have come across the same problem in the future:

    sum by (type_group) (
      label_replace(
        label_replace(sample_metric, "type_group", "$1", "type", ".+"),
        "type_group", "$1", "type", "some-(\\w+(-\\w+)*)-.*"
      )
    )
    

    So, the inner label_replace introduces a new label called type_group, whereas the outer label_replace replaces the values with the type pulled from the original label, with the help of regex. So, type_group will contain values, such as type1 and type2 ($1 refers to the regex group to pull). The inner group (-\\w+)* indicates that your group might be comprised of several parts, e.g. type1-type12, and it will treat it as yet another group.