Search code examples
prometheusgrafanapromql

Prometheus where/if clause


I have query I want to run only on hosts with a certain service running

the pseudo code would look something like

windows_os_virtual_memory_bytes 
where
windows_service_state{name="sense",state="running"} == 1

So that I only get the virtual memory if the service with name="sense" is active


Solution

  • Use the on clause to match labels. In this case,

    windows_os_virtual_memory_bytes > 0 and  on(instance) (windows_service_state{name="sense",state="running"} == 1) 
    

    So that both conditions must be met (and) where instance matches. You can use a vector of labels in the on() function.

    https://prometheus.io/docs/prometheus/latest/querying/operators/#one-to-one-vector-matches