Renan Prata
Renan Prata

Reputation: 49

Prometheus metric relabel for specific value

I'm trying to use regex to create new label. My metric is:

some_metric{path="test/name/bar/foo"}

I want to get bar and put this in new label (name). I tried to relabel but it did not work (prometheus.yml):

    relabel_configs:
    - source_labels: [path]
      regex: "/test/name/(*.)/*."
      replacement: "$1"
      target_label: "name"

New output:

some_metric{path="test/name/bar/foo", name="bar"}

EDIT:

I removed replacement field and it update regex to "test/name/(*.)/*.". It resolved for me.

Upvotes: 0

Views: 3168

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626806

You can use

relabel_configs:
    - source_labels: [path]
      regex: "test/name/([^/]*).*"
      target_label: "name"

Note that the regex is defined with a string here, not a regex literal, so the first / is erroneous here.

Next, *. is a user error, .* matches any text. However, if you have more subparts, it will capture till the last /, and this can be avoided with a negated character class [^/]+.

Details:

  • test/name/ - a literal fixed string
  • ([^/]*) - Capturing group 1: zero or more chars other than /
  • .* - the rest of the line (zero or more chars other than line break chars as many as possible).

If

Upvotes: 1

Related Questions