1

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]
Dzmitry Lazerka
  • 1,809
  • 2
  • 21
  • 37

1 Answers1

1

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.

hagen1778
  • 669
  • 3
  • 8
  • `/api/v1/query` will return only one result, as it requires `time=****` param, even if implicitly – Dzmitry Lazerka Aug 18 '21 at 14:24
  • 1
    last_over_time() may not return a single value either, or, more correctly, it respects `step` query parameter. I tried `last_over_time(vm_app_version[365d])` with different `step` values. Seems like the problem has to deal more with `step` param and not query, but I'm not sure what to do with it. – Dzmitry Lazerka Aug 18 '21 at 14:31