Video in progress

What is a metric? Give an example of a counter and a gauge.

A metric is a number measured over time. A counter only goes up (the number of requests, the number of errors), while a gauge shows the current value and can rise and fall (items in a queue, memory in use).

📖 Answer

A counter is a tally. It only goes up until a restart:

  • the count of http_server_requests;
  • the number of SMS messages sent;
  • payment_failed_total.

A counter's rate of growth is more interesting than the counter itself: "how many errors per second".

A gauge is a "snapshot" of the current state:

  • the number of messages waiting in a queue;
  • active DB connections;
  • memory in use on the heap.
Counter failed = Counter.builder("payment.failed")
        .description("Rad etilgan to'lovlar")
        .register(registry);
failed.increment();

Gauge.builder("orders.queue.size", queue, Queue::size)
        .register(registry);

A confusing spot: you can't make "the number of active users" a counter, because it goes down. And the other way around: if you make the total request count a gauge, Prometheus won't correctly understand the drop after a restart.