liveBook · Manning

Counter

As explained in Quarkus in Action

A counter is a metric that records a value intended to increase monotonically as occurrences are counted.

A counter records how often an event occurs. It is a single-value metric that can only be incremented, making it suitable for monotonically increasing totals such as placed orders or registered cars. section 10.3.2

In Quarkus with Micrometer, counters support business metrics alongside framework metrics. Create them declaratively with @Counted or programmatically through MeterRegistry; annotations are shorter, while imperative code provides more flexibility. section 10.3.4

Choose an implementation approach:

  • Annotate a CDI bean method with @Counted(name = "orders"). An interceptor registers the counter and increments it by one whenever the method is called. section 10.3.4
  • Inject MeterRegistry and call meterRegistry.counter("orders").increment() when the event occurs. The counter method creates the named counter if necessary and returns it. section 10.3.4
  • Use Counter.builder(...) when the metric needs a description or other metadata, and register it once before updates begin, such as during @PostConstruct initialization. section 10.3.4
@Counted(name = "orders", description = "Number of orders placed")
public void orderPlaced(OrderDetais details) {
    // order processing logic
}

The annotation form connects a method call directly to an incrementing orders counter. section 10.3.4

After enabling the Prometheus registry, inspect counters through /q/metrics. Prometheus output identifies metric types with # TYPE metadata, while Grafana can visualize stored metrics in dashboards. section 10.3.6

How Quarkus in Action covers it

How deep it goes
Defines counter semantics and demonstrates annotation, registry, metadata, exposure, and visualization, without detailing storage internals.
Examples
Uses orders and car registrations, @Counted methods, MeterRegistry calls, /q/metrics output, and Grafana dashboards.
What it assumes
Assumes Quarkus and Micrometer application context while introducing counter types, CDI annotations, registries, and metric endpoints.
What you can do after
Build it from scratch with @Counted or MeterRegistry, then expose and inspect it through Prometheus-compatible metrics.
How it connects
Connects counters with Micrometer, Quarkus, CDI, MeterRegistry, Prometheus, and Grafana.

Also explained in

Compare how 4 books explain counter →

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.