🌐
Prometheus
prometheus.io › docs › prometheus › latest › configuration › configuration
Configuration | Prometheus
Targets may be statically configured via the static_configs parameter or dynamically discovered using one of the supported service-discovery mechanisms. Additionally, relabel_configs allow advanced modifications to any target and its labels before scraping.
🌐
Grafana
grafana.com › blog › how-relabeling-in-prometheus-works
How relabeling in Prometheus works | Grafana Labs
March 22, 2022 - A <relabel_config> consists of seven fields. These are: ... A Prometheus configuration may contain an array of relabeling steps; they are applied to the label set in the order they’re defined in.
🌐
HeyOnCall
heyoncall.com › guides › the-art-of-metric-relabeling-in-prometheus
The Art of Metric Relabeling in Prometheus | HeyOnCall
Once Prometheus starts discovering and scraping metrics automatically the default ingest-all configuration can quickly get out of hand, ingesting and storing hundreds (or even thousands) of time series you don’t really need. relabel_configs is an array of configuration settings applied to every target discovered by Prometheus before the target’s metrics are scraped.
🌐
GitHub
gist.github.com › ruanbekker › 72216bea59fc56af189f5a7b2e3a8002
Prometheus Relabel Config Example · GitHub
In this case we want to relabel the __address__ and apply the value to the instance label, but we want to exclude the :9100 from the __address__ label: # Config: https://github.com/prometheus/prometheus/blob/release-2.36/config/testdata/conf.good.yml global: scrape_interval: 15s evaluation_interval: 15s external_labels: cluster: 'local' scrape_configs: - job_name: 'prometheus' scrape_interval: 15s static_configs: - targets: ['localhost:9090'] - job_name: 'multipass-nodes' static_configs: - targets: ['ip-192-168-64-29.multipass:9100'] labels: test: 4 - targets: ['ip-192-168-64-30.multipass:9100'] labels: test: 4 relabel_configs: - source_labels: [__address__] separator: ':' regex: '(.*):(.*)' replacement: '${1}' target_label: instance
🌐
Ruan Bekker's Blog
ruan.dev › blog › 2022 › 05 › 30 › prometheus-relabel-config-examples
Prometheus Relabel Config Examples | Ruan Bekker's Blog
May 30, 2022 - global: scrape_interval: 15s ... labels, __address__ which will be the given target including the port, then we apply regex: (.*) to catch everything from the source label, and since there is only one group we use the ...
🌐
Better Stack
betterstack.com › community › guides › monitoring › prometheus-relabeling
Relabeling in Prometheus: A Complete Guide | Better Stack Community
December 9, 2024 - For example, imagine you have 1000 microservices, each with a unique service_id label, and you want to distribute them evenly across five Prometheus servers. You can use the following configuration: ... scrape_configs: - job_name: 'my_services' relabel_configs: - source_labels: [service_id] modulus: 5 target_label: __tmp_hashmod action: hashmod - source_labels: [__tmp_hashmod]
🌐
OneUptime
oneuptime.com › home › blog › how to configure relabeling in prometheus
How to Configure Relabeling in Prometheus
December 5, 2025 - relabel_configs: # Keep only production namespace - source_labels: [__meta_kubernetes_namespace] action: keep regex: production # Keep pods with specific annotation - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape] action: keep regex: 'true' # Keep multiple namespaces - source_labels: [__meta_kubernetes_namespace] action: keep regex: 'production|staging|development'
🌐
Robust Perception
robustperception.io › relabel_configs-vs-metric_relabel_configs
relabel_configs vs metric_relabel_configs – Robust Perception | Prometheus Monitoring Experts
April 3, 2017 - So let's shine some light on these two configuration options. Prometheus needs to know what to scrape, and that's where service discovery and relabel_configs come in. Relabel configs allow you to select which targets you want scraped, and what the target labels will be.
🌐
OneUptime
oneuptime.com › home › blog › how to create prometheus metric relabeling
How to Create Prometheus Metric Relabeling
January 30, 2026 - # prometheus.yml scrape_configs: - job_name: 'my-service' static_configs: - targets: ['localhost:9090'] metric_relabel_configs: - source_labels: [__name__] # Labels to read from separator: ';' # Join multiple labels with this regex: '(.+)' # ...
Find elsewhere
🌐
Better Stack
betterstack.com › community › questions › relable-instance-to-hostname-in-prometheus
Relabel Instance to Hostname in Prometheus | Better Stack Community
November 18, 2024 - global: scrape_interval: 15s scrape_configs: - job_name: 'my_application' static_configs: - targets: ['localhost:9090', '127.0.0.1:8080'] relabel_configs: - source_labels: [__address__] target_label: instance - source_labels: [instance] target_label: hostname regex: '([^:]+).*' replacement: '${1}' Restart Prometheus: After making changes to the prometheus.yml file, restart your Prometheus server to apply the configuration.
🌐
Stack Overflow
stackoverflow.com › questions › 74248174 › prometheus-relabel-configs-issue
Prometheus relabel_configs issue - Stack Overflow
relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - source_labels: [__address__] regex: '\/health' replacement: 'my new label' target_label: service - source_labels: [module] target_label: __param_module - target_label: __address__ replacement: newdomain:9115
🌐
Last9
last9.io › blog › mastering-prometheus-relabeling-a-comprehensive-guide
Mastering Prometheus Relabeling: A Comprehensive Guide | Last9
February 26, 2026 - Sometimes, You may need to align data from dynamic environments with a predefined and static monitoring setup, ensuring consistent and meaningful data collection. Sometimes, your labels can become complex and unwieldy. Prometheus relabeling allows you to simplify your configurations and make ...
🌐
Medium
valyala.medium.com › how-to-use-relabeling-in-prometheus-and-victoriametrics-8b90fc22c4b2
How to use relabeling in Prometheus and VictoriaMetrics | by Aliaksandr Valialkin | Medium
March 29, 2022 - It allows performing arbitrary ... applied at the following stages: at scrape_configs->relabel_configs section for transforming per-scrape target labels and dropping unneeded scrape targets...
🌐
Medium
blog.freshtracks.io › prometheus-relabel-rules-and-the-action-parameter-39c71959354a
Prometheus Relabel Rules and the ‘action’ Parameter | by Bob Cotton | FreshTracks.io
October 27, 2017 - relabel_configs: - action: keep source_labels: [__meta_kubernetes_role] regex: (?:apiserver|node) - action: replace source_labels: [__meta_kubernetes_role] target_label: job replacement: kubernetes_$1 - action: labelmap regex: __meta_kubernetes_node_label_(.+)
🌐
VictoriaMetrics
docs.victoriametrics.com › victoriametrics › relabeling
VictoriaMetrics: Relabeling cookbook
How to use Relabeling in Prometheus and VictoriaMetrics ... Relabeling starts with relabel_configs in the prometheus scrape config (-promscrape.config).
🌐
Omarghader
omarghader.github.io › prometheus-labels-relabeling-clean-metrics-guide
Using Labels and Relabeling in Prometheus for Clean, Scalable Metrics ·
October 14, 2025 - This is where you can create labels based on information from the scrape target itself. Imagine your instances have hostnames like web-server-prod-01 and db-server-staging-02. You can tear this apart and create meaningful labels. scrape_configs: ...
Top answer
1 of 3
7

