Prometheus Relabel Config Not Matching Targets Fix
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
keepto include targets,dropto exclude targets - Test relabel configs on a single job before rolling out
- Check target labels in Prometheus UI after relabeling
- Use
labelmapto copy prefixed labels - Document relabel rules for team understanding
Common Mistakes with relabel config
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro