client_java icon indicating copy to clipboard operation
client_java copied to clipboard

Unable to pass doublequote to labels method

Open aarthipa opened this issue 8 years ago • 3 comments

I am trying to check is certain special unicode chars can be parsed by the prometheus client as per the documentation here https://prometheus.io/docs/instrumenting/exposition_formats/#text-based-format

So I'm first trying to test the prescribed clients in the prometheus site. When I try to pass backslash as a string value to the labels method of the java client, the output does not escape the backslash. Here is the code.

static final Counter requests = Counter
									.build()
							        .name("hello_worlds_total")
							        .help("Number of hello worlds served.")
							        .labelNames("utfChars")
							        .register();

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
    throws ServletException, IOException {
  resp.getWriter().println("Hello World!");
  String utfCharValue = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C" + "\"" + "\u0B85");
  System.out.println(utfCharValue);
  // Increment the number of requests.
  requests.labels(utfCharValue).inc();      
}

}

Output on console: 2018-07-24 09:52:52.671:INFO:oejs.AbstractConnector:Started [email protected]:1234 AêñüC"அ

Output on localhost:1234/metrics: (See the unescaped backslash being printed) image

Please advice.

aarthipa avatar Jul 24 '18 10:07 aarthipa

Your code isn't passing a backslash, it's passing a double quote which is being correctly escaped.

brian-brazil avatar Jul 24 '18 10:07 brian-brazil

I was testing backsslash, double quote and I marked this incorrectly

aarthipa avatar Jul 24 '18 10:07 aarthipa

In any case, the code is parsing it correctly. image

Apologies for the scare.

aarthipa avatar Jul 24 '18 10:07 aarthipa