TExcelTAD
Home › Observability: Prometheus, Grafana, Loki & OpenTelemetry › Scrape Configuration & Relabeling

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

yaml
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: staging

promtool check config validates syntax, types and referenced files (rule files are checked too):

bash · try it yourself
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.yml
Checking 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:

bash · try it yourself
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?

  1. None
  2. relabel_configs act on targets before scraping; metric_relabel_configs act on every scraped sample
  3. metric_relabel_configs run in Grafana
  4. relabel_configs only work with Kubernetes

A relabel rule has regex: canary. Does it match the value canary-2?

  1. Yes
  2. No, relabel regexes are fully anchored
  3. Only with action: keep
  4. Only in metric_relabel_configs

Which offline command shows targets before and after relabeling?

  1. promtool check rules
  2. promtool check service-discovery prometheus.yml <job>
  3. prometheus --dry-run
  4. amtool config routes