🎓 A1Learner
🔍
0 of 50 lessons completed

Prometheus: Choosing the Right Metric Type

Choosing a metric type is a decision about what the number must do later. The wrong type hides the answer the learner needs.

Four questions that pick the type

Answer these in order before writing any metric code.

1. Does it only rise? Use a counter.
2. Does it move both ways? Use a gauge.
3. Does its spread matter? Use a histogram.
4. Do you need percentiles later? Use a histogram.

The wrong choice, shown live

Requests in flight as a counter would climb forever and never fall.

shopsentry_in_flight 1

The right choice, shown live

The same value as a gauge reads 1 while one request runs.

shopsentry_up 1
shopsentry_in_flight 1

Example

curl -s http://localhost:18102/metrics | grep -E "shopsentry_in_flight|shop_orders_total"

Key points

  • Ask first whether the value only rises, moves both ways, or needs size buckets.
  • A value that only rises is a counter, like requests served or orders placed.
  • A value that moves both ways is a gauge, like requests in flight or memory used.
  • A value whose spread matters is a histogram, like request latency in seconds.
  • A wrong choice shows up fast, because the number cannot answer the question.
  • A counter for requests in flight fails, since it can never fall back to zero.

In this example, the live metrics page shows the decision working. Requests in flight is a gauge, so it reads 1 right now and can fall to 0. Orders placed is a counter, so it reads 78 completed and 5 failed and only ever rises. Each number answers its own question because its type fits.

Note

Pick the type from the question, not from the code that is easiest to write.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

The shop wants to know how slow its slowest requests are. Which type fits?