7

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?

Hidayat Rzayev
  • 339
  • 3
  • 14

2 Answers2

5

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.

Hidayat Rzayev
  • 339
  • 3
  • 14
2

There is no need to use two label_replace() functions - a single label_replace() would be enough:

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

It matches the type label with the given regexp - some-(\w+(-\w+)*)-.* - and then puts the matched outer group into type_group label. Then sum by (type_group) (...) sums results grouped by the constructed type_group label.

valyala
  • 11,669
  • 1
  • 59
  • 62