Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backslash in Prometheus label value does not work with dashboard variable #24478

Closed
Mario-Hofstaetter opened this issue May 9, 2020 · 5 comments

Comments

@Mario-Hofstaetter
Copy link

Mario-Hofstaetter commented May 9, 2020

What happened:
A prometheus label value containing a backlash \ causes an error when using the value via grafana dashboard variable. Example: logfile="C:\test\logfile.txt"

What you expected to happen:
There should be some way to make it work?

How to reproduce it (as minimally and precisely as possible):

  • Have a prometheus metric, with some label with a value containing a backlash, e.g. backslashtest{logfile="C:\test\logfile.txt"} , easiest created by textfile collector.
  • Create a dashboard variable Name=logfile, Type=Query, Datasource=Prometheus, Query=label_values(backslashtest, logfile), this gives:
    grafik
  • Try to query the metric using this variable, I got no working solution.
backslashtest{logfile="$logfile"}

or

backslashtest{logfile=~"$logfile"}

yields:

"1:23: parse error: unknown escape sequence U+006C 'l'"

grafik

I tried all the advanced-variable-format-options, neither worked.

backslashtest{logfile="${logfile:regex}"}

yields "1:23: parse error: unknown escape sequence U+002E '.'"

The only one that does not throw an error is backslashtest{logfile="${logfile:percentencode}"} but Prometheus returns no data for this.

Using the regex Operator =~ makes no difference:

backslashtest{logfile=~"${logfile:regex}"}
backslashtest{logfile=~"${logfile:raw}"}
[...]

Only this works: backslashtest{logfile="C:\\test\\logfile.txt"}

Is there a way to use the variable?

Anything else we need to know?:

Query-Inspector:

{
  "response": {
    "status": "error",
    "errorType": "bad_data",
    "error": "1:24: parse error: unknown escape sequence U+006C 'l'",
    "message": "1:24: parse error: unknown escape sequence U+006C 'l'"
  }
}
  • This issues for example arises because Windows filepaths use backlash as seperator, the metric source in my case is grop_exporter
  • The raw exporter text actually escapes the backlash backslashtest{logfile="C:\\test\\logfile.txt"}
  • When querying in prometheus graph, it needs to be escaped as well

Related?: #10555 #20869

Environment:

  • Grafana version: v6.7.3 and v7.0.0-beta3 (2169fc1a3f)
  • Data source type & version: Prometheus 2.18.1
  • OS Grafana is installed on: Windows 10 1909
  • User OS & Browser: Windows 10 1909 , Firefox 75
@Mario-Hofstaetter Mario-Hofstaetter changed the title Backslash in Prometheus label does not work with dashboard variable Backslash in Prometheus label value does not work with dashboard variable May 9, 2020
@Mario-Hofstaetter
Copy link
Author

Workaround

If anyone lands here via google, I was able to workaround this PAIN by substituting \ with / directly in grok_exporter using the gsub function like so:

  labels:
    logfile: '{{gsub .logfile "\\\\" "/"}}'

so backslashtest{logfile="C:/test/logfile.txt"} works just fine in grafana 🎉

I dunno if prometheus relabeling is able to do this, the docs there did not look that promising on first sight.

@torkelo
Copy link
Member

torkelo commented May 10, 2020

backslashtest{logfile=~"${logfile}"}

Should work as it will use the prometheus specific regex escaping logic.

@Mario-Hofstaetter
Copy link
Author

Mario-Hofstaetter commented May 10, 2020

Are you able to reproduce?
Slightly extended original post, it is happening with v6.7.3 and v7.0.0-beta3

The issue does not appear when using Loki, if using promtail on Windows the logfile label also contains the backslash, it is working fine with the =~ operator.

@aocenas
Copy link
Member

aocenas commented May 29, 2020

Seems like this may be a duplicate of #20869

@ivanahuckova
Copy link
Member

Closed by #26205

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants