relabel_configs (runs before scraping) and metric_relabel_configs (runs after scraping). The same relabel rules apply regardless of the service discovery mechanism — only the discovered labels differ.
Below are example discovered labels for node targets (shortened to the labels used in the examples):
env— environment (dev / staging / prod)team— responsible team (web / database)
Relabeling runs at two distinct phases:
relabel_configs— applied to discovered targets before scraping (decide which targets to scrape, and transform target labels).metric_relabel_configs— applied after scraping, giving you access to metrics and their labels (transform metric names and metric labels, drop or keep specific metrics).
How relabeling flows work
Quick reminders that will help you design rules:- When multiple
source_labelsare listed, their values are concatenated with;before regex evaluation. Capture groups map to$1,$2, etc., in that order. - Use anchored regexes (
^...$) when you need exact matches. relabel_configsshape target metadata and control which endpoints are scraped.metric_relabel_configsoperate on scraped metric samples (including__name__) and are the place to rename metrics, drop metrics, or transform metric labels.
Filter targets by environment (relabel_configs — before scraping)
To keep only targets whereenv="prod", add a relabel_configs entry to the scrape job. Here source_labels values are concatenated and matched with regex. Use action: keep to preserve matches and drop all others.
action: drop with the same regex, targets with env="prod" will be removed and everything else retained.
Tip: To keep multiple environments, use alternation in the regex like ^(prod|staging)$.
Example outcome after keep for env=prod (target list contains only production targets):
Combine labels into a new label (replace)
To create a composed labelinfo that joins team and env (for example database-prod), use a replace action with two source_labels. The order of source_labels determines $1, $2, etc. Because source_labels are joined with ;, the regex should expect a semicolon between captures.
$1isteam,$2isenv.target_label: infocreates the new labelinfowith the combined value.
Drop a discovered label (labeldrop / labelkeep)
To remove an unwanted discovered label such assize from target labels, use action: labeldrop with a regex that matches the label key:
labeldropaccepts a regex matching label keys to remove (e.g.,^size$).labelkeepis complementary: it keeps only the label keys that match and drops all others.
labeldrop: ^size$, the size label will no longer be present in target labels.
Be careful:
labeldrop and labelkeep operate on label keys. An overly broad regex may remove labels you intended to keep. Test rules incrementally in a non-production environment when possible.Metric relabeling (after scraping)
metric_relabel_configs run after scraping and operate on scraped samples. You can rename metric names (via the special __name__ label), keep or drop entire metrics, or transform metric labels.
Rename a metric name
To rename the metricnode_cpu_seconds_total to host_cpu_seconds_total:
- Matching
__name__lets you change the metric name. - Anchoring the regex with
^and$ensures exact matching.
host_cpu_seconds_total in place of node_cpu_seconds_total.
Keep only a specific metric (drop everything else)
To retain onlynode_arp_entries and drop all other scraped metrics for that scrape job:
action: keeppreserves matching metrics and drops others for the job.- Note: some Prometheus internal or exporter-side metrics (e.g.,
up, scrape-related metrics) may still appear depending on the context.
Rename a metric label key (e.g., mountpoint → path)
To copy themountpoint label value into a new label called path:
path with the same value as mountpoint. To remove the original mountpoint label so only path remains, add a labeldrop rule:
- The first rule creates
path. - The second removes the original
mountpointlabel from the metric.
Summary and best practices
- Use
relabel_configsto shape discovered target metadata, control which endpoints are scraped, and build composite labels for better grouping. - Use
metric_relabel_configsto transform scraped metric names and labels, or to drop/keep specific metrics. - Always anchor regexes (
^...$) for exact matches, and remembersource_labelsare concatenated with;(so capture groups must account for that separator). - Test rules progressively (start with non-production scrape jobs) and validate results in Prometheus targets and label explorers before rolling out broad changes.
Links and references
- Prometheus relabeling documentation
- Prometheus scrape configuration
- Prometheus metric relabeling guide