Learn Labs
Foundations

Architecture

Server, exporters, Alertmanager, Pushgateway, Grafana, and the scrape loop

Prometheus is one statically linked Go binary. Inside it are four jobs that run continuously: discover targets, scrape them, store the samples, and evaluate rules. Everything else in the stack is a separate process with a narrow job.

scrapealertsnotifyPromQLappsclient librariesexportersnode, postgres, …Pushgatewaybatch jobs push herePrometheusscrape, store, rulesAlertmanagerroute, group, silencereceiverspager, chat, webhookGrafanadashboards

Grafana sits beside Prometheus, not in the data path. It sends PromQL queries to Prometheus's HTTP API and draws the results.

The components

ComponentJobTalks to
Prometheus serverDiscovers targets, scrapes them, stores samples in its local TSDB, evaluates recording and alerting rules, serves PromQLTargets (pull), Alertmanager (push alerts)
ExportersTranslate something that doesn't speak Prometheus (Linux /proc, Postgres pg_stat_*, an HTTP probe) into a /metrics pageThe thing they export
Client librariesLet your own code expose /metrics directly (Go, Java, Python, Node, ...)Nothing; they just serve the page
AlertmanagerReceives firing alerts, deduplicates, groups, applies silences and inhibitions, sends notificationsReceivers (Slack, email, PagerDuty, webhooks)
PushgatewayHolds the last metrics pushed by short-lived jobs so Prometheus can scrape themBatch jobs push, Prometheus pulls
GrafanaDashboards, exploration, and optionally its own alertingPrometheus API, Alertmanager API

The scrape loop

For every target, Prometheus runs an independent loop:

  1. Wait for scrape_interval (default 1m, set to 15s here). Prometheus staggers targets across the interval so they aren't all scraped at the same moment.
  2. GET the target's metrics_path (default /metrics), with a scrape_timeout (10s here).
  3. Parse the response: each line is one sample.
  4. Apply metric_relabel_configs, which can drop or rename series.
  5. Attach the target's labels (job, instance, and any you added).
  6. Append every sample to the TSDB with the scrape's timestamp.
  7. Record synthetic metrics about the scrape itself: up, scrape_duration_seconds, scrape_samples_scraped, and others.

If the GET fails or times out, step 7 still happens, with up set to 0. Any series the target exposed last time are marked stale, so queries stop returning them within one interval instead of showing a frozen line.

Rule evaluation

Separately, every evaluation_interval (15s here), Prometheus runs every rule in rule_files:

  • Recording rules run a PromQL expression and store the result as a new series. For example, precomputing the p99 latency so dashboards don't recompute it from raw buckets.
  • Alerting rules run an expression. Every series it returns is an alert. The alert goes pending, then firing once it has been true for the for duration. Firing alerts are sent to Alertmanager.

Both are covered in Rules, Alerting & SLOs.

Where state lives

/prometheus/                # the prometheus_data volume
├── wal/                    # write-ahead log of recent samples (crash safety)
├── chunks_head/            # memory-mapped chunks of the in-memory head block
├── 01J9.../                # a persisted 2-hour (or compacted, larger) block
│   ├── chunks/             # compressed samples
│   ├── index               # label -> series lookup
│   ├── meta.json           # time range, stats
│   └── tombstones          # deletions not yet compacted away
└── queries.active          # in-flight queries, for crash diagnostics

The most recent ~2 hours live in memory (the head block), backed by the WAL so a crash loses nothing. Older data lives in immutable on-disk blocks that are compacted into larger blocks over time. The full story is in Head, WAL & Blocks.

Prometheus's own API

Everything in the UI is built on an HTTP API you can call directly, which is useful for scripts and debugging:

# instant query
curl -s 'localhost:9090/api/v1/query?query=up' | jq

# range query: 5 minutes of data at 15s resolution
curl -s 'localhost:9090/api/v1/query_range' \
--data-urlencode 'query=rate(prometheus_http_requests_total[1m])' \
--data-urlencode "start=$(date -v-5M +%s)" \
--data-urlencode "end=$(date +%s)" \
--data-urlencode 'step=15s' | jq '.data.result | length'

# what is being scraped, and is it healthy?
curl -s localhost:9090/api/v1/targets | jq '.data.activeTargets[] | {job: .labels.job, health}'

# which series cost the most?
curl -s localhost:9090/api/v1/status/tsdb | jq '.data.seriesCountByMetricName[:5]'

Grafana uses the same query and query_range endpoints. When a panel looks wrong, copy its query into the Prometheus UI (or curl) to see whether the problem is the data or the panel.

Next: Your First Scrape.

On this page