Skip to content
Last9 Last9

Scrape selective metrics in Prometheus

Recipe to only scrape selective metrics in Prometheus to reduce cardinality

Prometheus provides the ability to filter specific metrics post-scraping, via the metrics_relabel_config stanza.

We will use the node_exporter Prometheus exporter as an example, and send only metrics matching the regex node_cpu.*.

Prometheus scrape config without filtering

Terminal window
global:
scrape_interval: 1m
scrape_configs:
- job_name: 'node-exporter-01'
static_configs:
- targets: [ 'localhost:9100' ]

This scrapes and stores all node exporter metrics.

Prometheus scrape config with filtering

Terminal window
global:
scrape_interval: 1m
scrape_configs:
- job_name: 'node-exporter-01'
static_configs:
- targets: [ 'localhost:9100' ]
metric_relabel_configs:
- source_labels: [__name__]
action: keep
regex: '(node_cpu)'

This will scrape all metrics, but drop anything that does not match the entries in the regex section.