Search code examples
prometheusgrafanapromql

PromQL: counter and rate hint


When I'm trying to build a panel into my grafana dashboard using a counter metric, it's telling me:

Selected metric is a counter. Consider calculating rate of counter by adding rate().

  1. Why it suggest me above hint?
  2. Has any relation with scrape_interval?

I'm using those metrics:

  • method_timed_seconds_count -> counter
  • method_timed_seconds_max -> gauge
  • method_timed_seconds_sum -> counter

How should I visualize them? I mean, which promQL should I use?

Currently, I'm using them straightforwardly.


Solution

  • Counter metrics are rarely useful when displayed on the graph. For example, try obtaining any useful information from the graph on the method_timed_seconds_sum metric. That's why it is recommended wrapping these metrics into rate or increase functions:

    • rate(m[d]) returns the average per-second increase rate for counters matching m series selector over the lookbehind window d. For example, rate(method_timed_seconds_count[5m]) returns the average requests per second over the last 5 minites.
    • increase(m[d]) returns the increase of counters matching m over the lookbehind window d. For example, increase(method_timed_seconds_count[1h]) returns the number of requests during the last hour.

    Both method_timed_seconds_count and method_timed_seconds_sum counters can be used for calculating the average request duration on an arbitrary lookbehind window. For example, the following query returns the average request duration over the last 5 minutes:

    increase(method_timed_seconds_sum[5m])
      /
    increase(method_timed_seconds_count[5m])
    

    E.g. this query divides the sum of duration of all the requests during the last 5 minutes by the the number of requests during the last 5 minutes.