Search code examples
prometheusgrafanapromql

Can PromQL query metrics that some label less equal than some value?


For example, my metric looks like this:

node_signal{date="0731"} 1
node_signal{date="0801"} 1.5
node_signal{date="0802"} 0

I'm looking for something to query like this in grafana:

node_signal{date <= "0801"}

So it returns the first and the second metric. I have no idea is it possible to do this with promql in grafana panels.


Solution

  • What you are describing is not possible. Only available operations for labels are =, !=, =~, !~.

    You can try a workaround based on regex selector. For example you provided in the question you'll need query

    node_signal{date=~"0[1-7]\d\d|0801"}
    

    But this approach is not pretty: it requires new regex for every new day and those regexes while being algorithmically easy generatable, are not easy to comprehend. For example for <=0825 you'll need 0[0-7]\d\d|08[01]\d|082[0-5]


    Additionally, storing dates in labels of your metrics is usually not a good idea. You are increasingly cardinality of your metric which is bad for Prometheus' performance, both computational and storage-wise.

    Consider changing your approach to exposed metrics with relative time: every metric has a timestamp of scraping (unless timestampis specified for the metric while exposing) associated with it. You probably can use something like relative_date="-1" to show that exposed event occurred on the day before scraping.