1

I have a metric which has an instance label whose value pattern like this:

cga-ops01.it.local:1000
cue-ops01.it.local:1000
cga-ops02.it.local:1000
cue-ops02.it.local:1000
cue-ops03.it.local:1000

The only part I care about is first part of the string before first - char, and I want to group by this label to two groups with sum by PromQL command:

  • cga
  • cue

Edit:

I created query:

sum by(infra) (
    label_replace(
        rate(wallet_operation_total[1m]), "infra", "cue", "instance", "cue-.*"
    )
)
or
sum by(infra) (
    label_replace(
        rate(wallet_operation_total[1m]), "infra", "cga", "instance", "cga-.*"
    )
)

The outcome is almost what the outcome of what I'm looking for, but there is a label value named "Value" and I don't know where it's coming from.

enter image description here

Orhun
  • 1,162
  • 14
  • 22
  • You can add either a temporary label (https://prometheus.io/docs/prometheus/latest/querying/functions/#label_replace) or a permanent one (https://prometheus.io/docs/prometheus/latest/configuration/recording_rules/). Then aggregate as usual. – anemyte Jan 13 '23 at 13:18
  • Also, you can simply run two queries: one with `{instance=~"cue-.+"}` and another one with `{instance=~"cga-.+"}`. – anemyte Jan 13 '23 at 13:20
  • @anemyte Can you provide a concrete example as an answer? – Orhun Jan 13 '23 at 13:52
  • There are examples in similar question (https://stackoverflow.com/questions/49334224/prometheus-group-by-substring-of-label?rq=1) and in the docs which I mentioned. – anemyte Jan 13 '23 at 14:06
  • Does this answer your question? [Prometheus group by substring of label](https://stackoverflow.com/questions/49334224/prometheus-group-by-substring-of-label) – Marcelo Ávila de Oliveira Jan 13 '23 at 14:48
  • I already looked at these SO questions before creating mine. I edited the question with the point what I've reached. – Orhun Jan 13 '23 at 15:25
  • Note that you can simplify your expression with `sum by(infra) (label_replace(rate(wallet_operation_total[1m]), "infra", "$1", "instance", "([^-]*)-.*"))`. This `Value` thing looks like a grafana issue. You can inspect objects returned by grafana. – Michael Doubez Jan 14 '23 at 19:38

1 Answers1

1

Try the following query:

sum by(infra) (
    label_replace(
        rate(wallet_operation_total[1m]), "infra", "$1", "instance", "([^-]+).+"
    )
)

The inner label_replace extracts a prefix until the first - char from the instance label and stores it into the infra label. The outer sum(...) by (infra) sums the metrics by the infra label.

valyala
  • 11,669
  • 1
  • 59
  • 62