Use it when targets appear, disappear or get wrong labels.
Service Discovery in Prometheus
Prometheus service discovery finds scrape targets dynamically (Kubernetes, Consul, EC2, file_sd, DNS) and relabelling turns discovered metadata into target labels or filters.
1. Discovered vs kept: Status → Service Discovery, or curl -s localhost:9090/api/v1/targets?state=dropped.
2. relabel_configs rules.
3. Kubernetes annotations/ServiceMonitor selectors.
4. Permissions for the SD API.
5. Target labels as expected.
SD source → discovered targets → relabel → scrape targets
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Compare the expected number of targets with what Prometheus scrapes. Check RBAC allows Prometheus to list the Kubernetes objects.
Step 2 Common failures Narrow the fault
A keep rule on a missing annotation drops every target. Missing RBAC returns no targets without obvious errors. Scraping both Pods and Services duplicates series.
Check Service Discovery page: discovered labels before relabel vs target labels after. A keep/drop rule may be filtering your target.
curl -s localhost:9090/api/v1/targets?state=dropped— first check for this topiccurl -s localhost:9090/-/ready— Prometheus readycurl -s 'localhost:9090/api/v1/query?query=up'— scrape targets up/downpromtool check config prometheus.yml— config valid
ReferenceWhat it is1 section · depth on demand
How it works
Discovery refreshes target lists periodically. Each target carries __meta_* labels from its source; relabel_configs decide which targets to keep and what the final labels are. Labels starting with __ are removed after relabelling.
ReferenceImportant terms & boundaries3 sections · depth on demand
Kubernetes discovery example
scrape_configs:
- job_name: pods
kubernetes_sd_configs: [{ role: pod }]
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: "true"
- source_labels: [__meta_kubernetes_namespace]
target_label: namespace
Documented example, not captured output. Other mechanisms: file_sd_configs, consul_sd_configs, ec2_sd_configs, dns_sd_configs. The Prometheus Operator uses ServiceMonitor/PodMonitor objects instead. Check the Targets and Service discovery pages for dropped targets.
In depth
From discovered object to scraped target
1. A discovery mechanism (kubernetes_sd_configs, file_sd_configs, consul_sd_configs, cloud SDs …) returns targets with __address__ and many __meta_* labels.
2. relabel_configs run before the scrape: keep or drop targets, set __address__, __metrics_path__, __scheme__, and copy metadata into normal labels.
3. Labels starting with __ are removed after relabelling.
4. metric_relabel_configs run after the scrape, on every sample.
Annotation-driven pod scraping
relabel_configs:
- { source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape], regex: "true", action: keep }
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
regex: '([^:]+)(?::\d+)?;(\d+)'
replacement: '$1:$2'
target_label: __address__
- { source_labels: [__meta_kubernetes_namespace], target_label: namespace }
- { source_labels: [__meta_kubernetes_pod_name], target_label: pod }
With the Prometheus Operator, ServiceMonitor and PodMonitor objects generate this configuration; their selector must match the Service/Pod labels and the Prometheus object's serviceMonitorSelector must match the monitor's labels — a mismatch at either level gives no targets and no error.
Debugging discovery
| Where | Shows |
|---|---|
| Status → Service Discovery | Every discovered target with labels before relabelling, and which were dropped |
| Status → Targets | Kept targets, last scrape, error text |
prometheus_sd_discovered_targets | Count per SD config |
prometheus_target_scrape_pool_targets | Count per job after relabelling |
If discovered is non-zero and kept is zero, a keep rule does not match; if discovered is zero in Kubernetes, check the ServiceAccount's RBAC for get/list/watch on pods, services and endpointslices.
Common mistakes
- Relabelling dropping targets unintentionally.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.