2

In PromQL we use range vector selectors to get range vectors for functions like rate(). Grafana provides the dynamic variable $__interval and it is used like this:

sum(rate(my_metric{foo="bar"}[$__interval]))

It's value is an approximation so that the resulting range vector contains one entry for about every pixel (or more). From my understanding this prevents overfetching.

So far so good. But looking at multiple dashboards available in Grafana Cloud or for example the demo dashboards by Robust Perception, nobody is using $__interval. Instead people opt for 1m or 5m even if no recording rules or alerting is used.

Is there a reason for that?

trallnag
  • 2,041
  • 1
  • 17
  • 33

1 Answers1

4

The $__interval is useful for graphs which may be zoomed in / zoomed out. In this case Grafana automatically adjusts $__interval for the current zoom level. For example, $__interval=15s is used for a graph over 15s*1000=~4 hours time range (1000 is the approximate number of points per time series Grafana requests for building the graph; this is usually enough for a monitor with horizontal resolution of up to 4000 pixels), while $__interval=1h is used for a graph over 1h*1000=~40 days time range. This allows taking into account all the raw data points from the original time series when building the graph.

If a fixed 5m value is used in square brackets, then the accuracy of the graph may suffer on time ranges smaller than 5m*1000=~3.5 days. If the time range of the graph exceeds 3.5 days, then some raw data points will be missing in calculations.

valyala
  • 11,669
  • 1
  • 59
  • 62
  • Well this sounds like `$__interval` is superior to fixed alternatives and still most people use fixed values, for example the official dashboard for the node exporter https://grafana.com/grafana/dashboards/1860. I just wonder why that is the case – trallnag Aug 05 '20 at 11:10
  • 2
    Because of two reasons: 1) the `$__interval` is supported only in Grafana - it isn't a part of PromQL. So queries with `$__interval` inside them cannot be copied to alerting or recording rules. 2) the `$__interval` value may become too small on short time ranges. This may break some graphs. See, for example, https://www.percona.com/blog/2020/02/28/better-prometheus-rate-function-with-victoriametrics/ – valyala Aug 05 '20 at 14:58