DellEMC

Grafana “invalid query: unexpected character”

While working on a dashboard in Grafana I came across an error that puzzled me for a while. So I am writing this in the hope that it will help other people who value their time 🙂

My setup is quite straight forward:

  • Prometheus v2.46.0
  • Grafana v7.5.15

However, don’t get too hung up on the versions because I have seen other people complaining about the same behavior with other versions. The key is that the problem might lie with the browser. For completeness, I am using Google Chrome v116.0.5845.141.

The symptoms are:

  • I edit a new panel in a dashboard and I start typing the Prometheus query in the “Metrics” box. When I put a metric only nothing bad happens, but as soon as I add a “curly bracket” ie “{” the Metrics box gets a life of its own. It starts duplicating letters and the cursors don’t seem to be responding. When using the cursors it feels like I need to use the cursor 3 times to advance a single character.
  • The panel gets a red exclamation box in the top-left corner. If I hover over the exclamation the following message pops up “invalid parameter \”query\”: 1:21: parse error: unexpected character: ‘\\ufeff'”

This is what it looks like in my case

Everything points to some strange characters being inserted

If it is true that the problem is the browser, you might have to use a different browser. But as a workaround what I figured is that you can edit the Metric query in the panel’s JSON source directly.

You can get there by either:

  • click on the “red exclamation” and then the “JSON” tab
  • Or click on the panel’s name drop-down arrow, then “Inspect” and then “JSON”

At this point if you scroll down to “targets” you can see the “expr” field with all the garbage around it

Now you can click on the “expr” line and edit the query cleanly without introducing those weird characters.

IMPORTANT: One thing to watch out for is that you need to escape the double quotes in your query with a backslash. Otherwise they will get confused with the JSON’s own double quotes. This is what it looks like in my example:

storage_free_percent{system_type=~\"$system_type\",name=~\"$name\"}

I hope it helps!

Categories: DellEMC

Tagged as: , ,

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.