How Prometheus Works: Pull, TSDB & Targets
Prometheus is a time-series database with a built-in collector, a query language and a rule engine, shipped as one Go binary. It started at SoundCloud in 2012, became the second project to graduate in the CNCF (after Kubernetes), and is the default metrics system of the cloud-native world.
The pull model
Most metric systems have agents push numbers to a server. Prometheus pulls: every scrape_interval (often 15 s or 30 s) it makes an HTTP GET /metrics request to each target and stores what comes back.
┌──────────── Prometheus ─────────────┐
targets │ service discovery → scrape loop │
linkly:8080 ◀───┤ every 15s: GET /metrics │
node:9100 ◀───┤ TSDB (local disk, 2h blocks) │──▶ PromQL API ◀── Grafana, promtool
redis-exp ◀───┤ rule engine: recording + alerting │──▶ Alertmanager ──▶ Slack, PagerDuty
└─────────────────────────────────────┘Why pull?
- Up or down is free. Every scrape writes a synthetic
upseries:1if the scrape worked,0if not. A dead target is visible immediately; with push you can't tell "no data" from "nothing happened". - The server controls the load. A misbehaving client cannot flood it.
- You can curl it.
/metricsis plain text; any human can read what Prometheus will read.
Push still has a place: short batch jobs that finish before a scrape can send their final numbers to a Pushgateway, which Prometheus then scrapes. Use it only for that ("service-level batch jobs"); it is not a general push endpoint, and it never forgets a metric unless you delete it. Prometheus can also receive data through remote write (--web.enable-remote-write-receiver) and OTLP (--web.enable-otlp-receiver), which OpenTelemetry uses.
A real scrape
This session serves a static /metrics page with Python's web server, points Prometheus at it with a one-second interval, waits until the target is up, and asks the API what it collected:
mkdir site && cat > site/metrics <<'TXT'
# HELP linkly_links_stored Short links in the database.
# TYPE linkly_links_stored gauge
linkly_links_stored 1342
# HELP linkly_redirects_total Redirects served.
# TYPE linkly_redirects_total counter
linkly_redirects_total{code="302"} 5120
linkly_redirects_total{code="404"} 37
TXT
python3 -m http.server 8080 --bind 127.0.0.1 --directory site >web.log 2>&1 &
cat > prometheus.yml <<'YML'
global:
scrape_interval: 1s
scrape_configs:
- job_name: linkly
# python's server sends no Prometheus content type; tell Prometheus how to parse it
fallback_scrape_protocol: PrometheusText0.0.4
static_configs:
- targets: ["127.0.0.1:8080"]
YML
prometheus --config.file=prometheus.yml --storage.tsdb.path=data \
--web.listen-address=127.0.0.1:9090 >prom.log 2>&1 &
for i in $(seq 100); do curl -sf 127.0.0.1:9090/-/ready >/dev/null && break; sleep 0.2; done
for i in $(seq 50); do
curl -s 127.0.0.1:9090/api/v1/query --data-urlencode 'query=up == 1' | grep -q linkly && break; sleep 0.2
done
curl -s 127.0.0.1:9090/api/v1/query --data-urlencode 'query={job="linkly"}' |
jq -r '.data.result[] | "\(.metric.__name__) \(.metric | del(.__name__) | tojson) \(.value[1])"' |
grep -v '^scrape_' | sort
curl -s 127.0.0.1:9090/api/v1/targets | jq -c '.data.activeTargets[] | {scrapeUrl, health, labels}'linkly_links_stored {"instance":"127.0.0.1:8080","job":"linkly"} 1342
linkly_redirects_total {"code":"302","instance":"127.0.0.1:8080","job":"linkly"} 5120
linkly_redirects_total {"code":"404","instance":"127.0.0.1:8080","job":"linkly"} 37
up {"instance":"127.0.0.1:8080","job":"linkly"} 1
{"scrapeUrl":"http://127.0.0.1:8080/metrics","health":"up","labels":{"instance":"127.0.0.1:8080","job":"linkly"}}
Points to notice:
- Prometheus added two labels to every series:
job(fromjob_name) andinstance(thehost:portit scraped). Every series in Prometheus is identified by its metric name plus all its labels. upappeared without the target exposing it. So didscrape_duration_seconds,scrape_samples_scrapedand friends (filtered out here because their values vary).- Prometheus 3 checks the
Content-Typeof the response. Real exporters and client libraries send the right one; for a plain file server you needfallback_scrape_protocol, otherwise the target shows as down with a content-type error. Try deleting that line.
The TSDB
Samples are stored on local disk in Prometheus's own time-series database:
| Part | What it does |
|---|---|
| Head block | the most recent ~2 hours, in memory, protected by a write-ahead log (wal/) |
| Blocks | immutable 2-hour directories (01JX…/ with chunks/, index, meta.json), compacted into larger ones over time |
| Retention | --storage.tsdb.retention.time (default 15d) and/or --storage.tsdb.retention.size |
Each sample costs roughly 1–2 bytes after compression, so storage is cheap; what is expensive is the number of series (memory for the index and head). That is the subject of Labels & Cardinality.
A single Prometheus is deliberately simple: not clustered, local disk only. For high availability you run two identical ones; for long-term or global storage you add Thanos, Mimir or a managed service (Prometheus at Scale).
Running it for real
On a server, Prometheus is one binary and one config file under systemd; in Kubernetes almost everyone uses the kube-prometheus-stack Helm chart, which installs the Prometheus Operator, Prometheus, Alertmanager, Grafana, node-exporter and kube-state-metrics in one go (Service Discovery & the Prometheus Operator).
# sample session: a local cluster
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install monitoring prometheus-community/kube-prometheus-stack -n monitoring --create-namespace
kubectl -n monitoring port-forward svc/monitoring-kube-prometheus-prometheus 9090The web UI on port 9090 has a query page (with a graph tab), Status → Targets (what the session above printed with the API) and Alerts.
Test yourself
What does the series up{job="linkly", instance="10.0.1.5:8080"} 0 tell you?
- linkly has no traffic
- The last scrape of that target failed
- The target was just added
- Alertmanager is down
When is the Pushgateway the right tool?
- For every service behind a firewall
- For short batch jobs that finish before Prometheus could scrape them
- To make Prometheus highly available
- To send alerts
Which labels does Prometheus attach to every scraped series by default?
hostandportjobandinstanceserviceandenvnameandvalue