Gauge Metrics
Gauge metrics represent a current value that can move in either direction, such as queue depth, memory usage, or active connections. They are useful when the latest state matters more than the total number of events.
A gauge measures a value at a point in time. Unlike a counter, which normally increases as events occur, a gauge can rise or fall as the underlying state changes.
Typical backend examples include the number of active HTTP requests, items waiting in a queue, thread-pool utilization, or currently open connections. The metric should correspond to something whose current state has operational meaning.
With Java metrics libraries such as Micrometer, a gauge is commonly connected to an object or value supplier. The monitoring system observes that value when it collects metrics; application code generally changes the underlying state rather than manually incrementing and decrementing the published gauge.
A gauge is especially useful for alerting on conditions such as a queue remaining above a threshold or connection usage approaching a limit. For events that accumulate over time, such as requests processed or errors encountered, a counter is usually the more appropriate metric type.
Key points
- A gauge represents a current, changeable value.
- Gauges can increase or decrease.
- Common examples include queue depth, active connections, and memory usage.
- The monitored value should represent meaningful current state.
- Use counters for cumulative event totals instead of current-state measurements.
Interview questions
How would you expose a gauge in a Java backend?
With a Java metrics library such as Micrometer, I would associate the gauge with an object or value supplier representing the state I want to observe. For example, an `AtomicInteger` can represent active connections and a gauge can read its current value. Application code updates the underlying state when connections open or close. The monitoring system then observes the current value during metric collection.
What is a gauge metric, and when would you use one?
A gauge represents the current value of something that can move up or down. Examples include active connections, queue size, and memory utilization. I would use it when the current state itself is what I need to monitor. For cumulative events such as total requests, I would normally use a counter instead.
Why should a queue depth usually be represented as a gauge?
Queue depth describes how many items are currently waiting, so it naturally changes in both directions. Adding work increases the value, while consuming work decreases it. A gauge therefore captures the current backlog directly. A counter would instead describe cumulative events, such as how many items have been added over time.
What are common alerting mistakes with gauges?
A common mistake is treating every threshold crossing as an incident without considering how long the condition persists. Gauge values can fluctuate naturally, so an alert should often require the threshold to remain violated for a meaningful period. Another mistake is choosing a gauge for cumulative events, which loses the information needed to calculate event rates correctly. Alert thresholds should also account for normal workload and capacity.