Skip to main content
In this lesson we cover Prometheus relabeling: how to rewrite label sets for discovered targets and scraped metrics. Relabeling is essential for classifying, filtering, and normalizing targets and metrics before ingestion or during metric processing. Common use cases
  • 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.
Two relabeling phases Example showing both sections (placeholders wrapped in backticks to avoid MDX/JSX parsing):
Service discovery (for example, Amazon EC2) exposes many __meta_* labels. Example:
These __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:
The 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 more source_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:
Fields explained
  • source_labels: array of label names to concatenate and match.
  • regex: applied to the joined source_labels values.
  • action: the operation to perform when the regex matches.
Action semantics (common) Example: drop any target tagged with env=dev:

Matching multiple source labels

When multiple source_labels are specified, Prometheus concatenates their values using a delimiter (default ;) before applying regex. Example: keep only targets where env=dev and team=marketing:
Change the join delimiter with 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:
Explanation:
  • regex captures the portion before the colon in group 1.
  • replacement: '$1' places the captured value into the new ip label.
You can also drop or retain labels by name using labeldrop and labelkeep. Example: drop a discovery label named __meta_ec2_owner_id:
Example: keep only instance and job labels and drop all others:
The image shows a table of discovered labels and target labels for an EC2 instance, with a note explaining that labels beginning with "__" will be discarded after re-labeling.

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:
This preserves the values but makes the keys normal target label names visible on scraped series.

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:
Rename a metric (change the metric name):
Drop a metric label (remove code from all metrics):
Create a new metric label by transforming an existing label. Example: turn path="/api/v1" into endpoint="api/v1" by stripping the leading slash:
Notes:
  • Use capture groups (...) in regex and reference them as $1, $2, etc. in replacement.
  • relabel_configs has access to service-discovery metadata (the __meta_* labels); metric_relabel_configs has 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


Use these references to deepen your understanding and validate edge cases for complex relabeling pipelines.

Watch Video