MicroProfile: @Counted vs. @Gauge 📎
@Path("ping")
public class PingResource {
@GET
@Counted(monotonic = true)
public String ping() {
return "Enjoy MicroProfile and Java EE 8";
}
@Gauge(unit = "correctness")
public int answer() {
return 42;
}
}
According to the microprofile specification, the @Counted
annotation:
"Denotes a counter, which counts the invocations of the annotated object."with default unit:
MetricUnits.NONE
In contrary, the @Gauge
annotation:
"Denotes a gauge, which samples the value of the annotated object."no default, must be supplied by the user.
Typical output (curl -H"Accept: application/json" http://localhost:9080/metrics/application
):
{
"com.airhacks.ping.boundary.PingResource.answer":42,
"com.airhacks.ping.boundary.PingResource.ping":1
}
The @Counted
annotation automatically
counts the total invocations of the annotation method (monotonic=true)
, or the amount of parallel invoked methods at any time: (monotonic=false)
(ConcurrentGauge
will probably replace: (monotonic=false)
).
On the other hand, the @Gauge
exposes the return value of the annotated method as a metric.
@Gauge
annotations are used to expose metrics with dedicated methods. The values have to be provided by the developer (e.g. number of orders in the DB) and the method is going to be
invoked by the metrics infrastructure.
@Counted
and their invocation count is going
to be exposed as a metric.
See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.