Apply filters in Promql

304 views
Skip to first unread message

BHARATH KUMAR

unread,
Sep 12, 2022, 9:56:25 AM9/12/22
to Prometheus Users
Hello all,

Can we apply filters in the prometheus query?

I had a query up{instance=~"$instance"}

for reachable up{instance=~"$instance"} == 1
for unreachable up{instance=~"$instance"} == 0

Can I combine the above two queries conditions (==0 and ==1) in a single query?

Thanks & regards,
Bharath Kumar.

Brian Candler

unread,
Sep 12, 2022, 2:26:42 PM9/12/22
to Prometheus Users
up{instance=~"$instance"} == 1 == 0     # technically valid expression, but useless because it always gives zero results

What are you actually trying to do?  If this is something to do with Grafana then you're probably going about it the wrong way. In particular, Grafana lets you build a single panel with multiple queries.

BHARATH KUMAR

unread,
Sep 12, 2022, 11:29:13 PM9/12/22
to Prometheus Users
I have an "N" number of servers. for example, let the value of N is 100. Out of 100 60 are reachable, 30 are unreachable, and the remaining 10 servers are in another database. So in the grafana dashboard, I want to add a variable as a filter, so if I click that filter the promql should be able to get the records as per the requirement. 

Like the filter dropdown will be All, Reachable Unreachable.
The query should be like  "up{instance=~"$instance"}  some regex/filters"

so whenever I click from the grafana filter(All/Reachable/Unreachable), then the query should automatically change with respect to the regex/filter we used. Is that possible?

Thanks & regards,
Bharath Kumar.

Brian Candler

unread,
Sep 13, 2022, 3:01:38 AM9/13/22
to Prometheus Users
Remember that the $var syntax is part of Grafana, not part of Prometheus.  All the variables are substituted before the Prometheus even sees the query.

Maybe something like this would work (I've never tried it):
up{instance=~"$instance"}  == $state

where $state is set to 0 or 1.  But that's probably not what you want, as there's no "all" option.

Your question is about Grafana and how it handles local variables and drop-down selections, and how those variables interact with each other. You should be asking Grafana experts about whether it's possible in Grafana to configure what you want, and if so, how.  Prometheus is the database backend, and simply answers the queries that Grafana sends to it.
Reply all
Reply to author
Forward
0 new messages