Grafana/InfluxDB: Query works in a panel, but errors when assigning to a variable

I have a influxDB data set containing a large number of time series data from a bunch of virtual machines.

I’m trying to set up a variable to return the names of the top 10 machines with the highest cpu usage above a threshold.

The following query works in the query editor (I can get the results I want in a panel):

SELECT “name” FROM (SELECT TOP(usage, 10), “name” FROM (SELECT SUM(“cpu”) AS usage FROM “location” WHERE (“type”=~/^$microservice$/) AND $timeFilter AND “cpu” >= $cpuLimit GROUP BY “name”))

But when I try and define a new variable with this query, it errors out. For reference, $microservice is the name of the program selected, and $cpuLimit is another variable containing the threshold.

If I do:

SELECT “name” FROM (SELECT TOP(usage, 10), “name” FROM (SELECT SUM(“cpu”) AS usage FROM “location” WHERE (“type”=~/^$microservice$/) AND $timeFilter AND “cpu” >= 0.5 GROUP BY “name”))

Then the variable works just fine. It’s when I put in the $cpuLimit that I get the following errors: Validation and Templating Errors

Which says that there’s an unexpected character at position 216, but there’s not even 216 characters in my query so I am a bit lost. Why does it work in a panel but not in a variable? Any thoughts would be much appreciated!

One other thing I just noticed - it errors when $cpuLimit < 1 - it seems that if there is a decimal in $cpuLimit, such as “0.5”, it throws the error.

Am I not able to use decimals in variables? Why does it work in the panel if that is the case?

Hello @tdone,
This sounds like a grafana issue, since the InfluxQL is working as expected without grafana variables. Can I ask you to please ask your question here:

Thank you
it looks like some sort of formatting issue when injecting the value into the api call

Hi @Anaisdg - thank you for getting back to me. I was thinking this was a Grafana bug. I made a post about it here: Query works in a panel, but errors when assigning to a variable - Grafana - Grafana Labs Community Forums

Hopefully we hear back from the Grafana folks soon!

1 Like

I finally fixed it!

I did “cpu” >= ${cpuLimit:text}

Found documentation here: Variable syntax | Grafana Labs