Prometheus Counter vs Gauge Metric Wrong Choice Fix
In this tutorial, you'll learn about Prometheus Counter vs Gauge Metric Wrong Choice Fix. We cover key concepts, practical examples, and best practices.
Your Prometheus metric shows confusing behavior — the value keeps going up when you expect it to reset, or it fluctuates when you expect a cumulative total. You chose the wrong metric type between Counter and Gauge.
The Problem
# WRONG: using a Gauge for cumulative request count
from prometheus_client import Gauge
request_count = Gauge('http_requests_total', 'Total requests')
request_count.inc() # Goes up and down — wrong!
def handle_request():
request_count.inc()
When the process restarts, the Gauge resets to 0 and starts from there, making rate() calculations inaccurate. The total number of requests should be monotonically increasing (a Counter).
Step-by-Step Fix
1. Use Counter for cumulative values
from prometheus_client import Counter
# CORRECT: Counter is monotonically increasing
request_count = Counter('http_requests_total', 'Total requests')
def handle_request():
request_count.inc()
2. Use Gauge for point-in-time values
from prometheus_client import Gauge
# Gauge for current values
temperature = Gauge('cpu_temperature_celsius', 'Current CPU temperature')
memory_used = Gauge('memory_usage_bytes', 'Current memory usage')
active_users = Gauge('active_users', 'Number of active users')
def measure():
temperature.set(72.5)
memory_used.set(838860800)
active_users.inc() # User logged in
active_users.dec() # User logged out
3. Use Counter for rates and totals
// Counter: queries focus on rate of change
rate(http_requests_total[5m]) // Requests per second
increase(http_requests_total[1h]) // Total in last hour
// Gauge: queries focus on current value or average
memory_usage_bytes // Current memory
avg_over_time(cpu_usage_percent[5m]) // Average CPU
max_over_time(memory_usage_bytes[1h]) // Peak memory
4. Common use cases for each type
# Counter (always increasing):
# - HTTP requests served
# - Errors occurred
# - Bytes sent/received
# - Tasks completed
# Gauge (up and down):
# - Memory usage
# - CPU temperature
# - Queue depth
# - Active connections
# - Disk space
# - Concurrent users
Expected output:
# Counter: always shows total since start
http_requests_total 15000
# Gauge: shows current value
memory_usage_bytes 838860800
active_users 42
Prevention Tips
- Use Counter for "total so far" — things that only increase
- Use Gauge for "right now" — things that go up and down
- Counter names should end with
_total - Use
rate()andincrease()with Counters - Use
avg_over_time(),max_over_time()with Gauges
Common Mistakes with counter gauge
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world PROMETHEUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro