Monitoring with Prometheus and Grafana / Metrics and Instrumentation

Series, labels and the exposition format

Lesson 5 of 35. Open to read. No account needed.

In the previous lesson you saw Prometheus pull text from /metrics and turn it into something you could query. This lesson looks closely at both sides: the text format a program exposes, and the data model Prometheus stores. You will learn the four metric types and how to choose between them, because the type you choose decides which questions you can answer later.

You will practise on Prometheus's own /metrics page, which happens to contain one metric of every type, and on the shop, which does not yet expose any metrics of its own. The next lesson adds them.

Time Series, Labels and Samples

Prometheus stores everything as time series. A time series is identified by a metric name and a set of labels, which are name and value pairs. For example:

prometheus_http_requests_total{code="200", handler="/metrics", instance="localhost:9090", job="prometheus"}

The metric name, prometheus_http_requests_total, says what is measured: HTTP requests that Prometheus itself has served. The labels say which particular slice: requests to the /metrics handler that returned status 200, from the target localhost:9090 in the job prometheus. Internally the metric name is just another label called __name__.

The identity rule is strict: the metric name together with every label value identifies one series. Requests that returned 500 instead of 200 are a different series with its own history. So are requests to a different handler. This is what makes labels powerful (you can later add up all codes, or pick out only errors) and what makes them dangerous (every distinct value creates another series to store).

Each series holds samples. A sample is a timestamp, in milliseconds, and a value, a 64-bit floating-point number. The program being monitored never sends timestamps or history; it reports only the current value, and Prometheus records the time of each scrape.

From an exposition line to a time series. Step 1: one line on a metrics page, prometheus_http_requests_total with labels code 200 and handler /metrics and the value 3, is split into the metric name, the labels set by the program, and the value. Step 2: at each scrape Prometheus adds the job and instance labels from its configuration and the time of the scrape; the metric name plus every label is the series identity. Step 3: the series is a list of samples, one per scrape every 15 seconds, each a timestamp and a value, shown as 1, 2, 3, 3. A note says that changing any label value, for example code 500 instead of 200, makes a separate series, and many label values mean many series.

Read the diagram top to bottom. The program writes the first line. Prometheus adds job and instance from its own configuration, which together with the program's labels give the series identity in step 2. Every scrape then adds one sample to that series, as in step 3. The values in the diagram are illustrative.

The Exposition Format

The text on a /metrics page follows Prometheus's text exposition format. It is line-oriented: each line is either a comment beginning with #, or one sample. Here is a short extract from Prometheus's own page (illustrative values):

# HELP prometheus_http_requests_total Counter of HTTP requests.
# TYPE prometheus_http_requests_total counter
prometheus_http_requests_total{code="200",handler="/-/ready"} 1
prometheus_http_requests_total{code="200",handler="/metrics"} 3
# HELP prometheus_tsdb_head_series Total number of series in the head block.
# TYPE prometheus_tsdb_head_series gauge
prometheus_tsdb_head_series 731

The lines mean:

  • # HELP <name> <text> gives a human description. It is not stored as a series, but Prometheus's interface shows it.
  • # TYPE <name> <type> declares the metric's type for the whole metric family, the group of series that share a name.
  • A sample line is the metric name, optional labels in braces, a space and the value. Label values are always quoted strings; the value is a number. A sample line may also end with a timestamp, but programs normally leave it out and let Prometheus use the scrape time.

A metric with no labels, such as prometheus_tsdb_head_series, is still a series; it simply has only job and instance once Prometheus stores it.

The Four Metric Types

Counter

A counter is a number that only goes up, or resets to zero when the program restarts. It counts things that happen: requests served, errors, bytes sent. By convention its name ends in _total.

The value of a counter on its own is rarely interesting: "3 requests since some unknown start time". What you query is how fast it grows, which the PromQL module computes with rate(). Because restarts reset counters to zero, never use a counter for something that can legitimately decrease; and never treat a drop in a counter as a real decrease.

Gauge

A gauge is a number that can go up and down: a measurement at a moment, such as memory in use, items in a queue, or prometheus_tsdb_head_series, the number of series Prometheus currently holds in memory. Its current value is meaningful by itself.

Histogram

A histogram records the distribution of many observations, typically request durations, by counting how many fall at or below a set of boundaries called buckets. Prometheus's own request-duration histogram looks like this on its page (one handler shown, illustrative values):

# TYPE prometheus_http_request_duration_seconds histogram
prometheus_http_request_duration_seconds_bucket{handler="/metrics",le="0.1"} 3
prometheus_http_request_duration_seconds_bucket{handler="/metrics",le="0.2"} 3
prometheus_http_request_duration_seconds_bucket{handler="/metrics",le="0.4"} 3
prometheus_http_request_duration_seconds_bucket{handler="/metrics",le="1"} 3
prometheus_http_request_duration_seconds_bucket{handler="/metrics",le="+Inf"} 3
prometheus_http_request_duration_seconds_sum{handler="/metrics"} 0.028126957
prometheus_http_request_duration_seconds_count{handler="/metrics"} 3

(Its real output has more buckets, up to le="120"; the middle ones are omitted here.) One histogram produces several series:

  • _bucket series, one per boundary, with an le label meaning "less than or equal to". Each is cumulative: the le="0.4" bucket counts every observation of 0.4 seconds or less, including those already counted in le="0.1". The le="+Inf" bucket counts everything.
  • _sum, the total of all observed values (here 0.028 seconds across three requests).
  • _count, the number of observations; always equal to the +Inf bucket.

All three are counters underneath: they only grow. That is what lets you later compute a distribution over any time window, and add histograms from several instances together.

Summary

A summary also describes a distribution, but the program calculates selected quantiles itself and exposes them directly. Prometheus's garbage-collection pause time is a summary:

# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 4.4334e-05
go_gc_duration_seconds{quantile="0.25"} 0.000157666
go_gc_duration_seconds{quantile="0.5"} 0.000360083
go_gc_duration_seconds{quantile="0.75"} 0.000736333
go_gc_duration_seconds{quantile="1"} 0.002392874
go_gc_duration_seconds_sum 0.010373206
go_gc_duration_seconds_count 18

A quantile is the value below which a given fraction of observations fall: quantile="0.5" is the median, here about 0.36 milliseconds, and quantile="1" is the largest. The e-05 notation means "times 10 to the power of minus 5", so 4.4334e-05 seconds is about 0.044 milliseconds.

Summaries are cheap to read, but their quantiles have two limits. They are fixed when the program is written, so you cannot ask for a different quantile later. And they cannot be combined: the median of two servers' medians is not the median of all their requests. Histograms avoid both problems, which is why this course uses them for latency.

One more practical point: Python's prometheus_client summary exposes only _count and _sum. It does not calculate quantiles at all. If you need percentiles from a Python program, use a histogram.

Choosing a Type

Ask what you will want to know later:

You want to knowMeasurementType
How often something happensRequests, errors, bytes, jobs finishedCounter
How much of something there is nowMemory in use, queue length, temperatureGauge
How values are distributed, including slow percentiles, across many instancesRequest durations, response sizesHistogram
An average onlyDurations where percentiles do not matterSummary (count and sum) or histogram
When something last happenedTimestamp of the last successful backupGauge holding a Unix time

Two common mistakes: keeping a running count of events in a gauge, which hides restarts from the rate calculations you will learn later, and using a counter for something that can go down, where every decrease looks like a restart.

Two Things You Will See but Not Use Yet

_created series. Python's client library adds, by default, a <name>_created series for each counter, histogram and summary, holding the Unix time when that series was created. You will see these in the next lesson. They can be switched off with the environment variable PROMETHEUS_DISABLE_CREATED_SERIES=True; the course leaves them on and ignores them.

Native histograms and UTF-8 names. Prometheus 3 also supports native histograms, which store a whole distribution in one series with automatically chosen buckets. They are a stable feature, but Prometheus only collects them when a scrape setting turns them on, and the Python client used in this course does not produce them, so the course uses classic histograms throughout. Prometheus 3 also accepts metric and label names containing characters such as dots, which must then be quoted in queries, for example {"my.metric"}. This course keeps to the traditional style of letters, digits and underscores.

Exercise: Read a Scrape

Goal. Read two real /metrics pages, name the type and labels of several metric families, and find the same series in Prometheus. This trains you to read the raw source when a query result looks wrong.

Starting state. The practice stack is running (bash tests/check-stack.sh reports seven passes). Commands run from ~/pmg-practice; they only read.

Effects. None. These are GET requests and queries.

Part 1: Prometheus's own page

List the metric families and their types on Prometheus's page:

curl -s http://127.0.0.1:19090/metrics | grep '^# TYPE' | head -20

Each line shows a family name and its type. Prometheus 3.13.5 exposed about 250 families on the author's machine; head -20 shows the first twenty. Now look at one family of each type:

curl -s http://127.0.0.1:19090/metrics | grep -E '^prometheus_http_requests_total\{code="200",handler="/(metrics|-/ready)"\}'
curl -s http://127.0.0.1:19090/metrics | grep -E '^prometheus_tsdb_head_series '
curl -s http://127.0.0.1:19090/metrics | grep -E '^prometheus_http_request_duration_seconds_(bucket|sum|count)\{handler="/metrics"'
curl -s http://127.0.0.1:19090/metrics | grep -E '^go_gc_duration_seconds'

You should recognise a counter, a gauge, a histogram and a summary, matching the extracts above with your own values.

Then open the Query page at http://127.0.0.1:19090/query and execute each of these, one at a time, on the Table tab:

prometheus_tsdb_head_series
go_gc_duration_seconds
prometheus_http_request_duration_seconds_bucket{handler="/metrics"}

