- Service Discovery: discovered labels
- relabel_configs (pre-scrape): limit/drop/keep targets, create new labels
- metric_relabel_configs (post-scrape): rename metrics, drop/keep metrics, rename metric labels
- Typical discovered labels you will see:
env(dev/staging/prod),size,team(web/database),type, plus instance and job metadata. - With these labels you can decide which targets to scrape and which labels to keep or transform.

Relabeling is applied at two distinct times:
relabel_configsare applied during target discovery (before scraping).metric_relabel_configsare applied after scraping (on the collected metrics). Userelabel_configsto filter targets and adjust target labels. Usemetric_relabel_configsto rename metrics or manipulate metric labels.
1) relabel_configs (pre-scrape): filter targets and modify discovered labels
Example goal: only scrape targets whereenv=prod.
YAML example (add to the nodes job in prometheus.yml):
source_labels: list of discovered label keys to use for matching.regex: regular expression to match against the concatenatedsource_labels.action: keepwill keep matching targets and drop everything else.
env=prod.

action: drop behaves as the inverse of keep — drop all matched targets and keep the rest. Example:
action: drop, targets with env=prod are removed and other targets remain.
Combine labels into a new target label (replace)
Create a new target labelinfo that concatenates team and env (e.g. database-prod):
- When multiple
source_labelsare provided, Prometheus joins them with semicolons before matching theregex. $1,$2refer to the first and second captured groups from the regex.
Remove a discovered label (labeldrop / labelkeep)
To drop a discovered label such assize, use labeldrop:
labelkeep if you want to keep only specific labels and drop the rest.
Table: Common relabel actions
Careful with
keep/drop rules: a single keep can unintentionally drop many targets. When filtering, verify with the Service Discovery UI and reload Prometheus to confirm the effect.2) metric_relabel_configs (post-scrape): modify metrics and metric labels
Usemetric_relabel_configs to transform metrics after they are scraped. This is useful to:
- Rename metric names
- Drop or keep specific metrics
- Rename metric labels or copy label values
Remember: metric names are represented as a label whose key is
__name__. To match/rename a metric use source_labels: [__name__] and target_label: __name__.Rename a metric (change metric name)
Renamenode_cpu_seconds_total to host_cpu_seconds_total:
host_cpu_seconds_total in the expression browser.

Keep or drop entire metrics
Keep onlynode_arp_entries (drop all other metrics for the job/instance):
action: drop, the matched metrics will be removed and all others will be retained.
Example metric (for reference):
scrape_duration_seconds, scrape_samples_*) depending on your rules.
Rename a metric label (e.g., mountpoint → path)
To rename a metric label key such asmountpoint to path for metrics like node_filesystem_avail_bytes:
- This creates a new label
pathwith the value ofmountpoint. - The original
mountpointlabel still exists after this operation. To remove it, add another metric relabel rule usingaction: labeldropto dropmountpoint.
path label will appear in metric output:

Summary
- Use
relabel_configsto control which targets Prometheus scrapes and to manipulate target labels before scraping. - Use
metric_relabel_configsto transform metric names and metric labels after scraping, and to drop unneeded metrics to reduce storage and cardinality. - Always test relabel rules in a controlled environment and verify results using the Service Discovery and Expression Browser UI.
- Prometheus relabeling documentation: https://prometheus.io/docs/prometheus/latest/configuration/relabeling/
- Prometheus scrape configuration: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_configurations