@Counted declares a Micrometer counter for a method on a CDI bean. Quarkus registers the counter and attaches an interceptor that increments it whenever the method is called, making business metrics less verbose than programmatic instrumentation. section 10.3.4
A counted method provides:
- A named counter, such as orders, created automatically when the application first uses it. section 10.3.4
- Additional series labeled with the declaring class, method, thrown exception, and success or failure result. section 10.3.6
- Lazy availability: invoke the owning bean method at least once before expecting the counter at the metrics endpoint. section 10.3.6
Counting order placements
Annotate a CDI bean method with @Counted(name = "orders", description = "Number of orders placed"). Each invocation contributes one to the orders counter. The equivalent imperative approach injects MeterRegistry and calls meterRegistry.counter("orders").increment(), which remains more flexible for advanced use cases. section 10.3.4
@Counted(name = "orders", description = "Number of orders placed")
public void orderPlaced(OrderDetais details) {
// order processing logic
}
Use the annotation directly on the business operation that should produce a counter. section 10.3.4
Expose the resulting metrics through the Quarkus metrics endpoint after invoking the method. Prometheus and Grafana can then display the counter and its dimensions. section 10.3.6
