Metric Types
Counters, gauges, histograms, summaries, and native histograms
Storage-wise, every series is just floats over time. The type is a contract between the code that produces a metric and the person who queries it: it tells you which PromQL functions make sense. Pick the wrong type and the numbers will look plausible and be wrong.
Counter
A number that only goes up, or resets to zero when the process restarts. Use it for anything you'd describe as "how many so far": requests served, bytes sent, errors, jobs processed.
REQUESTS = Counter("http_requests", "HTTP requests handled.", ["method", "route", "status"])
REQUESTS.labels("GET", "/api/orders", "200").inc()- Name it with a
_totalsuffix (the Python client adds it for you). - The raw value is almost meaningless. It depends on when the process
started. Always wrap it in
rate()orincrease(), which turn "total so far" into "per second" and handle resets.
http_requests_total # 18233 (since some restart) — not useful
rate(http_requests_total[5m]) # 12.4 req/s — usefulGauge
A number that goes up and down: current memory, queue depth, open connections, temperature, requests in flight. Read it directly.
IN_FLIGHT = Gauge("http_requests_in_flight", "Requests currently being served.")
IN_FLIGHT.inc() # on request start
IN_FLIGHT.dec() # on request endhttp_requests_in_flight # value right now
max_over_time(http_requests_in_flight[10m]) # worst moment in the last 10 minutes
avg_over_time(http_requests_in_flight[10m]) # averageNever rate() a gauge, and never read a counter raw. A common bug is
exposing "requests in the last minute" as a gauge computed inside the
app. It loses data between scrapes, can't be aggregated correctly
across instances, and breaks on restarts. Expose a counter and let
PromQL compute the rate.
A useful gauge pattern is storing a timestamp:
# seconds since the nightly backup last succeeded
time() - nightly_backup_last_success_timestamp_secondsAlerting on "more than 26 hours since last success" catches both a job that fails and a job that never ran, which an "alert on failure" rule can't.
Histogram
For anything with a distribution: request latency, response size.
A histogram counts observations into buckets, each one cumulative,
labelled le ("less than or equal").
LATENCY = Histogram("http_request_duration_seconds", "HTTP request latency.", ["method", "route"],
buckets=(0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5))
LATENCY.labels("GET", "/api/orders").observe(0.023)One observation of 23 ms increments every bucket whose boundary is at least 0.023:
..._bucket{le="0.005"} 0 # 23ms is not <= 5ms
..._bucket{le="0.01"} 0
..._bucket{le="0.025"} 1 # 23ms <= 25ms
..._bucket{le="0.05"} 1 # cumulative: also <= 50ms
...
..._bucket{le="+Inf"} 1 # always equals _count
..._count 1 # number of observations
..._sum 0.023 # sum of observed valuesEvery one of those series is a counter. So you rate() them, and from
the rates you can compute:
- Throughput:
rate(..._count[5m]) - Average:
rate(..._sum[5m]) / rate(..._count[5m]) - Percentiles:
histogram_quantile(0.99, ...), estimated by interpolating inside the bucket where the 99th percentile falls - Share under a threshold:
rate(..._bucket{le="0.1"}[5m]) / rate(..._count[5m]). This is exact, not an estimate, if 0.1 is a bucket boundary.
The big advantage: histograms from many instances can be summed and a percentile computed over the whole fleet. Details in Histograms & Percentiles.
Choose bucket boundaries around your SLO. If the target is "99% of requests under 300ms", make sure 0.3 is a boundary, so "share under 300ms" is exact. Percentile estimates are only as precise as the buckets around them: with buckets at 0.25 and 0.5, a reported p99 of 0.4 really means "somewhere between 250 and 500 ms."
Summary
A summary also tracks a distribution, but computes quantiles inside the application and exposes them directly:
rpc_duration_seconds{quantile="0.5"} 0.012
rpc_duration_seconds{quantile="0.99"} 0.087
rpc_duration_seconds_count 9912
rpc_duration_seconds_sum 131.7That looks convenient, but there's a fatal catch: you cannot aggregate quantiles. The average of ten instances' p99s is not the fleet's p99. It's not anything meaningful. Summaries are also computed over a fixed window chosen in the code, not at query time.
| Histogram | Summary | |
|---|---|---|
| Quantile computed | At query time, in PromQL | In the app |
| Aggregate across instances | Yes | No (quantiles); yes (_sum, _count) |
| Choose quantile later | Yes, any | No, fixed in code |
| Accuracy | Depends on buckets | Exact for its window |
| Cost | One series per bucket | One series per quantile |
Default to histograms. The demo app's order_value_dollars is a
summary to show the contrast. The Python client's summaries don't even
expose quantiles, only _count and _sum, so all you can compute is an
average.
Native histograms
Classic histograms have two weaknesses: you must choose buckets up front, and each bucket is a separate series. Native histograms (stable in Prometheus 3) store the whole distribution as a single series with automatically chosen, exponentially spaced buckets. They're higher resolution and cheaper.
# with a native histogram, there is no _bucket and no le label:
histogram_quantile(0.99, sum(rate(http_request_duration_seconds[5m])))
histogram_count(rate(http_request_duration_seconds[5m]))
histogram_fraction(0, 0.3, rate(http_request_duration_seconds[5m])) # share under 300msThey require the protobuf scrape format and client library support (Go and Java have it; check your language). In a new system, they're worth using. In this module we use classic histograms because they're still what you'll meet in most existing exporters and dashboards.
Info metrics
Not a real type, but a convention: a gauge with value 1 whose labels
carry metadata.
app_build_info{version="1.4.0", language="python"} 1
node_uname_info{release="6.10.0", machine="aarch64", ...} 1Because the value is always 1, you can multiply it onto other series to attach the metadata. That's how you answer "which version was running when errors spiked?" (see Aggregation & Joins).
Naming conventions
| Rule | Example |
|---|---|
| Prefix with the subsystem | http_, pg_, node_, kafka_ |
| Base units, plural | _seconds not _ms; _bytes not _kb |
Counters end in _total | http_requests_total |
Ratios are 0-1, end in _ratio | cache_hit_ratio |
| Timestamps are Unix seconds | ..._timestamp_seconds |
Metadata metrics end in _info | app_build_info |
Base units matter more than they look. If one service exports milliseconds and another seconds, every cross-service dashboard needs a fudge factor, and someone will eventually get it wrong.