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. For further details on both please see
metrics()
and healthChecks()
respectively. By default metric collection is not enabled but health checks are.
It is important that this module is registered first in the modules list to ensure that request metrics are as accurate as possible.
Constructor and Description |
---|
CodaHaleMetricsModule() |
Modifier and Type | Method and Description |
---|---|
protected void |
configure() |
CodaHaleMetricsModule |
console()
Enable the reporting of metrics to the Console.
|
CodaHaleMetricsModule |
console(boolean enabled)
Enables or disables 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 |
healthChecks(boolean enabled)
Enables or disables the automatic registering of health checks.
|
CodaHaleMetricsModule |
jmx()
Enable the reporting of metrics via JMX.
|
CodaHaleMetricsModule |
jmx(boolean enabled)
Enables or disables the reporting of metrics via JMX.
|
CodaHaleMetricsModule |
jvmMetrics()
Enable the collection of JVM metrics.
|
CodaHaleMetricsModule |
jvmMetrics(boolean enabled)
Enables or disables the collecting of JVM metrics.
|
CodaHaleMetricsModule |
metrics()
Enables the collection of metrics.
|
CodaHaleMetricsModule |
metrics(boolean enabled)
Enables or disables the collecting of metrics.
|
CodaHaleMetricsModule |
websocket() |
CodaHaleMetricsModule |
websocket(boolean enabled) |
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindScope, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBinding
protected void configure()
configure
in class AbstractModule
public CodaHaleMetricsModule metrics()
To enable one of the built in metric reporters please chain the relevant reporter configuration
e.g. jmx()
, console()
.
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.
CodaHaleMetricsModule
jmx()
,
console()
,
csv(java.io.File)
public CodaHaleMetricsModule metrics(boolean enabled)
enabled
- If the metric collection should be enabled.CodaHaleMetricsModule
metrics()
public CodaHaleMetricsModule healthChecks()
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. When
health checks are enabled then all bound classes of type NamedHealthCheck will automatically be registered with the
HealthCheckRegistry
.
Health checks can be run using HealthCheckRegistry.runHealthChecks()
or by using the provided HealthCheckEndpoint
. Using HealthCheckEndpoint provides PathHandler
handlers with paths /health-check and /health-check/:name that can be inserted anywhere in the handler chain.
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.HealthCheckEndpoint 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() bind FooHealthCheck // if you don't bind the health check with Guice it will not be automatically registered } handlers { // Use the default health check handler to run health checks. /health-check will run all health checks and /health-check/:name will run individual checks handler(registry.get(HealthCheckEndpoint)) // Use the default health check handler wrapped in a Prefix to run health checks. Health checks can be run using /admin/health-check and /admin/health-check/:name prefix("admin") { handler(registry.get(HealthCheckEndpoint)) } // Use your own handlers to run health checks get("healthChecks") { HealthCheckRegistry healthCheckRegistry -> render healthCheckRegistry.runHealthChecks().toString() } get("healthCheck/:name") { HealthCheckRegistry healthCheckRegistry -> render healthCheckRegistry.runHealthCheck(pathTokens.get("name")).toString() } } }
CodaHaleMetricsModule
HealthCheckEndpoint
,
HealthCheckRegistry.runHealthChecks()
,
HealthCheckRegistry.runHealthCheck(String)
,
HealthCheckRegistry.runHealthChecks(java.util.concurrent.ExecutorService)
public CodaHaleMetricsModule healthChecks(boolean enabled)
enabled
- If the automatic registering of health checks should be enabled.CodaHaleMetricsModule
healthChecks()
public CodaHaleMetricsModule jvmMetrics()
The JVM Gauges and Metric Sets provided by Coda Hale's Metrics will be registered to this module's Metric Registry.
CodaHaleMetricsModule
public CodaHaleMetricsModule jvmMetrics(boolean enabled)
enabled
- If JVM metric collection should be enabled.CodaHaleMetricsModule
jvmMetrics()
public CodaHaleMetricsModule websocket()
public CodaHaleMetricsModule websocket(boolean enabled)
public CodaHaleMetricsModule jmx()
CodaHaleMetricsModule
console()
,
csv(java.io.File)
public CodaHaleMetricsModule jmx(boolean enabled)
enabled
- If JMX metric reporting should be enabled.CodaHaleMetricsModule
jmx()
public CodaHaleMetricsModule console()
CodaHaleMetricsModule
jmx()
,
csv(java.io.File)
public CodaHaleMetricsModule console(boolean enabled)
enabled
- If Console metric reporting should be enabled.CodaHaleMetricsModule
console()
public CodaHaleMetricsModule csv(File reportDirectory)
reportDirectory
- The directory in which to create the CSV report files.CodaHaleMetricsModule
jmx()
,
console()
public Handler decorate(Injector injector, Handler handler)
HandlerDecoratingModule
decorate
in interface HandlerDecoratingModule
injector
- The injector created from all the application moduleshandler
- The application handler