Prometheus: Counters and Gauges
A counter is a metric that only ever goes up. A gauge is a metric that can move up or down.
A counter in the metrics output
ShopSentry counts every request by route and status code.
http_requests_total{method="GET",route="/",status="200"} 79A gauge in the metrics output
The same page reports how many requests are being handled right now.
shopsentry_in_flight 1Two readings of one counter
One reading says little. Two readings show how fast requests arrive.
http_requests_total{route="/"} 73
http_requests_total{route="/"} 79Example
curl -s http://localhost:18102/metrics | grep -E "http_requests_total|shopsentry_in_flight"Key points
- A counter counts events that have happened, like requests served or errors seen.
- A counter never goes down while the process runs, except when it restarts.
- A gauge measures a current value, like memory used or requests in flight.
- A gauge can rise and fall freely as the thing it measures changes.
- Graphing a raw counter hides the real story, because its line only climbs.
In this example, the metrics page shows real counters and a real gauge from the running shop. The counters climb as traffic arrives, while the gauge sits at 1 only while a request is in flight. The numbers prove the two types behave differently, so a graph must treat them differently too.
Note
A counter reset looks like a drop to zero, so always compare counter values over time instead of reading one value.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
Which metric type fits the number of orders placed so far?