MeterRegistry is Micrometer’s global registry for application metrics. It manages the meters currently present and supports dynamically adding or removing them. section 10.3.4
Use it for programmatic instrumentation when annotations are insufficient or when metric creation needs more control. Calling counter("orders") uses get-or-create behavior: it creates the counter if necessary and returns it for updates. section 10.3.4
A typical imperative workflow is:
- Inject MeterRegistry into the CDI bean that performs the measured operation. section 10.3.4
- Obtain a counter with meterRegistry.counter("orders") and increment it after the relevant operation. section 10.3.4
- Build the metric explicitly with Counter.builder(...) when a description or other metadata is required. section 10.3.4
Counting orders programmatically
@Inject MeterRegistry meterRegistry; public void orderPlaced(OrderDetails details) { // order processing logic meterRegistry.counter("orders").increment(); }
Register explicitly configured meters once, before updates begin, such as from a @PostConstruct method in a @Singleton bean. Annotation-based @Counted instrumentation remains shorter for straightforward method invocation counters. section 10.3.4 section 10.3.6
