Skip to content

Prometheus Metric Types Confusion Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Prometheus Metric Types Confusion Fix. We cover key concepts, practical examples, and best practices.

Your PromQL expression returns unexpected results — rate() on a gauge shows no data, increase() on a counter shows negative values, or histogram quantile calculations seem wrong. Each Prometheus metric type has specific rules for PromQL functions.

The Problem

// WRONG: rate() on a gauge metric
rate(memory_usage_bytes[5m])
Empty result (no data)

rate() only works on Counter metrics (monotonically increasing). memory_usage_bytes is a Gauge (can go up and down). Using rate() on a Gauge produces no results.

Step-by-Step Fix

1. Use correct functions per metric type

// Counter metrics (monotonically increasing)
rate(http_requests_total[5m])           // Requests per second
increase(http_requests_total[1h])       // Total increase over 1h

// Gauge metrics (up and down)
memory_usage_bytes                       // Current value
avg_over_time(cpu_usage_percent[5m])    // Average over time

// Histogram metrics (bucketed observations)
histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))

// Summary metrics (pre-computed quantiles)
http_request_duration_seconds{quantile="0.95"}

2. Follow metric naming conventions

// Counters end with _total
http_requests_total
cpu_seconds_total
bytes_sent_total

// Gauges have descriptive names
memory_usage_bytes
cpu_temperature_celsius
queue_size

// Histograms have _bucket, _count, _sum suffixes
http_request_duration_seconds_bucket
http_request_duration_seconds_count
http_request_duration_seconds_sum

// Summaries have _count, _sum, and quantile labels
http_request_duration_seconds_count
http_request_duration_seconds_sum

3. Use _total suffix for counters

// Prometheus automatically adds _total
// If metric is named http_requests, PromQL uses http_requests_total

// These are equivalent:
rate(http_requests_total[5m])
rate(http_requests[5m])  // Prometheus appends _total internally

4. Check metric type with PromQL

// View metric metadata including type
metrics_metadata?metric=http_requests_total

// Or in Prometheus UI: Status → Metric Metadata

5. Convert between types properly

// Counter → rate (per-second)
rate(http_requests_total[5m])

// Counter → increase (absolute over window)
increase(http_requests_total[1h])

// Gauge → predicted value (linear regression)
predict_linear(memory_usage_bytes[1h], 3600)

// Histogram → average
rate(http_request_duration_seconds_sum[5m])
/
rate(http_request_duration_seconds_count[5m])

Expected output:

rate(http_requests_total[5m]) → 45.2 (requests/second)
memory_usage_bytes → 838860800 (800MB)
histogram_quantile(0.95, ...) → 0.345 (345ms at p95)

Prevention Tips

  • Use _total suffix for counter metrics
  • Use rate() only on counters, avg_over_time() on gauges
  • Check metric type with metrics_metadata API before writing queries
  • Use histogram for latency distributions, summary for pre-computed quantiles
  • Follow Prometheus naming conventions for clarity

Common Mistakes with metric types

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to 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

### What's the difference between histogram and summary?

Histogram calculates quantiles on the server side from bucket counters — you can change quantile values later. Summary pre-computes quantiles on the client side — uses less storage but quantile values are fixed at instrumentation time. Histogram is preferred for most use cases.

Why does increase() sometimes show non-integer values?

increase() extrapolates from the rate over the window. If the counter resets (e.g., process restart), the extrapolation may produce non-integer values. Use rate() * window_seconds for more predictable results.

Can I change a metric type after ingestion?

No, metric type is determined at registration time. The _total naming convention is a convention, not enforced. If you register a _total counter as a Gauge, PromQL functions will still work but the semantic meaning is lost.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro