Not all numbers are created equal. To effectively monitor a system, we use four fundamental types of metrics. Understanding the difference is crucial for choosing the right tool for the job — using the wrong type leads to misleading data or wasted storage.
1. Counter: The "Up-Only" Metric
A Counter is a cumulative metric that represents a single numerical value that only ever goes up. Think of an odometer in a car — it records total miles driven and never resets (unless the car is replaced).
- Behavior: It starts at zero and increments. It never goes down (unless the process restarts, resetting it to zero — monitoring systems account for this with "reset detection").
- Use Cases:
- Total number of HTTP requests received.
- Total number of errors logged.
- Total tasks completed.
- Total bytes sent over a network interface.
You almost never care about the absolute value of a counter (e.g., "we have served 5 million requests since restart"). Instead, you care about how fast it's increasing over time, calculated as a rate (e.g., "requests per second"). In Prometheus, the rate() function does exactly this — computing requests-per-second over a trailing window.
2. Gauge: The "Right Now" Metric
A Gauge is a metric that represents a single numerical value that can arbitrarily go up and down. Think of a speedometer or a fuel gauge — it shows the current state, not a cumulative total.
- Behavior: It shows a snapshot of the current state. It can be set to any value at any time.
- Use Cases:
- Current memory usage in bytes.
- Current CPU utilization percentage.
- Number of items currently in a queue.
- Number of active database connections.
- Temperature of a server.