liveBook · Manning

@Counted

As explained in Quarkus in Action

@Counted is a Micrometer annotation that declares counting of invocations of an annotated method.

@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

How Quarkus in Action covers it

How deep it goes
Explains annotation interception, lazy registration, and metric dimensions, while deferring detailed manual metadata registration.
Examples
Uses order placement and car registration methods, metrics output, GraphQL invocation, and Grafana dashboards.
What it assumes
Assumes a Quarkus CDI bean and metrics setup, while distinguishing declarative annotation use from MeterRegistry instrumentation.
What you can do after
Apply it via Micrometer and Quarkus to count method calls and inspect labeled series through the metrics endpoint.
How it connects
Connects Micrometer, Quarkus, CDI beans, MeterRegistry, Prometheus, and Grafana.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quarkus in Action. The section links point to the parts of the book it draws on.