Course contentsShow
← Software Engineering
Lesson 1139 of 1,93334. Logging and ObservabilityFree lesson

Counter, Gauge, and Histogram Metrics

Choosing the right metric type for monotonic values, snapshots, and distribution measurements.

Counter, Gauge, and Histogram Metrics

What you'll learn: How to choose the right metric type for different kinds of measurements in your observability system.

When instrumenting your application, not all metrics behave the same way. Picking the correct metric type ensures your dashboards and alerts work properly.

The Three Core Metric Types

Counter: Always Going Up

A counter tracks monotonic values that only increase (or reset to zero). Use counters for:

  • Total HTTP requests served
  • Number of errors encountered
  • Bytes sent over the network

Think of a car's odometer—it only goes up. You typically care about the rate of change ("requests per second") rather than the absolute number.

Gauge: Point-in-Time Snapshots

A gauge represents a value that can go up or down arbitrarily. Use gauges for:

  • Current memory usage
  • Number of active connections
  • Queue depth
  • CPU temperature

Think of a thermometer or fuel gauge—it shows the current state right now. You care about the actual value at any moment.

Histogram: Understanding Distributions

A histogram records observations (usually durations or sizes) and groups them into buckets to understand distribution. Use histograms for:

  • Request latency (p50, p95, p99 percentiles)
  • Response payload sizes
  • Database query durations

Think of sorting test scores into grade ranges (A, B, C, D, F). You want to know how values are distributed, not just the average.

Choosing the Right Type

  • If it only goes up: Counter
  • If it fluctuates: Gauge
  • If you need percentiles or distribution: Histogram

Misusing types causes problems: tracking request count as a gauge makes rate calculations impossible; using a counter for memory usage loses the ability to see decreases.

Key Takeaway: Counters measure cumulative totals, gauges capture current state, and histograms reveal distributions—choose based on whether your metric is monotonic, fluctuating, or needs statistical analysis.