Counters, gauges, histograms
We turn repeated observations into bounded series. A dashboard can lie even when every sample is correct. We will build counters, gauges, distributions, and label budgets.
#Section 9Counters and rates
The request counter increases until the process restarts and its value returns to zero. A counter records a cumulative nonnegative count within an instance lifetime. Its absolute value is rarely the chart we need; changes over time reveal the event rate.
Read two valid observations, subtract the earlier from the later, and divide by elapsed time, while treating a restart as a reset. At 1,000 requests per second, a 60-second interval contains 60,000 attempts. If 600 fail, the error rate is 600 divided by 60,000, or 0.01. Read that expression aloud: failed eligible attempts divided by all eligible attempts over the same interval. Summing counters from instances before handling their resets can hide a reset inside another instance’s increase.
The failure numerator must come from the same request population as the attempt denominator. If failures include authentication rejections but attempts count only admitted handlers, the ratio describes no coherent set of events. Process a restarting instance’s counter reset before combining its rate with the other instances. Otherwise another instance’s increase can conceal the reset and produce an apparently ordinary fleet change. Keep the observed interval and scrape validity alongside the calculation. The illustrated 60,000 attempts and 600 failures establish the interval fraction; they cannot establish the outcome of requests that never entered this measurement boundary.
A missing scrape is not a zero counter. Query behavior must distinguish gaps, resets, and genuinely no traffic.
#Section 10Gauges and state
The API has a connection pool with 40 slots, of which 32 are busy. A gauge measures a present quantity that can increase or decrease. Here occupancy is 32 divided by 40, or 0.8, but occupancy alone does not say whether callers are waiting.
Record busy slots, maximum slots, waiter count, and acquisition delay together. A pool can remain fully occupied while serving quickly, or nearly empty because the database is unreachable. Gauges should describe the state after the operation they observe, with cleanup that runs on exceptions. If an in-flight gauge increments at entry but fails to decrement on an error path, it reports a leak in the instrument rather than a leak in the application. Compare state with completion rates to detect that inconsistency.
Follow a request that needs a database slot. It first joins acquisition waiting, then owns a slot while the operation runs, and finally releases it even if the query fails. Instrument those state transitions so waiting does not count as occupied capacity and cancellation removes the waiter. The illustrated 32 busy slots out of 40 can accompany prompt acquisition or severe waiting if occupancy changed between samples. Compare wait durations and completion rates rather than choosing a larger pool from that snapshot alone. If the instrument retains busy slots after all operations finish, inspect cleanup before diagnosing a database connection leak.
Summing memory gauges across machines measures total allocation, while averaging them measures a different quantity. Specify the question before choosing the aggregation.
#Section 11Histograms and latency
A fast average hides the viewer who waits longest. A histogram groups observations into intervals and stores counts for those intervals. Unlike a mean, it retains enough distribution information to ask how much traffic falls below a threshold.
Heron’s illustrative latency sample contains 50 observations at 5 milliseconds, 45 at 40, four at 100, and one at 200. Cumulative counts are 50, 95, 99, and 100. For a nearest-rank percentile, choose the observation at the ceiling of the requested fraction times the count. The 99th observation is 100 milliseconds. A bucket covering a wide range cannot reveal the exact percentile inside that range; interpolation is an estimate from bucket data and must be described as such. Exact sample percentiles and bucket estimates are different calculations.
Read the cumulative rows as nested populations. The count at 40 milliseconds includes the observations already counted at five, so subtracting 50 from 95 gives the illustrated 45 observations in the next interval. Summing cumulative counts would count fast requests repeatedly. To combine instances, first add counts for matching boundaries and populations, then find the rank in that merged distribution. If bucket layouts differ, reconcile the layouts rather than treating their positions as interchangeable. The exact sample values in this example allow an exact nearest-rank answer; ordinary bucket counts only bound where the desired observation lies.
Averages and percentile summaries generally cannot reconstruct the original distribution. Prometheus explains histogram and summary aggregation in its histogram guidance.
#Section 12Cardinality and label budgets
A developer adds user_id to every latency measurement. The dashboard now has a distinct time series for each user and each existing label combination. Cardinality is the number of distinct series created by those combinations.
With 12 operations, six regions, and five result values, the possible combinations total 12 times six times five, or 360. An illustrative classic histogram with 12 buckets plus count and sum has 14 series per combination, making 5,040 series. Adding 50,000 users multiplies the base combinations to 18,000,000 before the histogram expansion. Put request and user identifiers in logs or traces, where querying individual records is the purpose. Metrics should use bounded dimensions that answer aggregate questions. Even bounded labels need a multiplication budget when combined.
A review should write down possible values, not merely count the label names. Operation, region, and result combine to form distinct identities even when every value is individually useful. Histogram storage then emits several related series for each identity, so a request identifier added later affects every bucket as well as count and sum. Remove individual identities from aggregate instrumentation before they reach the exporter. When one user needs investigation, locate its request in a log or trace and compare it with bounded population metrics. Dropping the offending label only in a dashboard leaves the ingestion and retention cost intact.
A label that currently has few values can grow without bound if its source is free text, URLs with identifiers, or exception messages. Normalize routes and outcomes.