relable_config: trim a part of address

28 views
Skip to first unread message

Anna Chekhovskikh

unread,
May 27, 2020, 3:20:11 AM5/27/20
to Prometheus Users
Hi, colleagues!

I need your help in relable_configs: I added a new label, but it doesn't work with a correct regex.
 - source_labels: [instance]
        regex: '.*\/'
        replacement: 'xxx'
        target_label: store

I need to replace a part of address (label instance) to xxx, because Polystat Panel in Grafana can't display full name of instance because of length.
What am I doing wrong?
Thank you in advance!

Brian Candler

unread,
May 27, 2020, 3:55:03 AM5/27/20
to Prometheus Users
The 'instance' label isn't set until after relabelling.

So you need to use source_labels: [__address__]

After relabeling, the instance label is set equal to the __address__ label - but only if you haven't already created an instance label yourself.  This gives you an opportunity to set the 'instance' label to something more meaningful.  It also lets you keep the exporter port number out of the label: e.g.

- source_labels: [__address__]
  target_label: instance
- source_labels: [__address__]
  replacement: '${1}:9100'
  target_label: __address__

Brian Candler

unread,
May 27, 2020, 3:58:44 AM5/27/20
to Prometheus Users
Example:

- source_labels: [__address__]
  regex: '(.*)/.*'
  replacement: '$1'     # this is the default anyway
  target_label: store

This will strip off everything after the final slash and put it in the 'store' label.

Anna Chekhovskikh

unread,
May 27, 2020, 4:07:05 AM5/27/20
to Prometheus Users
Thank you, Brian!

I checked your solution, it works, but I need to strip off all _before_ last slash. And if I change your regex to mine, I can't see the new label.


среда, 27 мая 2020 г., 10:58:44 UTC+3 пользователь Brian Candler написал:

Brian Candler

unread,
May 27, 2020, 5:22:59 AM5/27/20
to Prometheus Users
On Wednesday, 27 May 2020 09:07:05 UTC+1, Anna Chekhovskikh wrote:
I checked your solution, it works, but I need to strip off all _before_ last slash.

- source_labels: [__address__]
  regex: '.*/(.*)'
  replacement: '$1'     # this is the default anyway
  target_label: store

The regular expression "captures" sections in parentheses.  $1 is the first parenthesised subexpression, $2 is the second etc.  So it could also be written:

- source_labels: [__address__]
  regex: '(.*)/(.*)'
  replacement: '$2'
  target_label: store

Regexps are "greedy" by default, so aaa/bbb/ccc will be matched as (aaa/bbb) / (ccc) 


And if I change your regex to mine, I can't see the new label.


You'll need to show your regex, and an example of the input you're trying to match against it, and what you would like to see as the output.

Anna Chekhovskikh

unread,
May 27, 2020, 6:36:15 AM5/27/20
to Prometheus Users
Brine, so thank you for your help! It works perfectly and it's absolutely the same, what I want to see!


среда, 27 мая 2020 г., 12:22:59 UTC+3 пользователь Brian Candler написал:
Reply all
Reply to author
Forward
0 new messages