Are Dropwizard Counter and Meter count the same?

4.8k Views Asked by At

I would like to know whether there is a difference between the "Counter" and the count maintained by the "Meter" class? I understand the Meter measures rates too, but I am curious to know whether if there were a counter and a meter updated(incremented for Counter) at the same time, I would think both numbers would be same. Am I wrong in this assumption?

2

There are 2 best solutions below

1
On BEST ANSWER

the Meter simply also keeps track of the count of mark events that you have on the meter. It does so in the same way a counter does, thus the Meter is just an object that holds the internals of the counter + the logic to measure the rates of the occurring events too.

Here is a code example:

public class MetricTest {
    public static void main(String[] args) {
        MetricRegistry r = new MetricRegistry();
        Counter counter = r.counter("counter");
        Meter meter = r.meter("meter");

        counter.inc();
        meter.mark();

        System.out.println(counter.getCount());
        System.out.println(meter.getCount());

        counter.inc(10);
        meter.mark(10);

        System.out.println(counter.getCount());
        System.out.println(meter.getCount());
    }
}

Which will print:

1
1
11
11

So yes, if the counter and meter are updated in the same way, they will have the same count. The meter uses the count additionally to calculate the mean rate (in addition to the 1/5/15 - minute one)

I hope that helps,

Artur

0
On

Counter can be decremented. Meter can not be decremented. So, when counter and meter are used together, the values of "counters" differ when the Counter value is decremented.