What Is Prometheus? Pull-Based Monitoring Explained
In the last lab, curl localhost:8000/metrics showed shop-demo's request counts, but only as they are right now. Nobody writes them down, so you cannot see how they changed, and nobody notices when the app stops answering. So what is Prometheus? It is the tool that fetches that page again and again and keeps what it finds. This lesson shows how, and why it asks instead of waiting.
The roll call: Prometheus pulls the numbers
Prometheus is an open-source monitoring system that collects metrics, stores them and lets you query and alert on them. As What Is a Metric? explained, it stores them as time series: timestamped values identified by a metric name and labels.
Prometheus uses a pull model: it starts every collection itself. Once per scrape interval, 15 seconds in this lab, it sends an HTTP GET request to the /metrics page of each target, a program on its list, and reads the reply. That fetch is a scrape. The app never needs to know where Prometheus is; it only answers when asked.
The flowchart follows one scrape of shop-demo. In the failure branch, Prometheus notices the missing answer because it does the asking.
Steps 3 and 4 turn a page of numbers into history. Prometheus reads each line in the text format from that lesson, then stores its value as a sample, stamped with the scrape time, in its time-series database. Unlike the teacher, it does not ask one target at a time: each target has its own loop, so a slow one does not hold up the rest.
What Prometheus is made of
One Prometheus program does five jobs, shown in the middle of the diagram:
- Scraper: fetches each target's
/metricson schedule. - Time-series database (TSDB): keeps the samples on local disk, compressed, for 15 days by default.
- PromQL query engine: answers questions written in PromQL, Prometheus's query language, which PromQL Basics teaches.
- Rules: queries run on a schedule, to save a result as a new series or to fire an alert when a condition holds.
- Web UI and HTTP API: the pages on port 9090, and the API that tools such as Grafana query.
Around it sit separate programs. Exporters, the subject of the next lesson, publish other systems' stats as /metrics pages. Alertmanager receives firing alerts and decides who is told. Grafana draws dashboards from PromQL answers.
Targets, jobs and the up metric
A target is one endpoint that Prometheus scrapes, such as shop-demo:8000; the Prometheus docs also call it an instance. A job is a named group of targets with the same purpose, for example three copies of one app. Both come from prometheus.yml; here is the lab's, without comments:
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ["localhost:9090"]
- job_name: shop-demo
static_configs:
- targets: ["shop-demo:8000"]
Each entry under scrape_configs is a job, and static_configs gives its fixed list of targets. Prometheus scrapes itself as the job prometheus, because it serves its own /metrics page too. With no path or scheme (http or https) set, it fetches http://shop-demo:8000/metrics. evaluation_interval is how often rules run; there are none yet.
To every series it scrapes, Prometheus adds two labels: job, the job's name, and instance, the target's host:port. You see both on the Target health page, sketched below, which lists every target with its labels, its last scrape and its state.
On every scrape, Prometheus also writes a few series of its own. The most useful is up: 1 when the last scrape of that target worked, 0 when it failed through a refused connection, a timeout, an error status or an unreadable reply. Because up is written even when a target stays silent, it is the usual base for a "target down" alert (lesson 7).
Start the Prometheus lab
The lab is in course-labs/open-source-observability-stack/02-prometheus-alertmanager. Its docker-compose.yml starts six services: shop-demo and loadgen from the previous lab, with their code mounted from ../01-lab-setup, plus node-exporter, alertmanager and webhook, which Prometheus ignores until lessons 4 and 8. Here is the prometheus service, without its health check and comments:
prometheus:
image: prom/prometheus:v3.16.0
command:
- --config.file=/etc/prometheus/prometheus.yml
- --storage.tsdb.path=/prometheus
- --web.enable-lifecycle
volumes:
- ./prometheus:/etc/prometheus:ro
- prometheus-data:/prometheus
ports:
- "127.0.0.1:9090:9090"
networks:
- obs-lab
The first two flags point to the config, in the read-only prometheus folder, and the database, in the prometheus-data volume. --web.enable-lifecycle allows reloading an edited config over HTTP, which the next lesson uses. The health check asks Prometheus's /-/ready page, so docker compose ps shows (healthy) once it can answer queries.
Pull or push: which fits when
In a push model, the app sends its data to the monitoring system whenever it chooses; in this course, traces and profiles travel that way. The Prometheus FAQ calls pull "slightly better" than push, but not a major point when choosing a monitoring system.
Pull fits long-running services that Prometheus can reach, such as web apps, databases and machines. You get up for free, you can inspect a target yourself with curl or a browser, and you can start a second Prometheus that scrapes the same targets without changing them.
Push fits when Prometheus cannot ask. A short-lived job, such as a backup script that runs for a few seconds, can start and finish between two scrapes, leaving no page to fetch. For that case there is the Pushgateway, a separate server on port 9091 by default: the job pushes its final numbers to it before it exits, and Prometheus scrapes the Pushgateway like any other target.
Use it sparingly. The Prometheus docs call capturing the outcome of a service-level batch job, one not tied to a single machine, usually its only valid use. The Pushgateway never forgets a pushed series unless you delete it, and the job loses its own up series. For a target behind a firewall, the docs suggest running Prometheus inside that network instead.
Check yourself
shop-demo crashes at 10:00:05. How does Prometheus find out, and what does up show?
At the next scrape, at most 15 seconds later, the GET request fails, so Prometheus stores up = 0 for job="shop-demo". It notices because it asks; with push, silence looks the same as having nothing to report.
You start a new service on obs-lab with a working /metrics page, but it never appears on the Target health page. Why?
Prometheus scrapes only the targets its configuration names, like a teacher who calls only the names on the class list. Add a job for it to prometheus.yml, as the next lesson does for the node exporter.
A nightly script runs for 5 seconds and exits. Why might Prometheus never see its metrics, and what helps?
With a 15-second interval, the script can start and finish between two scrapes, so there may never be a page to fetch. Have it push its result to the Pushgateway, which Prometheus scrapes, before it exits.
Go deeper
- Setup Kubernetes cluster monitoring the easy way: Prometheus on Kubernetes via Helm, where ServiceMonitors replace the hand-written target list.
- Monitoring NGINX Ingress Controller using Open-source tools: Prometheus scraping a real component's metrics for Grafana dashboards.