Why does MicroMeter Timer returns zero?

非 Y 不嫁゛ 提交于 2021-02-11 12:13:51

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/55207793/why-does-micrometer-timer-returns-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!