public class CodaHaleMetricsModule extends AbstractModule implements HandlerDecoratingModule
To use it one has to register the module and enable the required functionality by chaining the various configuration
options. For example, to enable the capturing and reporting of metrics to jmx()
one would write: (Groovy DSL)
import ratpack.codahale.metrics.CodaHaleMetricsModule
import static ratpack.groovy.Groovy.ratpack
ratpack {
modules {
register new CodaHaleMetricsModule().jmx()
}
}
To enable the capturing and reporting of metrics to JMX and the console(), one would
write: (Groovy DSL)
import ratpack.codahale.metrics.CodaHaleMetricsModule
import static ratpack.groovy.Groovy.ratpack
ratpack {
modules {
register new CodaHaleMetricsModule().jmx().console()
}
}
This module supports both metric collection and health checks.
By default Timer metrics are collected for all requests received. The module adds a
RequestTimingHandler to the handler chain before any user handlers. This means that response times do not take any
framework overhead into account and purely the amount of time spent in handlers. It is important that the module is
registered first in the modules list to ensure that all handlers are included in the metric.
Additional custom metrics can be registered with the provided MetricRegistry instance
Example custom metrics: (Groovy DSL)
import ratpack.codahale.metrics.CodaHaleMetricsModule
import com.codahale.metrics.MetricRegistry
import static ratpack.groovy.Groovy.ratpack
ratpack {
modules {
register new CodaHaleMetricsModule().jmx()
}
handlers { MetricRegistry metricRegistry ->
handler {
metricRegistry.meter("my custom meter").mark()
render ""
}
}
}
Custom metrics can also be added via the Metrics annotations (Metered, Timed and Gauge)
to any Guice injected classes.
Health checks verify that application components or responsibilities are performing as expected.
To create a health check simply create a class that extends NamedHealthCheck and bind it with Guice.
This will automatically add it to the application wide HealthCheckRegistry.
Health checks can be run by obtaining the HealthCheckRegistry via dependency injection or context registry lookup,
then calling HealthCheckRegistry.runHealthChecks().
To expose the health check status over HTTP, see HealthCheckHandler.
Example health checks: (Groovy DSL)
import com.codahale.metrics.health.HealthCheck
import com.codahale.metrics.health.HealthCheckRegistry
import ratpack.codahale.metrics.CodaHaleMetricsModule
import ratpack.codahale.metrics.HealthCheckHandler
import ratpack.codahale.metrics.NamedHealthCheck
import static ratpack.groovy.Groovy.ratpack
class FooHealthCheck extends NamedHealthCheck {
protected HealthCheck.Result check() throws Exception {
// perform the health check logic here and return HealthCheck.Result.healthy() or HealthCheck.Result.unhealthy("Unhealthy message")
HealthCheck.Result.healthy()
}
def String getName() {
"foo_health_check"
}
}
ratpack {
modules {
register new CodaHaleMetricsModule().healthChecks()
bind FooHealthCheck // if you don't bind the health check with Guice it will not be automatically registered
}
handlers {
// Using the provided handler…
get("health-check/:name", new HealthCheckHandler())
// Using a custom handler to run all health checks…
get("healthChecks-custom") { HealthCheckRegistry healthCheckRegistry ->
render healthCheckRegistry.runHealthChecks().toString()
}
}
}
| Constructor and Description |
|---|
CodaHaleMetricsModule() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
configure() |
CodaHaleMetricsModule |
console()
Enable the reporting of metrics to the Console.
|
CodaHaleMetricsModule |
csv(File reportDirectory)
Enable the reporting of metrics to a CSV file.
|
Handler |
decorate(Injector injector,
Handler handler)
Decorate the given handler with any global logic.
|
CodaHaleMetricsModule |
healthChecks()
Enables the automatic registering of health checks.
|
CodaHaleMetricsModule |
jmx()
Enable the reporting of metrics via JMX.
|
CodaHaleMetricsModule |
jvmMetrics()
Enable the collection of JVM metrics.
|
CodaHaleMetricsModule |
metrics()
Enables the collection of metrics.
|
CodaHaleMetricsModule |
websocket()
Enable the reporting of metrics via web sockets.
|
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindScope, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBindingprotected void configure()
configure in class AbstractModulepublic CodaHaleMetricsModule metrics()
CodaHaleMetricsModulejmx(),
console(),
csv(java.io.File),
websocket()public CodaHaleMetricsModule healthChecks()
CodaHaleMetricsModuleHealthCheckHandler,
HealthCheckRegistry.runHealthChecks(),
HealthCheckRegistry.runHealthCheck(String),
HealthCheckRegistry.runHealthChecks(java.util.concurrent.ExecutorService)public CodaHaleMetricsModule jvmMetrics()
The JVM Gauges and Metric Sets provided by Coda Hale's Metrics will be registered to this module's Metric Registry.
CodaHaleMetricsModulepublic CodaHaleMetricsModule websocket()
To broadcast metrics within an application see MetricsWebsocketBroadcastHandler.
CodaHaleMetricsModuleconsole(),
csv(java.io.File),
jmx()public CodaHaleMetricsModule jmx()
CodaHaleMetricsModuleconsole(),
csv(java.io.File),
websocket()public CodaHaleMetricsModule console()
CodaHaleMetricsModulejmx(),
csv(java.io.File),
websocket()public CodaHaleMetricsModule csv(File reportDirectory)
reportDirectory - The directory in which to create the CSV report files.CodaHaleMetricsModulejmx(),
console(),
websocket()public Handler decorate(Injector injector, Handler handler)
HandlerDecoratingModuledecorate in interface HandlerDecoratingModuleinjector - The injector created from all the application moduleshandler - The application handler