Search code examples
grafanaprometheus

grafana define variable with prometheus query based on metrics


I am pretty new to Grafana, so the question might be an easy one:

I try to store a metric value in a variable. Therefore I setup a variable with Prometheus query:

metrics(passed_tests_total{job="MyJob"})

Surprising to me, the value returns value None, although metric values with that label exist. I verified that by setting up a 'singlestat' panel with query passed_tests_total{job="MyJob"} which works perfectly fine.

So my question: how can I store a metric value to a variable?

Remark: my approach is basing on docu http://docs.grafana.org/features/datasources/prometheus/


Solution

  • If you want to retrieve the value of a metric you should use query_result(), metrics() gives you the name of matching metrics, not the value itself.

    Your Query should be: query_result(passed_tests_total{job="MyJob"})

    And the Regex to extract just the value of metric should be /.* ([^\ ]*) .*/.