Scrape Configuration & Relabeling
prometheus.yml says what to scrape, how often, and how to rewrite labels on the way in. This lesson writes one for linkly and uses two offline promtool commands to check it.
The shape of prometheus.yml
global:
scrape_interval: 30s # default for every job
scrape_timeout: 10s
evaluation_interval: 30s # how often rules run
external_labels: # added when data leaves this server (remote write, federation, alerts)
cluster: prod-eu-1
rule_files:
- rules/*.yml
alerting:
alertmanagers:
- static_configs:
- targets: ["alertmanager:9093"]
scrape_configs:
- job_name: linkly
metrics_path: /metrics # the default
scheme: http
scrape_interval: 15s # per-job override
static_configs:
- targets: ["linkly-1:8080", "linkly-2:8080"]
labels:
env: stagingpromtool check config validates syntax, types and referenced files (rule files are checked too):
mkdir rules
cat > rules/linkly.yml <<'YML'
groups:
- name: linkly
rules:
- record: job:http_requests:rate5m
expr: sum by (job) (rate(http_requests_total[5m]))
YML
cat > prometheus.yml <<'YML'
global:
scrape_interval: 30s
evaluation_interval: 30s
rule_files:
- rules/*.yml
scrape_configs:
- job_name: linkly
scrape_interval: 15s
static_configs:
- targets: ["linkly-1:8080", "linkly-2:8080"]
labels: {env: staging}
YML
promtool check config prometheus.yml
echo "--- a typo:"
sed -i 's/scrape_interval: 15s/scrape_intervall: 15s/' prometheus.yml
promtool check config prometheus.ymlChecking prometheus.yml
SUCCESS: 1 rule files found
SUCCESS: prometheus.yml is valid prometheus config file syntax
Checking rules/linkly.yml
SUCCESS: 1 rules found
--- a typo:
Checking prometheus.yml
FAILED: parsing YAML file prometheus.yml: yaml: unmarshal errors:
line 8: field scrape_intervall not found in type config.ScrapeConfig
Prometheus itself refuses to start (or to reload) with an invalid file, keeping the old config on reload. Run the check in CI on every change, and reload with kill -HUP <pid> or curl -X POST localhost:9090/-/reload (needs --web.enable-lifecycle).
Relabeling
Relabeling rewrites the label set of a target before it is scraped (relabel_configs) or of each sample after (metric_relabel_configs). Service discovery hands Prometheus targets with lots of __meta_* labels (Kubernetes pod name, namespace, annotations, cloud tags); relabeling decides which targets to keep and which of those labels survive. Labels starting with __ are dropped after relabeling.
Each rule reads source_labels (joined with ;), matches regex (fully anchored), and does an action:
| action | effect |
|---|---|
replace (default) |
set target_label to replacement (with $1 capture groups) |
keep / drop |
keep / drop the target (or sample) if the regex matches |
labelmap |
copy every label matching regex to a new name (replacement) |
labeldrop / labelkeep |
remove / keep labels whose names match |
hashmod |
target_label = hash(source) % modulus, for sharding targets over servers |
lowercase / uppercase |
change case |
keepequal / dropequal |
keep / drop when the source equals target_label |
promtool check service-discovery runs discovery and relabeling for one job and prints what each target looks like before and after, without scraping anything. Here it drops canary targets, derives a host label from the address, and copies team into owner:
cat > prometheus.yml <<'YML'
scrape_configs:
- job_name: linkly
static_configs:
- targets: ["10.0.1.5:8080", "10.0.1.6:8080"]
labels: {env: staging, team: links}
- targets: ["10.0.2.9:8080"]
labels: {env: canary, team: links}
relabel_configs:
- source_labels: [env]
regex: canary
action: drop
- source_labels: [__address__]
regex: '(.+):\d+'
target_label: host
replacement: '$1'
- source_labels: [team]
target_label: owner
- regex: team
action: labeldrop
YML
promtool check service-discovery prometheus.yml linkly |
jq -c '.[] | {address: .discoveredLabels.__address__,
kept: (.labels | with_entries(select(.key | startswith("__") | not)))}'{"address":"10.0.1.5:8080","kept":{"env":"staging","host":"10.0.1.5","instance":"10.0.1.5:8080","job":"linkly","owner":"links"}}
{"address":"10.0.1.6:8080","kept":{"env":"staging","host":"10.0.1.6","instance":"10.0.1.6:8080","job":"linkly","owner":"links"}}
{"address":"10.0.2.9:8080","kept":{}}
The canary target comes back with an empty label set: dropped. The other two get host and owner, lose team, and Prometheus fills in instance from __address__. Because the regex is anchored, '(.+):\d+' must match the whole address.
Useful per-job settings
| Setting | Use |
|---|---|
honor_labels: true |
keep the target's own job/instance labels on conflict (federation, Pushgateway) |
params |
query parameters, e.g. module: [http_2xx] for the blackbox exporter |
basic_auth, authorization, tls_config |
authenticated or TLS targets |
sample_limit, label_limit |
guard rails against cardinality explosions |
scrape_protocols, fallback_scrape_protocol |
content negotiation (Prometheus 3) |
scrape_native_histograms, always_scrape_classic_histograms |
native vs classic histograms during migrations |
Test yourself
What is the difference between relabel_configs and metric_relabel_configs?
- None
- relabel_configs act on targets before scraping; metric_relabel_configs act on every scraped sample
- metric_relabel_configs run in Grafana
- relabel_configs only work with Kubernetes
A relabel rule has regex: canary. Does it match the value canary-2?
- Yes
- No, relabel regexes are fully anchored
- Only with action: keep
- Only in metric_relabel_configs
Which offline command shows targets before and after relabeling?
promtool check rulespromtool check service-discovery prometheus.yml <job>prometheus --dry-runamtool config routes