Get the latest value per timeserie

7.8k Views Asked by At

How to query the latest value per timeserie in MetricsQL or PromQL?

For example, query metric_name returns two timeseries for metric_name{job="foo"} and metric_name{job="bar"} for a long range:

{"metric":{"__name__":"metric_name","job":"foo"},"values":[.................. <long list>],
{"metric":{"__name__":"metric_name","job":"bar"},"values":[.................. <long list>]

Is there a way to get the latest value for each label? So that response would contain only two timestamps -- one for job="foo", and another for job="bar":

{"metric":{"__name__":"metric_name","job":"foo"},"values":[1510000000,123],
{"metric":{"__name__":"metric_name","job":"bar"},"values":[1610000000,321]
1

There are 1 best solutions below

2
On

Have you tried to use last_over_time?

last_over_time(m[d]) - returns the last value for m on the time range d.

See more details about MetricsQL here - https://github.com/VictoriaMetrics/VictoriaMetrics/wiki/MetricsQL

You might want to use /api/v1/query endpoint to get an instant query result.