-1

Quick question regarding how to build a visual on a specific condition of a java counter in Grafana please.

Currently, I have a small piece of java code, straightforward.

  private String question(MeterRegistry meterRegistry) {
        if (someCondition()) {
            Counter.builder("theCounter").tags("GOOD", "GOOD").register(meterRegistry).increment();
            return "good";
        } else {
            LOGGER.warn("it is failing, we should increment failure");
            Counter.builder("theCounter").tags("FAIL", "FAIL").register(meterRegistry).increment();
            return "fail";
        }
    }

As you can see, it is very simple, just a "if a condition is met, increment the GOOD counter, if not, increment the FAIL counter"

I am interested in building a dashboard for the failures only.

When I query my /prometheus endpoint I successfully see:

myCounter_total{FAIL="FAIL",} 7.0
myCounter_total{GOOD="GOOD",} 3.0

Hence, I started using this query.

myCounter_total{_ws_="workspace",_ns_="namespace",_source_="source}

Unfortunately, this query is giving me the visual for everything, the GOOD and the FAIL. In my example, I see all 10 counters, while I just want to see the 7 failures.

I tried putting

myCounter_total{FAIL="FAIL",_ws_="workspace",_ns_="namespace",_source_="source}

{{FAIL}}

But no luck. May I ask what did I miss please?

PatPanda
  • 3,644
  • 9
  • 58
  • 154

1 Answers1

0

Create only one counter for this case and give it a label named status, for example. Now, depending on whether the good or the fail condition occurs, increment your counter with a string "GOOD" or "FAIL" as a value for the status label, for example like in this pseudo code:

Counter myCounter =
    Counter.build().name("myCounter").help("This is my counter").labelNames("status").register();


if (someCondition()) {
    myCounter.labels("GOOD").increment();
} else {
    myCounter.labels("FAIL").increment();
}

Now you should be able see a query output like this:

myCounter_total{status="FAIL"} 7.0
myCounter_total{status="GOOD"} 3.0

For visualization purposes, if you'd like to see two graphs, one for good and one for bad cases, you could use something like below. This one query querying myCounter_total reveals its data and the Legend takes care of separating values/graphs with different status label values.

Query: myCounter_total

Legend: {{status}}

nezzzzzz
  • 86
  • 1
  • 1
  • 4
  • Thanks For the answer! I am currently using ```Counter``` from ```io.micrometer.core.instrument.Counter``` from ```micrometer-core-1.7.1.jar```. It differs a bit from your sample. There is no usage of ```.labelNames("status")``` ```.labels("GOOD")``` – PatPanda Jul 14 '21 at 03:28