Search code examples
prometheusgrafana

Prometheus query not working for Grafana Variable


I am trying to create a variable in Grafana using below query to return me a number based on cpu usage percentage. This works fine for panels but it throws an error when I try to create a variable out of it.

(vector(1) and on() (sum(node_namespace_pod_container:container_cpu_usage_seconds_total:sum_irate)/0.04)<50)
or on() (vector(2) and on() (sum(node_namespace_pod_container:container_cpu_usage_seconds_total:sum_irate)/0.04)<80)
or on() vector(3)

Error Message


Solution

  • You have a couple of problems here:

    1. query is too complicated to be parsed as is. Grafana will have better understanding what is it expected to do, if function query_result is used.
    2. you query contains new-lines, and Grafana is not happy with them for whatever reason.

    I reproduced your error with similar query, and it began to work correctly, once new-lines were removed.

    My query:

    query_result((vector(1) and on() (sum(node_cpu_seconds_total)/0.04)<1000000000) or (vector(2) and on() (sum(node_cpu_seconds_total)/0.04)<8000000000) or vector(3))
    

    Tested at play.grafana.com