When querying Prometheus for
process_cpu_seconds_total{instance="localhost:9090"}
I see the browser makes a call where the current time is used in the query parameter time:
curl 'http://localhost:9090/api/v1/query?query=process_cpu_seconds_total%7Binstance%3D%22localhost%3A9090%22%7D&time=1700078953.325' \
--compressed
And the result
{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"process_cpu_seconds_total","instance":"localhost:9090","job":"prometheus"},"
value":[1700078953.325,"0.82"]}]}}%
ALWAYS matched the timestamp given as query parameter.
When I query for a range vector, the result is close to what I expect, the browser still uses the current time as query param, but in the result usually this time is not there as the metric is only scraped every 15 seconds.
curl 'http://localhost:9090/api/v1/query?query=process_cpu_seconds_total%7Binstance%3D%22localhost%3A9090%22%7D%5B1m%5D&time=1700078953.325' \
--compressed
{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"process_cpu_seconds_total","instance":"localhost:9090","job":"prometheus"},"
values":[[1700078900.426,"0.8"],[1700078915.427,"0.8"],[1700078930.388,"0.82"],[1700078945.388,"0.82"]]}]}}%
Why does prometheus always pretends to know the value of now, when we ask query for an instant vector ?
When you query for instant vector (directly or indirectly, like sum(my_metric)
or metric1 + metric2
) Prometheus simply returns newest sample before requested time. This is mainly to support cases like aggregation (sum
, avg
, and so on), where multiple aggregated time series do not exactly align in time.
When you query range vector Prometheus returns values from within specified range as is. Since range vectors can only be used in limited cases (mainly aggregation/processing over time and rarely by simply requesting my_metric[1m]
) it is reasonable to expect that none of those cases don't require any alignment for different time series, and thus no such mechanism is implemented.