Incorrect understanding

I think there was a mistake in my understanding of how labeling in prometheus works. My incorrect understanding was:

  1. before applying regex, string would be first split on separator (otherwise what is its purpose?),
  2. each substring has regex evaluated against it,
  3. if match groups are declared and found, they will be available as indexed values available to use in target_label and replacement fields.
  4. if regex does not match, then that substring will be ignored.
  5. because regex is expected to be applied to each substring after the split, it will lead to multiple labels from multiple substrings.

Correct understanding

However, from brian-brazil's post linked in his answer and Prometheus's documentation, it seems the following happening:

  1. All __meta tags are combined into one long separator separated line.
  2. regex is applied on that line only once.
  3. If regex matches and includes groups, they are indexed beginning from 1 and available for use in target_label and replacement.
  4. separator seems to be getting ignored in this section even if you mention it.

Config from corrected understanding

From this idea and following from example in the question, I was able to make the following config that works

relabel_configs:
- source_labels: [__meta_consul_tags]
  regex:         '.*,a=([a-z0-9_]+),.+'
  target_label:  'a'
  replacement:   ${1}

- source_labels: [__meta_consul_tags]
  regex:         '.*,b=([a-z0-9_]+),.+'
  target_label:  'b'
  replacement:   ${1}

- source_labels: [__meta_consul_tags]
  regex:         '.*,c=([a-z0-9_]+),.+'
  target_label:  'c'
  replacement:   ${1}

- source_labels: [__meta_consul_tags]
  regex:         '.*,d=([a-z0-9_]+),.+'
  target_label:  'd'
  replacement:   ${1}

Caveats

I believe both approaches (the approach brian-brazil wrote in his blogpost, and what I am using above) have caveats - we either need to know all the labels we want beforehand, or have a set number of them. This means if a developer wants to associate different, or more labels with his/her service, s/he would need to work with ops as general flow will not be able to handle it. I think it is a minor caveat that should be addressed.

2 of 3
1

https://www.robustperception.io/extracting-full-labels-from-consul-tags/ shows how to do this, in particular the last example.

🌐
Promlabs
training.promlabs.com › training › relabeling › introduction-to-relabeling › relabeling-overview
Relabeling | Prometheus Trainings by PromLabs
A relabeling configuration section consists of a list of rules that are applied in sequence to each labeled object.