Sub topic 3
Metrics, labels, and time series
This chapter explains how Prometheus represents data. Every query, alert, and dashboard in later chapters builds on this model. It assumes you can read YAML, use curl, and know what a TCP port and an HTTP endpoint are. It also assumes you have seen the pull architecture from the previous chapter, where Prometheus fetches data from targets on a schedule.
The core idea: everything is a time series
A time series is a sequence of values for one thing, recorded over time. Prometheus stores all of its data this way. Each value is a sample, made of two parts:
- A timestamp with millisecond precision.
- A 64-bit floating-point value.
Prometheus has no separate storage for strings, booleans, or events. A boolean such as "is this target up?" becomes 1 or 0. A text fact such as a kernel version becomes a series with the value 1 and the text stored in a label.
Anatomy of a series
A series is identified by a metric name and a set of labels:
metric_name{label1="value1", label2="value2"}- The metric name says what is measured, for example
node_cpu_seconds_total. By convention it contains a prefix for the source (node_), the thing measured, and a unit (_seconds,_bytes). Counters end in_total. - Labels are key-value pairs that split one metric into dimensions, such as which CPU core and which mode.
- The sample is the timestamp and value recorded at each scrape.
The name is stored internally as a special label called __name__. The selector node_cpu_seconds_total is shorthand for {__name__="node_cpu_seconds_total"}.
Two series are the same series only if the name and the complete label set match exactly. Adding, removing, or changing one label creates a different series.
What a target exposes
A target answers an HTTP GET on /metrics with plain text. Each line holds one sample, and comment lines carry metadata. You can look at this directly:
curl -s http://localhost:9100/metrics | grep -E '^(# (HELP|TYPE) )?node_filesystem_avail_bytes'Typical output on a server with two real filesystems:
# HELP node_filesystem_avail_bytes Filesystem space available to non-root users in bytes.
# TYPE node_filesystem_avail_bytes gauge
node_filesystem_avail_bytes{device="/dev/sda1",fstype="ext4",mountpoint="/"} 3.1457280e+10
node_filesystem_avail_bytes{device="/dev/sdb1",fstype="xfs",mountpoint="/data"} 4.8318382e+11The two lines are two series of one metric. They share a name and differ in their labels. # HELP is a description and # TYPE declares the metric type, which the next chapter covers. The target does not send timestamps. Prometheus stamps each sample with the time it scraped it.
Labels from the target and labels from Prometheus
Labels on the page above come from the exporter. Prometheus also attaches target labels to every sample it ingests:
jobis thejob_namefrom the scrape configuration, for examplenode.instanceis thehost:portof the target, for exampleserver1.example.com:9100.
After ingestion, one filesystem series looks like this:
node_filesystem_avail_bytes{device="/dev/sda1", fstype="ext4", instance="server1.example.com:9100", job="node", mountpoint="/"}This is how a single query can cover many servers. The same metric name exists once per target, and the instance label tells the copies apart.
If an exporter itself emits a label named instance or job, it collides with the target label. By default Prometheus keeps its own value and renames the exporter's label to exported_instance or exported_job. The scrape setting honor_labels: true reverses that behaviour. It is used mainly with the Pushgateway and federation, and is rarely needed for host monitoring.
One metric, many series
The following diagram shows how one metric name expands into many series as labels are added, and how each series holds its own list of samples.
On a 4-core server, node_cpu_seconds_total has one series for each core and mode pair. With 8 modes that is 32 series from one metric on one server. You can check the count with a query:
count by (instance) (node_cpu_seconds_total)Cardinality: the cost of labels
Cardinality is the number of distinct series. Each series costs memory for its index entry and its recent samples, and disk for its stored data. The total is roughly the product of the distinct values of each label. A metric with labels cpu (4 values) and mode (8 values) has up to 32 series. Adding a label with 1,000 values would turn those 32 into 32,000.
Follow these rules when you choose labels, whether for your own metrics or when deciding what to keep from an exporter:
- Use labels for dimensions with a small, bounded set of values: mountpoint, HTTP method, status class, environment.
- Never put unbounded values in labels: user IDs, email addresses, full URLs with query strings, request IDs, or timestamps.
- Do not encode a dimension in the metric name (
requests_get_total,requests_post_total). Use one metric with amethodlabel so queries can aggregate across it.
Series lifetime and staleness
A series exists as long as targets keep exposing it. When a target stops exposing a series, or a scrape fails, Prometheus writes a special stale marker. Queries then stop returning that series immediately. Without staleness handling, a series would appear to hold its last value for the default lookback window of five minutes.
Label changes also end a series. If a server is renamed, or a filesystem's device label changes after a reboot, the old series goes stale and a new series begins. Graphs of such a metric show a break instead of a continuous line. This matters when you write alerts that depend on a stable label set.
Naming and label conventions to follow
- Use
snake_casefor metric and label names. - Include the base unit in the name and use base units: seconds, bytes, and ratios from 0 to 1, rather than milliseconds, megabytes, or percentages.
- Names starting with
__are reserved for Prometheus internals. - Label values can hold any string, but label names must match
[a-zA-Z_][a-zA-Z0-9_]*.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Treating `instance` as a hostname | It includes the port, so `server1:9100` differs from `server1:9101` | Relabel or match with a regular expression when you need the host alone |
| Assuming a missing series means value 0 | A failed scrape produces no series, and the query returns nothing | Alert on `up == 0` as well as on the metric |
| Storing a number in a label | You cannot do arithmetic on labels | Expose the number as a sample value |
| Comparing series with different label sets | Binary operations drop series with no matching partner | Match on the shared labels, covered in the PromQL chapters |
Exercise
A custom exporter exposes http_requests_total{path="/users/48213", method="GET"}, with a different path for every user. Why is this a problem, and how would you change the labels?
Show answer
The path label has unbounded values, one per user, so it creates a new series for every user ID. This inflates cardinality and memory use. Replace the concrete path with a route template such as path="/users/:id", which has a small, fixed set of values. Keep method as a label, because it has only a few values.
Exercise
The query node_filesystem_avail_bytes{mountpoint="/"} returns two series, with instance values server1.example.com:9100 and server2.example.com:9100. Name the two label sets that distinguish these series from the exporter's output, and say where each label comes from.
Show answer
instance and job are target labels that Prometheus attaches at scrape time. instance comes from the target address and job from the job_name in prometheus.yml. The other labels, such as device, fstype, and mountpoint, come from node_exporter itself.