Pcornel
Browse topics
Options
Recent
Favorites
Observability · Technical reference

Service Discovery in Prometheus

Direct answer

Prometheus service discovery finds scrape targets dynamically (Kubernetes, Consul, EC2, file_sd, DNS) and relabelling turns discovered metadata into target labels or filters.

When to use this

Use it when targets appear, disappear or get wrong labels.

First 30 seconds

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.

Operational verification

SD source → discovered targets → relabel → scrape targets

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
OpenTelemetry / Prometheus / Grafana / common metrics-log-trace pipelines
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformOpenTelemetry / Prometheus / Grafana / common metrics-log-trace pipelines
Scope / prerequisiteUse it when targets appear, disappear or get wrong labels.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

Check Service Discovery page: discovered labels before relabel vs target labels after. A keep/drop rule may be filtering your target.

Key checks
  • curl -s localhost:9090/api/v1/targets?state=dropped — first check for this topic
  • curl -s localhost:9090/-/ready — Prometheus ready
  • curl -s 'localhost:9090/api/v1/query?query=up' — scrape targets up/down
  • promtool 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

WhereShows
Status → Service DiscoveryEvery discovered target with labels before relabelling, and which were dropped
Status → TargetsKept targets, last scrape, error text
prometheus_sd_discovered_targetsCount per SD config
prometheus_target_scrape_pool_targetsCount 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • Relabelling dropping targets unintentionally.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

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.

Aliases that resolve here
service discovery in prometheusprometheus service discoveryrelabel_configs
Confirmation

Confirm action

Continue?