How to filter prometheus query by label value using greater-than

7.7k Views Asked by At

If I have matrics like this

my_metric{deployTime="1603284798",foo='bar'}
my_metric{deployTime="1603284799",foo='bar2'}
my_metric{deployTime="1603284800",foo='bar3'}

And I want to get only the metrics where the timestamp is greater than some value, how can I do it?

I was thinking of:

count by (deployTime, foo) (my_metric{deployTime > "1603284799", foo=~".*"})

but of course, this doesn't work as labels are strings. So what are my options to filter by label value if I want to use the greater than operator?

2

There are 2 best solutions below

0
On

This can be done with label_value() function from MetricsQL:

count by (deployTime, foo) (
  label_value(my_metric{foo=~"..."}, "deployTime") > 1603284799
)

MetricsQL also supports now() function, which can simplify calculations for timestamp relative to the current time. For example, the following query would filter out time series with deployTime values older than one hour from now:

label_value(my_metric{foo=~"..."}, "deployTime") > now() - 1h
2
On

I don't know if I understood correctly but, do you have a label called "timestamp"? This seems to be unnecessary.

I think you can achieve what you want with the following query:

count by (foo) (timestamp(my_metric)>1603284799)

UPDATE

After you have clarified your question the answer is: unfortunately you can't query for labels using "greater than", "less than", etc.