Skip to content
$ lynuxpro
← Monitoring with Prometheus, Grafana & Datadog

LESSON 2 OF 18 · BEGINNER · 13 MIN

Metrics, types and labels

Free previewLesson 2 of the 2 free lessons in this Pro course.Unlock all 18

HOW IT WORKSA metric and its labels

Metric namehttp_requests_totalcode="200"series 1code="500"series 2user_id="48213"too many!Value + time10482 @ 14:00
1/5

A metric has a name and a number that is measured again and again.

In simple words

  • Metric: a named number measured over time, such as http_requests_total.
  • Label: a key=value tag on a metric, such as status="500", so you can slice the data.
  • Counter / gauge: a counter only goes up (total requests). A gauge goes up and down (memory in use).
  • Histogram: counts values in buckets, so you can work out percentiles like p99 latency.
  • Cardinality: how many different label combinations exist. Too many makes monitoring slow and expensive.

Big picture

A metric is a number with a name that you measure again and again. Prometheus stores each reading with a timestamp, so you can draw graphs and spot changes.

Think of a thermometer reading written down every 15 seconds. The list of readings is a time series.

Anatomy of a metric

http_requests_total{method="GET", code="200"} 10482
OUTPUTterminalutf-8 · 1L
PartMeaning
http_requests_totalthe metric name
{method="GET", code="200"}labels: extra details that split the metric into separate series
10482the value right now

Every unique set of labels is its own time series. code="200" and code="500" are two different series.

The four metric types

TypeBehaviourAnalogyExample
CounterOnly goes up (resets on restart)Car odometerhttp_requests_total
GaugeGoes up and downFuel gaugememory_used_bytes, queue_length
HistogramCounts values into bucketsA tally of "how many were under 100 ms, 300 ms, 1 s"request_duration_seconds
SummaryLike a histogram, but works out percentiles inside the appA pre-made reportrarely needed

Rule of thumb: you almost never use a counter's raw value. You use its rate (how fast it grows), which you learn in the PromQL lesson.

Naming rules

  • Use lowercase words with underscores: http_requests_total.
  • Put the unit in the name: _seconds, _bytes.
  • Counters end in _total.
  • Do not put values in the name. Use a label instead.

Labels: powerful, but dangerous

Labels let you slice data: "errors by route", "latency by region".

But each new label value creates a new series, which costs memory.

LabelSafe?Why
method="GET"yesa handful of values
route="/orders"yestens of values
user_id="48213"nomillions of values
request_idnoa new value every request

Too many series is called high cardinality, and it is the most common way to break a Prometheus server.

curl -s localhost:9090/api/v1/status/tsdb | head -c 200
SHELLbashutf-8 · 1L
{"status":"success","data":{"headStats":{"numSeries":84213,"numLabelPairs":1530,"chunkCount":84213}}}
OUTPUTterminalutf-8 · 1L

numSeries shows how many series you are storing.

A histogram in detail

A histogram is really three kinds of series. Here is one request-duration histogram on a metrics page:

http_request_duration_seconds_bucket{le="0.1"}  8200http_request_duration_seconds_bucket{le="0.3"}  9750http_request_duration_seconds_bucket{le="1"}    9990http_request_duration_seconds_bucket{le="+Inf"} 10000http_request_duration_seconds_sum   1843.2http_request_duration_seconds_count 10000
OUTPUTterminalutf-8 · 6L
SeriesMeaning
_bucket{le="0.3"}how many requests took less than or equal to 0.3 seconds (le means "less or equal")
_sumthe total time of all requests added together
_counthow many requests there were

So 9,750 of 10,000 requests (97.5%) finished within 0.3 s. The buckets are cumulative: each one includes the ones before it.

Average duration is _sum / _count = 0.18 s. But the average hides the slow ones, which is why percentiles from buckets are better.

Choose buckets near the numbers you care about. If your goal is "under 300 ms", make sure there is a bucket at 0.3.

Reading a metrics page

# HELP node_memory_MemAvailable_bytes Memory available# TYPE node_memory_MemAvailable_bytes gaugenode_memory_MemAvailable_bytes 3.4e+09
OUTPUTterminalutf-8 · 3L
  • # HELP explains the metric.
  • # TYPE says counter, gauge or histogram.
  • Numbers like 3.4e+09 mean 3.4 billion.

A quick look at the same metric types in other tools

IdeaPrometheus wordOther tools may say
Number that only growscountercumulative sum, count
Number that movesgaugegauge, last value
Spread of valueshistogramdistribution

Common mistakes

  • Putting user ids, emails or URLs with ids in labels.
  • Forgetting units in names.
  • Using a gauge for something that only grows.
  • Graphing a counter directly instead of its rate.

Summary

  • A metric is a named number over time, split by labels.
  • Counter goes up, gauge goes up and down, histogram counts into buckets.
  • Keep label values few. High cardinality is the big danger.

CHECK YOUR UNDERSTANDING

3 quick questions

  1. 1. Which metric type only goes up (until a restart)?

  2. 2. Which label is a bad idea?

  3. 3. What should a counter name end with by convention?

You've finished the free preview of Monitoring with Prometheus, Grafana & Datadog

Upgrade to Pro to unlock the remaining 16 lessons, every practice lab and a certificate when you finish.

See plans