🎓 A1Learner
🔍
0 of 50 lessons completed

Prometheus: Custom Metrics That Answer Real Questions

A custom metric is a number you add because a real question needs it. Instrument the question, not the code.

Errors by route

The status label splits requests into successes and failures per route.

http_requests_total{method="GET",route="/api/orders",status="500"} 5

Orders by status

One counter with a status label counts completed and failed orders apart.

shop_orders_total{status="completed"} 78
shop_orders_total{status="failed"} 5

Latency by endpoint

The histogram holds one set of buckets for all routes in this demo.

http_request_duration_seconds_bucket{le="0.005"} 248
http_request_duration_seconds_count 254

Example

curl -s "http://localhost:18102/break?mode=errors"; for i in 1 2 3 4 5; do curl -s -o /dev/null http://localhost:18102/api/orders; done; curl -s http://localhost:18102/heal; curl -s http://localhost:18102/metrics | grep -E "shop_orders_total|status=\"500\""

Key points

  • A good metric starts from a question, like how many orders failed today.
  • Errors by route need a counter with a status label on each request.
  • Orders by status need a counter with one series per status, like completed and failed.
  • Latency by endpoint needs a histogram with one set of buckets per route.
  • A label splits one metric into useful series, so one counter answers many questions.
  • Every label value becomes a series, so keep the label values few and steady.

In this example, five real orders failed while errors mode was on. The orders counter shows 5 failed against 78 completed, and the requests counter shows 5 failures on the orders route. Both numbers answer a real question a shop owner would ask.

Note

A label that holds a user name or an order id would create endless series, so never label by those.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

The shop owner asks which route produces the most errors. What does the metric need?