Performance: Metrics with Micrometer
This concept affects how efficiently application metrics are collected and reported, impacting monitoring overhead and response time.
MeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); Counter counter = registry.counter("requests"); for (int i = 0; i < 10000; i++) { counter.increment(); } // Metrics are scraped asynchronously by Prometheus
MeterRegistry registry = new SimpleMeterRegistry(); Counter counter = registry.counter("requests"); for (int i = 0; i < 10000; i++) { counter.increment(); }
| Pattern | CPU Usage | Memory Usage | Blocking | Verdict |
|---|---|---|---|---|
| SimpleMeterRegistry with synchronous increments | High CPU during increments | Low memory | Blocks threads on increment | [X] Bad |
| PrometheusMeterRegistry with async scraping | Low CPU spikes | Moderate memory for buffering | Non-blocking increments | [OK] Good |
Counter counter = Counter.builder("requests.count").register(meterRegistry);
counter.increment();
counter.increment(2);
What will be the value of the counter after these calls?Counter counter = Counter.builder("api.calls").register(meterRegistry);
counter.increment(-1);
What is the issue with this code?processData()?Pass the test and this pattern moves unknown