Skip to main content

Discovery and collection

Stage 6 · Mission Operations

You will be able to: explain how a ServiceMonitor becomes a list of Pod targets, read a target's labels and health, tell a discovery failure from a scrape failure, and find the broken link when a metric is missing from a dashboard.

A Grafana panel for booking is empty. The cause could be the app, the scrape configuration, Prometheus itself, or the query behind the panel. Without a model of the chain, you poke at random. The single most useful idea in this chapter: creating a configuration object is not the same as data arriving. Every link has to be proven with data.

Prometheus needs a target list​

Prometheus collects by scraping: every interval, it sends GET /metrics to each target (one address and port) and stores what comes back. On a static network you would list the targets by hand. In Kubernetes, Pods are replaced all the time with new IPs, so the list must be discovered from the API and kept current.

Prometheus can discover targets itself, but its configuration file is long and easy to get wrong. Apollo uses the Prometheus Operator, which lets you describe what to scrape with small Kubernetes objects instead:

Custom resourceDescribesApollo has
PrometheusA Prometheus server: version, storage, retention, and which monitors and rules it selectsapollo in apollo-observability
ServiceMonitor"Scrape the Pods behind Services with these labels, on this named port and path"One per backend: booking, flight, identity, notification, search
PodMonitorThe same, selecting Pods directlyNone
ProbeBlack-box checks of a URL through a proberFrontend health through the blackbox exporter
PrometheusRuleRecording and alerting rules (next chapter)apollo-services
AlertmanagerWhere alerts are sentConfigured in alerting.yaml

The Operator watches these objects, generates Prometheus's real configuration from them, and stores it in a Secret that a sidecar in the Prometheus Pod reloads. You never edit prometheus.yml.

From ServiceMonitor to targets​

Two selections happen, and either can silently miss:

  1. Prometheus selects ServiceMonitors. Apollo's Prometheus object accepts ServiceMonitors in the apollo-observability namespace carrying app.kubernetes.io/part-of: apollo-airlines. A ServiceMonitor without that label is ignored, with no error anywhere.
  2. The ServiceMonitor selects Services. Booking's selects Services labelled app: booking in any namespace (namespaceSelector: {any: true}), and names the Service port http.

Then the important detail: Prometheus does not scrape the Service's ClusterIP. It reads the Service's endpoints and scrapes each Pod IP directly. Scraping the ClusterIP would reach a random Pod each time, mixing two Pods' counters into one series. One target per Pod keeps each Pod's counters separate, and a Pod that is not Ready drops out of the endpoints and so out of the targets.

To do all this, Prometheus's ServiceAccount needs permission to list and watch Services, endpoints and Pods across namespaces. Missing RBAC shows up as discovery errors in Prometheus's own log, not as a missing object.

# servicemonitors/booking-sm.yaml (trimmed)
metadata:
labels: {app.kubernetes.io/part-of: apollo-airlines} # 1. Prometheus selects this
spec:
selector: {matchLabels: {app: booking}} # 2. selects the Service
namespaceSelector: {any: true}
endpoints:
- port: http # the Service port's NAME
path: /metrics
interval: 30s
scrapeTimeout: 10s
honorLabels: true
jobLabel: app

Booking's Pods also carry prometheus.io/scrape-style annotations. The Operator ignores them; they only matter to hand-written Prometheus configurations that look for them.

What a target looks like​

Every sample scraped from a target gets target labels added by Prometheus:

LabelValue for one booking PodFrom
jobbookingjobLabel: app (the Service's app label)
instance10.244.1.7:8082The address scraped
namespace, service, pod, container, endpointapollo-airlines-apps, booking, booking-7d…, …Kubernetes discovery

Booking's own metrics already carry a service label. honorLabels: true tells Prometheus to keep the app's value when the two collide, instead of renaming it to exported_service.

Prometheus also writes a few series of its own for every target, after every scrape:

SeriesMeaning
up1 if the last scrape succeeded, 0 if it failed
scrape_duration_secondsHow long the scrape took (compare with scrapeTimeout)
scrape_samples_scrapedHow many samples the target returned

up is the cheapest health signal in the system: it exists precisely because Prometheus pulls.

Diagnose from the inside out​

An empty panel means at least one of four links is broken. Test them in order, starting at the app:

LinkOwnerWhat a break looks likeEvidence
1. The app exposes the metricThe app/metrics fails, or the metric name is absentcurl inside the Pod
2. DiscoveryServiceMonitor, Service labels, RBACThe target is missing from Prometheus's target list/api/v1/targets
3. ScrapePrometheus, networkThe target is listed with health: down; up == 0lastError on the target
4. QueryWhoever wrote the panelSamples exist, but the panel's query selects nothingRun the panel query yourself
Common errorLinkFix
ServiceMonitor lacks the label the Prometheus object selects2Read the real serviceMonitorSelector; do not copy another chart's convention
port: 8082 instead of port: http2port takes the Service port's name (targetPort takes a number)
Service has no app: booking label, or a different port name2Compare kubectl get svc booking --show-labels with the selector
App listens on another port or path3lastError shows connection refused or 404
Scrape takes longer than scrapeTimeout3lastError: context deadline exceeded
Label typo in the panel query (service="bookings")4Query http_requests_total{service=~"book.*"}

Missing and down are different problems: missing means Prometheus never learned about the target; down means it knows and cannot read it.

Apollo example​

ObjectSelects
Prometheus apolloServiceMonitors, PrometheusRules and Probes labelled part-of: apollo-airlines in apollo-observability; Alertmanager alertmanager
5 ServiceMonitorsServices app: booking, flight, identity, notification, search; port http; every 30 s
Probehttp://frontend.apollo-airlines-ui.svc:3000/healthz through the blackbox exporter

The frontend is nginx serving static files and has no /metrics, so it is checked from the outside instead: the blackbox exporter requests the URL and reports probe_success. That is the difference between white-box monitoring (the app reports on itself) and black-box (something else tries it the way a user would). Prometheus keeps its samples for 30d on a 1 Gi volume.

Try it​

Unverified

Not yet run on a live cluster for this documentation revision. Needs Stage 6 with at least the metrics substage installed. The break is undone by the last command.

kubectl -n apollo-observability port-forward svc/prometheus 19090:9090 >/dev/null &
targets() { curl -s localhost:19090/api/v1/targets | jq -r '.data.activeTargets[] | "\(.labels.job) \(.labels.pod) \(.health) \(.lastError)"'; }

targets # two healthy targets per backend: one per Pod
curl -sG localhost:19090/api/v1/query --data-urlencode 'query=up{job="search"}' | jq -c '.data.result[] | [.metric.pod, .value[1]]'

# Break link 2: the ServiceMonitor no longer selects any Service
kubectl -n apollo-observability patch servicemonitor search --type=merge \
-p '{"spec":{"selector":{"matchLabels":{"app":"nonexistent"}}}}'
sleep 60
targets | grep search || echo "search targets are gone, not down"
kubectl -n apollo-observability get servicemonitor search # the object still exists

# Restore from the chart
bash stages/stage6/scripts/signals-lab.sh apply 1 helm
sleep 60; targets | grep search
kill %1
  • Each backend had one target per Pod, not one per Service.
  • With a broken selector the object still existed and nothing reported an error; the targets simply disappeared. That is a discovery failure.

Common misconceptions​

  • "The ServiceMonitor exists, so Prometheus is collecting." Check the target list and that new samples arrive.
  • "Prometheus scrapes the Service." It uses the Service to find Pods, then scrapes each Pod.
  • "A missing target and a down target are the same problem." Missing is discovery; down is the scrape.
  • "port in a ServiceMonitor is a number." It is the Service port's name.
  • "prometheus.io/scrape annotations configure scraping." Not with the Operator.

Check yourself​

A target is listed with health: down. Discovery problem or scrape problem?

Scrape. Discovery worked, because the target is listed. Read its lastError, then check the port, the path and the network.

Why does booking produce two targets instead of one?

Prometheus scrapes each Pod behind the Service separately, so each replica's counters stay a separate series.

You add a ServiceMonitor for a new service, and nothing appears. The object is valid. What is the first thing to check?

Whether it carries the label and sits in the namespace that the Prometheus object's serviceMonitorSelector and serviceMonitorNamespaceSelector require.

Where this leads​

Samples are now arriving. Queries, alerts and objectives turns them into answers, into notifications, and into a definition of "good enough" for booking.

References​