Illustrative result for the second query:

go_gc_duration_seconds{instance="localhost:9090", job="prometheus", quantile="0.0"}    0.000044334
go_gc_duration_seconds{instance="localhost:9090", job="prometheus", quantile="0.25"}   0.000157666
go_gc_duration_seconds{instance="localhost:9090", job="prometheus", quantile="0.5"}    0.000360083
go_gc_duration_seconds{instance="localhost:9090", job="prometheus", quantile="0.75"}   0.000918834
go_gc_duration_seconds{instance="localhost:9090", job="prometheus", quantile="1.0"}    0.002392874

Compare it with the page. Two differences are expected and worth noticing:

  • Every stored series has job and instance labels; the page had none.
  • The page said quantile="0" and quantile="1", but Prometheus stored quantile="0.0" and quantile="1.0". Prometheus 3 normalises quantile and le values to one spelling when it ingests them, so you will see le="1.0" in queries where the page shows le="1". Write label values as stored when you filter on them.

The third query returns ten series, one per bucket from le="0.1" to le="+Inf", all for the /metrics handler. If the bucket values are all equal, every request so far was faster than 0.1 seconds.

Part 2: The shop's page

curl -s http://127.0.0.1:19200/metrics | grep '^# TYPE'

Illustrative output:

# TYPE python_gc_objects_collected_total counter
# TYPE python_gc_objects_uncollectable_total counter
# TYPE python_gc_collections_total counter
# TYPE python_info gauge
# TYPE process_virtual_memory_bytes gauge
# TYPE process_resident_memory_bytes gauge
# TYPE process_start_time_seconds gauge
# TYPE process_cpu_seconds_total counter
# TYPE process_open_fds gauge
# TYPE process_max_fds gauge

Find one of them in Prometheus with the Query page:

python_info

It returns one series with labels such as version="3.14.8" and the value 1. This is an info-style gauge: the value is always 1 and the useful information is in the labels.

What differing results mean

  • A family on the page has no series in Prometheus. The scrape is failing (check Status > Target health) or happened before the family existed; wait one interval.
  • You filtered on le="1" or quantile="0" and got nothing. Use the stored spellings le="1.0" and quantile="0.0".
  • Bucket values decrease as le increases. That is impossible for a correct histogram, because buckets are cumulative; you are probably comparing values from different moments or different handlers.

Independent task

Every family on the shop's page comes from somewhere other than the shop's own code. Decide where each group comes from, then predict which new families the next lesson will add when the shop counts its requests and tracks work in progress.

Answer

All of them come from the prometheus_client library's default collectors: python_gc_* and python_info describe the Python interpreter, and process_* describe the operating-system process (CPU time, memory, open files). The shop's code defines none yet, which is why its page says nothing about requests.

The next lesson adds a request counter (a family ending in _total, with labels for route, method and status code), a gauge of requests in progress, and, because the counter comes from the Python client, a matching _created family.

Check Your Understanding

  1. Classify each line by metric type and say what its labels identify:
    • process_cpu_seconds_total 0.58
    • prometheus_tsdb_head_series 731
    • prometheus_http_request_duration_seconds_bucket{handler="/metrics",le="0.4"} 7
    • go_gc_duration_seconds{quantile="0.5"} 0.00036
    • go_gc_duration_seconds_count 19
    • python_info{implementation="CPython",version="3.14.8"} 1.0
    • process_resident_memory_bytes 2.53e+07
    • prometheus_http_requests_total{code="200",handler="/-/ready"} 1
  2. Choose a type for each: a payment service's failed payments; the number of open database connections; how long checkout takes, including the slowest 1%; the time the last nightly import finished; the size in bytes of uploaded files, averaged over an hour.
  3. Why can't you combine two servers' quantile="0.99" values into one overall 99th percentile, and what should you expose instead?

Answers

  1. Counter (CPU seconds used, no labels); gauge (series in memory); histogram bucket (requests to /metrics taking 0.4 s or less, cumulative); summary quantile (median GC pause); summary count (number of GC pauses); info-style gauge (labels carry the Python version, value always 1); gauge (resident memory, 2.53e+07 is 25,300,000 bytes); counter (successful requests to the readiness page).
  2. Failed payments: counter. Open connections: gauge. Checkout duration with the slowest 1%: histogram, because you need a percentile and may run several instances. Last import time: gauge holding a Unix timestamp. Upload size averaged: a summary's _sum and _count are enough (a histogram also works and allows percentiles later).
  3. Quantiles are not additive: the 99th percentile of the combined requests depends on every individual request, which two summary values cannot reconstruct. Expose a histogram; its cumulative bucket counts can be added across servers and a percentile estimated from the total.

30 more lessons in the full course

Course access opens every lesson and adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 199 plus 18% GST. 90 days of online access. No auto-renewal.

Get the full course

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy