Skip to content

Prometheus Relabel Config Not Matching Targets Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Prometheus Relabel Config Not Matching Targets Fix. We cover key concepts, practical examples, and best practices.

Your Prometheus scrape targets show up with wrong labels or are missing entirely — the relabel configuration drops targets unexpectedly, or labels are not transformed as expected. Relabel order matters, and the wrong action can silently drop targets.

The Problem

relabel_configs:
  - source_labels: [__meta_kubernetes_pod_label_app]
    regex: "myapp"
    action: drop

All pods with label app=myapp are dropped from scraping, but you intended to keep only myapp pods. The action: drop drops matching targets — use keep to retain matching targets.

Step-by-Step Fix

1. Use keep instead of drop for inclusive filtering

scrape_configs:
  - job_name: 'kubernetes-pods'
    kubernetes_sd_configs:
      - role: pod
    relabel_configs:
      # Keep only pods with app=myapp
      - source_labels: [__meta_kubernetes_pod_label_app]
        regex: "myapp"
        action: keep

2. Understand relabel order

relabel_configs:
  # Step 1: Filter targets (keep/drop)
  - source_labels: [__meta_kubernetes_pod_label_app]
    regex: "myapp"
    action: keep

  # Step 2: Replace labels
  - source_labels: [__meta_kubernetes_pod_label_app]
    target_label: app
    action: replace

  # Step 3: Set static labels
  - target_label: environment
    replacement: production

3. Debug relabel with Prometheus UI

# Use action: replace with replacement to debug
relabel_configs:
  - source_labels: [__address__]
    target_label: debug_addr
    action: replace
    replacement: "${1}"

Check targets at http://<a href="/devops/prometheus-grafana/">prometheus</a>:9090/targets — the debug_addr label shows the original address.

4. Map metadata labels to Prometheus labels

relabel_configs:
  # Map Kubernetes pod labels to Prometheus labels
  - source_labels: [__meta_kubernetes_pod_label_app]
    target_label: app
  - source_labels: [__meta_kubernetes_pod_label_version]
    target_label: version
  - source_labels: [__meta_kubernetes_pod_node_name]
    target_label: node

5. Use labelmap for prefix-based mapping

relabel_configs:
  # Copy all labels with __meta_kubernetes_pod_label_ prefix
  # Remove the prefix to create clean labels
  - action: labelmap
    regex: __meta_kubernetes_pod_label_(.+)
    replacement: "${1}"

Expected output:

Before relabel:
  __address__: "10.0.0.1:8080"
  __meta_kubernetes_pod_label_app: "myapp"
  __meta_kubernetes_pod_label_version: "v2"

After relabel:
  __address__: "10.0.0.1:8080"
  app: "myapp"
  version: "v2"

Prevention Tips

  • Use keep to include targets, drop to exclude targets
  • Test relabel configs on a single job before rolling out
  • Check target labels in Prometheus UI after relabeling
  • Use labelmap to copy prefixed labels
  • Document relabel rules for team understanding

Common Mistakes with relabel config

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world PROMETHEUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What's the difference between replace and labelmap?

replace sets a specific target label from a source label — good for mapping one label at a time. labelmap applies a regex to ALL label names and creates new labels from matches — good for bulk copying prefixed labels like __meta_*.

Why does my relabel config drop ALL targets?

Common causes: (1) action: drop with a too-broad regex that matches everything, (2) source_labels list includes a missing label (makes the entire list empty), or (3) regex uses Prometheus default (.+) which matches all non-empty values.

Can I use relabeling to modify metric names?

No, relabeling changes labels on targets, not metric names within the scrape. To modify metric names, use metric_relabel_configs which applies relabeling to scraped samples after the scrape completes.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro