Creating Domain-Specific Metrics On-The-Fly with MicroProfile 📎
Domain-specific counters can be created on-the-fly by injecting the MetricRegistry
:
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.annotation.RegistryType;
@Path("ping")
public class PingResource {
@Inject
@RegistryType(type = MetricRegistry.Type.APPLICATION)
MetricRegistry registry;
@GET
public String ping() {
registry.counter("pingCount").inc();
return "Enjoy Java EE 8 with MicroProfile ThinWARs!";
}
}
curl http://localhost:9080/metrics/application
yields:
# TYPE application:ping_count counter
application:ping_count 1
and
curl -H"Accept: application/json" http://localhost:9080/metrics/application
returns
{"pingCount":1}
The size of the WAR is 4kB. Initial build time: 3292 ms, subsequent build times: < 3s. Deploy time: 0.233 seconds.
Built and deployed with WAD and tested with openliberty (the largest possible installation with all Java EE 8 + MicroProfile features enabled).
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.