prometheusgrafanakamon

Understanding range vector selectors


From reading https://prometheus.io/docs/prometheus/latest/querying/basics/ a 'Range Vector Selectors' are defined as :

Range vector literals work like instant vector literals, except that they select a range of samples back from the current instant. Syntactically, a time duration is appended in square brackets ([]) at the end of a vector selector to specify how far back in time values should be fetched for each resulting range vector element.

In this example, we select all the values we have recorded within the last 5 minutes for all time series that have the metric name http_requests_total and a job label set to prometheus: http_requests_total{job="prometheus"}[5m]

I'm failing to understand how these selector's behave. My understanding of the query :

sum(rate(data_total[1h]))

is return the rate of change of the data_total metric using a time during of 1h. Is this correct ? Is each point on the graph the sum(rate( of the previous hour ?

sum(rate(data_total[1h])) :

From the above definition shouldn't 1h of data be displayed ? Instead may of hours of data are displayed : enter image description here

Changing from 1h to 4h :

sum(rate(data_total[4h])) :

renders :

enter image description here

Why has the shape changed when changing from [1h] to [4h] ?


Solution

  • The range vector selector is just an ordinary time series selector (which is confusingly named instant vector selector) with the added lookbehind window in square brackets.

    An ordinary time series selector (aka instant vector selector) selects time series matching the given filter. For example, http_requests_total{path=~"/foo/bar|/baz"} selects time series with the name http_requests_total and the path label containing either /foo/bar or /baz values.

    The corresponding range vector selector with the one hour lookbehind window looks like the following http_requests_total{path=~"/foo/bar|baz"}[1h].

    The range vector selector can be used in the following places:

    Let's look how rate(m[d]) is calculated on the start ... end time range with the given step:

    See also this answer.