- Filter discovered targets you don’t want to scrape (e.g., non-production instances).
- Rename or extract parts of labels (for example, remove ports from addresses).
- Drop unwanted labels or specific metrics to reduce cardinality.
Example showing both sections (placeholders wrapped in backticks to avoid MDX/JSX parsing):
__meta_* labels. Example:
__meta_* labels are available during pre-scrape relabeling but are discarded after the relabeling pipeline unless you explicitly map them to target labels.
If you inspect scraped series you might see entries such as:
instance and job labels are examples of target labels that will appear on scraped time series. Discovery-specific labels beginning with __ are metadata only and will not be preserved unless mapped.
Relabeling rules are evaluated sequentially. A common pitfall is using
action: keep without care — any target not matched by a keep rule is implicitly dropped. Validate your rules to avoid unintentionally excluding targets.Basic relabeling (pre-scrape)
A relabeling rule typically reads one or moresource_labels, joins them (with a separator), applies a regex, and performs an action such as keep, drop, or replace.
Example: keep only targets discovered with env=prod:
source_labels: array of label names to concatenate and match.regex: applied to the joinedsource_labelsvalues.action: the operation to perform when the regex matches.
Example: drop any target tagged with
env=dev:
Matching multiple source labels
When multiplesource_labels are specified, Prometheus concatenates their values using a delimiter (default ;) before applying regex.
Example: keep only targets where env=dev and team=marketing:
separator:
Turning discovery metadata into target labels
Discovery labels that begin with__ (for example, __meta_ec2_*) are metadata and are discarded after relabeling unless you explicitly map them to target labels.
Example: extract the IP from __address__ (e.g., 172.31.1.156:80) and save it to a target label named ip:
regexcaptures the portion before the colon in group 1.replacement: '$1'places the captured value into the newiplabel.
labeldrop and labelkeep.
Example: drop a discovery label named __meta_ec2_owner_id:
instance and job labels and drop all others:

Mapping discovery keys into target label names: labelmap
labelmap renames label keys (not values). A common use is to convert discovery keys like __meta_ec2_AMI into readable target labels like ec2_AMI.
Example: map all __meta_ec2_* keys to ec2_* target labels:
Metric relabeling (post-scrape)
metric_relabel_configs runs after a scrape and operates on each metric and its labels. The fields are the same as relabel_configs, but now __name__ refers to the metric name.
Example: drop an entire metric called http_errors_total:
code from all metrics):
path="/api/v1" into endpoint="api/v1" by stripping the leading slash:
- Use capture groups
(...)inregexand reference them as$1,$2, etc. inreplacement. relabel_configshas access to service-discovery metadata (the__meta_*labels);metric_relabel_configshas access to scraped metric names and labels (__name__and metric labels).
All relabeling rules are applied in order. Test and verify your relabeling pipeline in a staging environment, and use the Prometheus web UI service discovery pages and target pages to inspect label lifecycles and debug rules.
Quick reference: common actions and examples
Links and references
- Prometheus relabeling docs
- Prometheus metric_relabel_configs docs
- Kubernetes Service Discovery
- Amazon EC2 documentation