mordowiciel
mordowiciel

Reputation: 121

Using regular expressions in Prometheus relabel_configs source_labels

I'm trying to save the meta labels retrieved from EC2 Service Discovery as target labels. I'm mostly concerned about the tags - every instance contains a lot of them and I would love to gather them all using one simple configuration entry with the usage of regular expression.

The perfect solution seems to be something like this:

relabel_configs:
   - source_labels:
      - '__meta_ec2_tag_(.*)'
     target_label: '### use extracted regex group here'

Unfortunately, I get the following error:

\"__meta_ec2_tag_(.*)\" is not a valid label name"

Does that mean that I can't use regular expressions to describe source labels and that I have to specify each source label separately like in the sample below?

- source_labels:
    - '__meta_ec2_tag_Name'
  target_label: 'instance_name'

- source_labels:
    - '__meta_ec2_tag_environment'
  target_label: 'environment'

- source_labels:
    - '__meta_ec2_tag_project'
  target_label: 'project'

Upvotes: 4

Views: 6032

Answers (2)

Benjamin Delacour
Benjamin Delacour

Reputation: 11

I just encountered the same problem but the previous answer didn't work for me with this error :

relabel configuration for replace action requires 'target_label' value

I found out that Prometheus now has a labelmap option for relabel_configs that does this : https://grafana.com/blog/2022/03/21/how-relabeling-in-prometheus-works/#labelmap

TLDR;

relabel_configs:
- action: labelmap
  regex: "__meta_ec2_tag_(.*)"
  replacement: "$1"

Upvotes: 1

Alex Sveshnikov
Alex Sveshnikov

Reputation: 4329

Try this:

relabel_configs:
   - regex: '__meta_ec2_tag_(.*)'
    replacement: $1

Upvotes: 4

Related Questions