PromQL "where" clause

4/13/2021

How does one add a where clause in PromQL?

I'm trying to construct a query that displays when an application running in Kubernetes has been up for more than one minute but I want to filter by namespace.

This is what my query looks like at the moment

100 * (count(up == 1) BY (job, namespace, service) ) > 1

This works fine but it gives me additional information that I don't need.

{job="prometheus-grafana", namespace="monitor", service="prometheus-grafana"}
{job="jenkins", namespace="jenkins", service="jenkins"}
{job="kube-state-metrics", namespace="monitor", service="prometheus-kube-state-metrics"}
{job="node-exporter", namespace="monitor", service="prometheus-prometheus-node-exporter"}
{job="kubelet", namespace="kube-system", service="prometheus-kube-prometheus-kubelet"}
{job="apiserver", namespace="default", service="kubernetes"}

What I'm trying to accomplish is to get results for only the jenkins and default namespace.

{job="apiserver", namespace="default", service="kubernetes"}
{job="jenkins", namespace="jenkins", service="jenkins"}

I've tried doing

100 * (count(up == 1) BY (job, namespace, service) ) > 1 and ON {namespace="jenkins"}

But I get an invalid parameter "query": 1:65: parse error: unexpected "{" in grouping opts, expected "(" error.

-- Hammed
kubernetes
prometheus
promql
where-clause

3 Answers

8/3/2021
  • You can try this too. In Kubernetes all resources uses pod. So if you take pod status metrics and minus current time with 60, which gives post 1 min pods running status.

time()-60 > (kube_pod_start_time)

-- chaitanya vura
Source: StackOverflow

4/1/2022

Prometheus provides the following ways for filtering the data in queries:

  • Time series selectors. They allow filtering time series by metrics and labels. For example, up{namespace=~"default|jenkins"} is a series selector, which returns only time series with the name up, which contain label namespace matching the given given regular expression: default|jenkins. This is roughly equivalent to the following SQL:
SELECT * FROM table WHERE name = 'up' and namespace ~ '^(default|jenkins)
#x27;
  • Comparison operators, which allow filtering time series by values. For example, up == 0 returns time series with up name, which have 0 value. This is roughly equivalent to the following SQL:
SELECT * FROM table WHERE name = 'up' and value == 0
  • Time series matching via binary operators. This allows performing join-like queries. For example, up * on(instance) group_left(name) node_os_info joins up metric with node_os_info metric via instance label and selects additional name label from node_os_info metric. This is roughly equivalent to the following SQL:
SELECT up.*, node_os_info.name
FROM up LEFT JOIN node_os_info ON (instance)
-- valyala
Source: StackOverflow

4/13/2021

You would have to filter the metric "up" by the labels you want (namespaces) in your case it should look something like this:

100 * count(up{namespace=~"default|jenkins"} == 1) > 1
-- Petar Nikolov
Source: StackOverflow