public class Metrics extends Object implements Jooby.Module
Metrics provides a powerful toolkit of ways to measure the behavior of critical components in your production environment.
{
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 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 are available at /sys/healthCheck via:
use(new Metrics()
.healthCheck("db", new DatabaseHealthCheck()));
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());
A thread dump is available at /sys/threadDump via:
use(new Metrics()
.threadDump());
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.
That's all folks!
| Constructor and Description |
|---|
Metrics()
Creates a new
Metric module. |
Metrics(com.codahale.metrics.MetricRegistry registry)
Creates a new
Metric module. |
Metrics(com.codahale.metrics.MetricRegistry registry,
String pattern)
Creates a new
Metric module. |
Metrics(String pattern)
Creates a new
Metric module. |
| Modifier and Type | Method and Description |
|---|---|
void |
configure(Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder) |
<H extends com.codahale.metrics.health.HealthCheck> |
healthCheck(String name,
Class<H> check)
Append a health check to the
HealthCheckRegistry. |
Metrics |
healthCheck(String name,
com.codahale.metrics.health.HealthCheck check)
Append a health check to the
HealthCheckRegistry. |
<M extends com.codahale.metrics.Metric> |
metric(String name,
Class<M> metric)
Append a metric to the
MetricRegistry. |
Metrics |
metric(String name,
com.codahale.metrics.Metric metric)
Append a metric to the
MetricRegistry, this call is identical to
MetricRegistry.register(String, Metric). |
Metrics |
ping()
Append a simple ping handler that results in a
200 responses with a
pong body. |
Metrics |
reporter(BiFunction<com.codahale.metrics.MetricRegistry,com.typesafe.config.Config,com.codahale.metrics.Reporter> callback)
Append a
Reporter to the MetricRegistry. |
Metrics |
reporter(Function<com.codahale.metrics.MetricRegistry,com.codahale.metrics.Reporter> callback)
Append a
Reporter to the MetricRegistry. |
Metrics |
request()
Instrument request using
InstrumentedHandler. |
Metrics |
request(String pattern)
Instrument request using
InstrumentedHandler. |
Metrics |
request(String method,
String pattern)
Instrument request using
InstrumentedHandler. |
Metrics |
threadDump()
Append a handler that prints thread states (a.k.a thread dump).
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitconfigpublic Metrics(com.codahale.metrics.MetricRegistry registry,
String pattern)
Metric module.registry - Use the given registry.pattern - A root pattern where to publish all the services. Default is: /sys.public Metrics(String pattern)
Metric module.pattern - A root pattern where to publish all the services. Default is: /sys.public Metrics(com.codahale.metrics.MetricRegistry registry)
Metric module. Services will be available at: /sys.registry - Use the given registry.public Metrics()
Metric module. Services will be available at: /sys.public Metrics request(String method, String pattern)
InstrumentedHandler.method - Method to filter for. Default is: GET.pattern - A pattern to filter for. Default is: * (all the requests).public Metrics request(String pattern)
InstrumentedHandler.pattern - A pattern to filter for. Default is: * (all the requests).public Metrics request()
InstrumentedHandler. It will intercept all the
GET calls.public Metrics ping()
public Metrics threadDump()
ThreadDumpHandler.public Metrics metric(String name, com.codahale.metrics.Metric metric)
MetricRegistry, this call is identical to
MetricRegistry.register(String, Metric).name - Name of the metric.metric - A metric objectpublic <M extends com.codahale.metrics.Metric> Metrics metric(String name, Class<M> metric)
MetricRegistry. The metric will be resolved by Guice. This call
is identical to MetricRegistry.register(String, Metric).M - Metric type.name - Name of the metric.metric - A metric object.public Metrics healthCheck(String name, com.codahale.metrics.health.HealthCheck check)
HealthCheckRegistry. This call is identical to
HealthCheckRegistry.register(String, HealthCheck).name - Name of the check.check - A check object.public <H extends com.codahale.metrics.health.HealthCheck> Metrics healthCheck(String name, Class<H> check)
HealthCheckRegistry. The metric will be resolved by Guice.
This call is identical to HealthCheckRegistry.register(String, HealthCheck).H - HealthCheck type.name - Name of the check.check - A check object.public Metrics reporter(BiFunction<com.codahale.metrics.MetricRegistry,com.typesafe.config.Config,com.codahale.metrics.Reporter> callback)
Reporter to the MetricRegistry.callback - Reporter callback.public Metrics reporter(Function<com.codahale.metrics.MetricRegistry,com.codahale.metrics.Reporter> callback)
Reporter to the MetricRegistry.callback - Reporter callback.public void configure(Env env, com.typesafe.config.Config conf, com.google.inject.Binder binder)
configure in interface Jooby.ModuleCopyright © 2016. All rights reserved.