I need to use the number of CPU cores in multiple calculations on a Grafana dashboard and was hoping to perform the query once and store in a variable. I can't find a way to return the data in a format that Grafana will accept.

Grafana Error:

Template variables could not be initialized: parse error at char 1: vector selector must contain label matchers or metric name

I have tried (PromQL):



Element {}, Value 2

Also (PromQL):



Element scalar, Value 2

Using Grafana's query_result function like so:


I get the following values in the preview:

{} 2 1521669355000

The 2 in the middle is the one I want.

Any ideas how to make this work?


You can use Regex field to grab your value, in your case: /.* (.*) .*/

| improve this answer | |

You should avoid scalars for things like this as it doesn't work if for example you want to graph something with two hosts that might have different numbers of cores. I'd suggest doing this inside each query expression, rather than via a template variable.

| improve this answer | |

This works if you put this in the regex box in grafana

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.