Prometheus: Add label to a metric using relabel_configs

prometheus relabel_configs add label

1 min read | by Jordi Prats

In Prometheus, to be able to add a label to the metrics retrieved by a particular job we can use relabel_configs.

In order to apply the label to any metrics scraped using a particular job we'll have to add a match for something that always exists, such as __address__. We're going to add a replacement with a regex that always match without specifying any match group’s in our replacement string: This way the entire string is just copied into target_label:

scrape_configs:  - job_name: prometheus  scrape_interval: 30s  static_configs:  - targets: ['localhost:9090']  relabel_configs:  - source_labels: [__address__]  target_label: new-label  replacement: "new-label-value" 

With this configuration any metric coming from job_name: prometheus will have the label new-label=new-label-value.


Posted on 08/03/2023