Sub topic 1
Why monitor with metrics
This chapter explains what metrics are, why they suit server monitoring, and how they differ from logs and traces. It also sets up the vocabulary the rest of the tutorial uses. You will not install anything yet.
Prerequisites
This chapter assumes you can:
- Work comfortably on the Linux command line and edit configuration files.
- Start, stop, and inspect services with systemd.
- Read basic YAML.
- Describe TCP ports and HTTP requests at a basic level.
- Name the main server resources: CPU, memory, disk, and network.
No Prometheus or Grafana experience is needed.
What monitoring is for
Monitoring is the continuous collection of data about a system so you can answer three questions:
- Is it working right now?
- Is it getting worse over time?
- What changed when it broke?
Without monitoring, you learn about a full disk or an overloaded database from a user complaint. With it, you can see the disk filling over several days and act before anything fails.
Good monitoring serves four uses:
- Alerting: a person is notified when something needs attention.
- Diagnosis: during an incident, you look at history to find the cause.
- Capacity planning: you use trends to decide when to add disk, memory, or servers.
- Verification: after a change, you check that the system behaves as expected.
Three kinds of telemetry
Telemetry is data a system emits about itself. Three kinds are in common use.
| Kind | What it records | Typical question it answers | Cost to store |
|---|---|---|---|
| Metrics | Numbers sampled over time | How busy is the CPU? Is the error rate rising? | Low and predictable |
| Logs | Text records of individual events | What exactly happened to this request? | High and grows with traffic |
| Traces | The path of one request across services | Which service made this request slow? | Medium to high |
A metric is a numeric measurement that is recorded repeatedly. A single measurement is called a sample. It consists of a value and a timestamp. A sequence of samples for one measurement is a time series.
Metrics are compact. A server that handles a million requests per hour still produces one sample per metric per scrape, not one record per request. This is why metrics scale well and why they are the usual starting point for server monitoring.
Metrics also have a limit. They aggregate, so they tell you that errors increased, but not which request failed. Logs and traces fill that gap. This tutorial covers metrics only, and you should expect to use logs alongside them in practice.
What a sample looks like
Prometheus exposes and stores metrics in a text format. This is a small example of what a server might publish:
# HELP node_load1 1m load average.
# TYPE node_load1 gauge
node_load1 0.42The line starting with # HELP describes the metric. The line starting with # TYPE states its type. The last line holds the metric name and its current value. Chapter 3 covers names and labels in detail, and chapter 4 covers the types.
Prometheus reads this text every few seconds and stores each value with the time it was read. Over an hour, one metric collected every 15 seconds produces 240 samples.
Push and pull collection
A monitoring system needs a way to get data from servers. There are two models.
- Push: each server sends its data to a central collector.
- Pull: the central collector asks each server for its data at a fixed interval.
Prometheus uses pull. The act of pulling is called a scrape. Each server, or a small helper program running on it, exposes an HTTP endpoint, and Prometheus fetches it.
Pull has practical effects on how you operate the system:
- A failed scrape is itself a signal. If Prometheus cannot reach a target, it records that fact, so a dead server shows up without extra work.
- You can check a target by hand. Running
curlagainst the endpoint shows exactly what Prometheus would see. - The central server controls the load. It decides how often to scrape, so a busy fleet cannot flood it with unsolicited data.
- The collector must be able to reach every target. Firewalls and network layout matter, which is a cost of this model.
Pull is a poor fit for very short-lived jobs, such as a script that finishes before the next scrape. Prometheus has a separate component for that case, which a later chapter on architecture mentions.
The four-part stack
The tutorial builds one stack from four parts, each with a single job:
- node_exporter exposes Linux host metrics over HTTP.
- Prometheus scrapes and stores the metrics, and evaluates rules against them.
- Alertmanager receives alerts from Prometheus and decides who is told, and when.
- Grafana draws dashboards by querying Prometheus.
The path of data looks like this:
Grafana does not store metrics. It asks Prometheus each time a panel is drawn. Alertmanager does not decide when something is wrong. Prometheus decides that with alerting rules, and Alertmanager only handles delivery. Keeping these roles separate makes failures easier to locate.
What to measure first
Beginners often try to collect everything and then cannot tell which graphs matter. A better start is a small set of questions, grouped by resource. Two common frameworks help.
The USE method applies to resources such as CPU, memory, disk, and network. For each one, check:
- Utilisation: how much of the resource is in use.
- Saturation: how much work is waiting because the resource is full.
- Errors: how many error events occurred.
The RED method applies to services that handle requests:
- Rate: requests per second.
- Errors: failed requests per second.
- Duration: how long requests take.
This tutorial focuses on host resources, so the USE method is the main guide. For a Linux server, it leads to a short starting list:
| Resource | Utilisation | Saturation | Errors |
|---|---|---|---|
| CPU | Percent of time not idle | Load average above the core count | Rare on most hardware |
| Memory | Percent in use | Swapping activity | Out-of-memory kills |
| Disk | Percent of space used | I/O wait and queue length | Disk read and write errors |
| Network | Bandwidth versus link speed | Dropped packets | Interface errors |
Alert on symptoms, not every cause
Metrics make it easy to create alerts, and too many alerts cause real harm. If a pager fires often for problems that need no action, people stop reacting to it. This is called alert fatigue.
Follow these rules when you decide what to alert on:
- Alert on conditions that need a human to act now, such as a down server or a disk about to fill.
- Prefer symptoms users would notice over internal causes. High CPU for one minute is rarely worth a page. A service returning errors is.
- Require a condition to hold for some time before alerting. A brief spike should not wake anyone.
- Send lower-priority conditions to a ticket or a dashboard instead of a page.
Later chapters show how Prometheus and Alertmanager express these rules.
Common mistakes
These errors appear often when teams adopt metrics monitoring:
- Collecting without a question. Data that no dashboard or alert uses still costs storage and attention.
- Averaging away the problem. An average across ten servers can look healthy while one server is failing. Chapters on PromQL show how to keep per-server views.
- Trusting a graph without checking the source. An empty panel can mean a healthy system with no data, or a broken scrape. Always check that targets are being scraped.
- Monitoring only from the same machine. If the monitoring server dies with the thing it watches, no one is told. Plan for at least one independent check.
- Treating metrics as a replacement for logs. Metrics show that something is wrong. Logs usually show why.
What you will build
Over the remaining chapters you will:
- Run node_exporter on Linux servers as a systemd service.
- Configure Prometheus to scrape those servers and check target health.
- Write PromQL queries for CPU, memory, and disk.
- Connect Grafana and build a dashboard with a server selector.
- Add recording rules and alerting rules, and route alerts through Alertmanager.
- Plan retention, security, and scaling for a small production setup.
Check your understanding
Exercise
A web server's disk fills slowly over three weeks and then the service fails. Explain why metrics help here more than logs, and say what alert you would add.
Show answer
Metrics record disk usage as a number at regular intervals, so a graph shows the steady upward trend weeks before the failure. Logs record events, and a slowly growing disk produces no single event that stands out. An alert on free disk space falling below a threshold, such as 10%, that holds for several minutes would give a warning well before the disk is full.
Exercise
Prometheus uses a pull model. Name two operational benefits and one cost of this choice.
Show answer
Benefits: a failed scrape records that a target is unreachable, so a dead server is visible without extra work; and you can run curl against a target's endpoint to see exactly what Prometheus sees. The Prometheus server also controls the scrape rate, so it is not flooded with data. Cost: Prometheus must be able to reach every target over the network, so firewalls and network layout must allow it.
Exercise
Which component decides that an alert condition is true, and which component decides who is notified?
Show answer
Prometheus decides that the condition is true by evaluating alerting rules. Alertmanager decides who is notified, grouping and routing the alerts it receives from Prometheus.