问题
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