Labels with client_java

21 views
Skip to first unread message

William Walsh

unread,
Feb 15, 2022, 7:23:39 AM2/15/22
to Prometheus Developers

I have coded a Prometheus client_java collector/exporter for a proprietary server that counts the number of its jobs and observes elapsed times and data lengths. This works fine. But, the requirement now is to expand on that, adding labels to the count of jobs; notionally:

type="PDF|HTML|TEXT|RTF"
status="fail|success"

Unfortunately, the labels example in https://github.com/prometheus/client_java#labels isn't enough for me to bridge my lack of Prometheus understanding. Is that example indicating that by coding labelNames("method") in the Counter declaration and then calling requests.labels("get").inc() that the get method count is being incremented? If so, that sounds like Prometheus dynamically use that method label to store the count for get or post or any other method name passed, is that correct?

In my experience, as soon as I add labelNames to a Counter declaration like this:

private static final Counter jobsSuccessful = Counter.build().namespace("curam_xmlserver").name("job_successes").help("Successful xmlserver jobs").labelNames("type").register();

it breaks the Counter. That is, without labelNames coded I get this output (no activity):

curl -s http://localhost:8080/metrics | grep job_successes
# HELP curam_xmlserver_job_successes_total Successful xmlserver jobs
# TYPE curam_xmlserver_job_successes_total counter
curam_xmlserver_job_successes_total 0.0

And after adding labelNames there is no longer the zero counter (curam_xmlserver_job_successes_total 0.0) and the collector doesn't count or observe anything.

Is this a bug that I should create an issue for, or what am I doing wrong, please? This is with release 0.15.0.

Thank you,
www

William Walsh

unread,
Feb 24, 2022, 9:37:09 AM2/24/22
to Prometheus Developers
Example of using multiple labels with client_java:
// Declaration for job counts with labels for status=success|fail and type=PDF|HTML|TEXT|RTF"
private static final Counter jobTotals = Counter.build() ... labelNames("status", "type") ... ;

// statsData contains the data
String [] statsData = statsLine.split("\\t");

if (statsData[0].contains("true")) {
  jobTotals.labels("success", statsData[1]).inc();
} else {
  // When the job fails there is no valid job type
  jobTotals.labels("fail", "").inc();
}
Reply all
Reply to author
Forward
0 new messages