metrics
Metrics provides a powerful toolkit of ways to measure the behavior of critical components in your production environment.
dependency
<dependency>
<groupId>org.jooby</groupId>
<artifactId>jooby-metrics</artifactId>
<version>1.6.6</version>
</dependency>
usage
{
use(new Metrics()
.request()
.threadDump()
.ping()
.healthCheck("db", new DatabaseHealthCheck())
.metric("memory", new MemoryUsageGaugeSet())
.metric("threads", new ThreadStatesGaugeSet())
.metric("gc", new GarbageCollectorMetricSet())
.metric("fs", new FileDescriptorRatioGauge())
);
}
Let’s see what all these means.
metrics
Metrics are available at /sys/metrics
or /sys/metrics/:type
via:
use(new Metrics()
.metric("memory", new MemoryUsageGaugeSet())
.metric("threads", new ThreadStatesGaugeSet())
.metric("gc", new GarbageCollectorMetricSet())
.metric("fs", new FileDescriptorRatioGauge()));
The /:type
parameter is optional and let you filter metrics by type counters
, guages
, etc..
There is a name
filter too: /sys/metrics?name=memory
or /sys/metrics/guages?name=memory
. The name
parameter filter all the metrics where the name starts with the given name
.
health checks
Health checks are available at /sys/healthCheck
via:
use(new Metrics()
.healthCheck("db", new DatabaseHealthCheck()));
instrumented requests
Captures request information (like active requests or min/mean/max execution time) and a breakdown of the response codes being returned: InstrumentedHandler.
use(new Metrics()
.request());
thread dump
A thread dump is available at /sys/threadDump
via:
use(new Metrics()
.threadDump());
reporting
Reporters are appended via a callback API:
{
use(new Metrics()
.reporter(registry -> {
ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();
reporter.start(1, TimeUnit.MINUTES);
return reporter;
});
}
You can add all the reporters you want. Keep in mind you have to start them (if need it), but you don’t have to stop them as long they implements the Closeable interface.