Tag Archives: codahale

Gauge vs Counter

Let’s say you need to keep track of certain values on your application, to decide whether is under-performing or achieving any given intended goal, etc… so you think about what you need to measure and decide to insert some KPI’s in your codebase.

gauge

Now think about what you need to measure… is it something that increments in time, or is it something that may increment and decrement? is it something which value depends on previous values, or is it something where we only care about the value on the moment of measurement, which won’t very likely be related to its value maybe a couple of minutes before?

In the first case you will need a counter. In the second, a gauge. Why do I make this distinction?, why is it important?, why are you probably reading this?, basically because any single monitoring software I’ve used make this distinction when creating a metric or defining an alarm. Zenoss, when defining a MIB; Nagios, when specifying the data source type

Ok, yeah, very nice. But very theoretical. Give me some examples so I can understand this better.

There you go then. Examples:

– Number of concurrent visitors in a website: that’d be a gauge, we’re only interested in the number of visitors now browsing a website; grab it, record it, keep moving.

– Number of visitors in the last five minutes in a website: that’d be a counter, plus some basic arithmetics (current value – value five minutes ago).

– Memory use: gauge. We’ll send info on % of memory use at the time when the info is requested.

– CPU use: gauge, as the previous one.

– Number of requests: counter.

There is a bit of complication when you want to measure how many times something happens in certain amount of time, since you need a counter to track down the number of occurrences of the event, but you need to keep track of the previous value of the counter when the time interval comes to an end. To make your life easier, some libraries written to help you track down values for your KPI’s contemplate a third type of metric, called, precisely, meters… but that’s getting out of the scope of the present post.