What Prometheus and Grafana are and how they work
Prometheus is a monitoring system with its own time series database: the server polls targets over HTTP, stores samples and runs queries over them. Grafana provides dashboards and alerts on top of data sources, and for this pair the main data source is Prometheus.
Prometheus collects numeric metrics, such as web server response time, the number of active database connections or memory in use. Each metric is stored with a timestamp and labels, which are name–value pairs used to filter it: job="api", instance="web1". The model is pull: Prometheus fetches values from targets itself, and the application is unaware of it.
How the pair works together
Each target serves its metrics as text over HTTP at /metrics, either from the application itself (with a client library) or from a separate exporter. Prometheus polls targets at a fixed interval (once a minute by default), stores the samples, each a value with a timestamp, in its TSDB and evaluates rules: some compute new series, others fire alerts. Alerts go to Alertmanager, which sends them on to receivers.
Components
| Component | What it does |
|---|---|
| Prometheus server | polls targets, stores series in the TSDB, evaluates rules and answers PromQL queries |
| Exporters | expose metrics for things that have no /metrics of their own: the Linux kernel, PostgreSQL, nginx |
| Client libraries | counters and timers in application code, serving /metrics |
| Pushgateway | accepts metrics from short-lived jobs so that Prometheus can scrape them later |
| Alertmanager | deduplicates, groups and routes alerts to receivers |
| Grafana | dashboards and alerts on top of Prometheus and other data sources |
Most components are written in Go and shipped as static binaries: unpack and run. The exception is client libraries, which are packages for the application’s language. Many components are optional; the minimum is a single Prometheus server.
A minimal set of targets
The Prometheus server needs targets. List them in the prometheus.yml config statically or find them through service discovery. A typical pair is prometheus (itself) and a host metrics exporter on each server:
global: scrape_interval: 15s scrape_configs: - job_name: prometheus static_configs: - targets: ["localhost:9090"] - job_name: node static_configs: - targets: ["web1:9100", "web2:9100"]What each exporter provides and how to add your own is covered in the section “Collecting metrics and exporters”; the full query language is in the section “The PromQL query language”.
What Alertmanager does
Alertmanager is a separate service, and Prometheus sends it alerts over HTTP. It deduplicates repeats, groups similar alerts into a single email or message and routes them to receivers according to rules: email, Telegram, PagerDuty. It is also where you configure silences for maintenance windows.
route: receiver: mail receivers: - name: mail email_configs: - to: admin@example.com - name: tg telegram_configs: - bot_token: change-me chat_id: 123456789Why Grafana
Prometheus has graphs, but dashboards are built in Grafana. It connects to Prometheus as a data source, requests metrics through PromQL and draws graphs, tables and charts. One dashboard can combine panels from several data sources, and template variables substitute host names into queries, so the same dashboard works for staging and production. Grafana can also send alerts itself, but pick one place for them, usually Alertmanager.
When Prometheus fits and when it does not
Prometheus is designed for numeric time series: metrics of machines, services and applications, including dynamic microservice environments. Each server is autonomous and does not depend on network storage, so you can rely on it when everything else has already broken.
- Fits: service load and errors, request latency, disk and queue capacity.
- Does not fit: billing and anything that needs every single event to be exact, because time-based sampling may miss individual values.
- Does not replace: logs, which belong to Loki and similar systems (see the section “Storage: TSDB and remote write”), or traces, which belong to distributed tracing.
What to know beforehand
- HTTP and response codes: metrics are scraped over HTTP; see the HTTP and REST reference, sections on methods and status codes.
- The Linux command line: processes, services, firewall; see the Linux reference.
- YAML syntax: the Prometheus and Alertmanager configs are written in it.
Section sources
the section is written from these pages- Overviewprometheus.io/docs/introduction/overview/(opens in a new tab)
- About Grafanagrafana.com/docs/grafana/v13.3/introduction/(opens in a new tab)
- Alertmanagerprometheus.io/docs/alerting/0.34/alertmanager/(opens in a new tab)
- Configurationprometheus.io/docs/prometheus/latest/configuration/configuration/(opens in a new tab)
- Alertmanager configurationprometheus.io/docs/alerting/latest/configuration/(opens in a new tab)
- node_exportergithub.com/prometheus/node_exporter(opens in a new tab)