IsaacLevon
IsaacLevon

Reputation: 2580

Why does MicroMeter Timer returns zero?

Consider the following code:

public static void main(String[] args) {
    Timer timer = Metrics.timer("item.processing");
    for (int i = 0; i < 100; i++) {
        timer.record(i, TimeUnit.SECONDS);
    }

    System.out.println(timer.count());
    System.out.println(timer.mean(TimeUnit.SECONDS));
}

The output is zero for both prints, but of course it is expected to be a positive number.

I'm using the globalRegistry but I don't think it should make a difference.

Upvotes: 0

Views: 3286

Answers (1)

CryptoFool
CryptoFool

Reputation: 23129

The reason you are seeing the results you're seeing is that you haven't registered a concrete Registry implementation. The default setup of Micrometer does not have a backing Registry implementation defined. Because of this, your Timer values are just being dropped on the floor and not retained.

Add this line as the first line of main(), and you'll start getting the behavior you expect:

Metrics.addRegistry(new SimpleMeterRegistry());

like so:

public static void main(String ...args) {

    Metrics.addRegistry(new SimpleMeterRegistry());

    Timer timer = Metrics.timer("item.processing");
    for (int i = 0; i < 100; i++) {
        timer.record(i, TimeUnit.SECONDS);
    }

    System.out.println(timer.count());
    System.out.println(timer.mean(TimeUnit.SECONDS));
}

which gives results:

100
49.5

Upvotes: 7

Related Questions