Converts a code block to a com.codahale.metrics.health.HealthCheck and registers it.
Converts a code block to a com.codahale.metrics.health.HealthCheck and registers it.
Use it as follows:
object Application { // The application wide health check registry. val healthCheckRegistry = new com.codahale.metrics.health.HealthCheckRegistry() } trait Checked extends CheckedBuilder { val registry = Application.healthCheckRegistry } class ExampleWorker extends Checked { healthCheck("alive") { workerThreadIsActive() } }
The code block must have a result of type Boolean
, Try
, Either
, Future
,
com.codahale.metrics.health.HealthCheck.Result, or simply Unit
.
true
indicates healthy, false
indicates unhealthy..toString
) or throwable is used as (un)healthy message.[Any]
or Left[Throwable]
indicates
unhealthy. The embedded value (after applying .toString
) or throwable is used as (un)healthy message.It is also possible to override the health check base name. For example:
class ExampleWorker extends Checked { override lazy val metricBaseName = MetricName("Overridden.Base.Name") healthCheck("alive") { workerThreadIsActive() } }
To change the timeout for Future execution, set an implicit duration:
class ExampleWorker extends Checked { implicit private val timeout = 10.seconds healthCheck("alive")(Future { workerThreadIsActive() }) } NOTE: only one health check can be registered under a name (including the base name which is derived from the class name by default). Any subsequent health check registrations will be ignored. This happens for example when a class that defines a health check is instantiated multiple times. This metrics-core behavior might be changed with https://github.com/dropwizard/metrics/issues/1245. @param name the name of the health check @param unhealthyMessage the unhealthy message for checkers that return `false`, defaults to `"Health check failed"` @param checker the code block that does the health check
The base name for all metrics created from this builder.
The base name for all metrics created from this builder.
The MetricRegistry where created metrics are registered.
The MetricRegistry where created metrics are registered.
The MetricBuilder that can be used for creating timers, counters, etc.
The MetricBuilder that can be used for creating timers, counters, etc.
The com.codahale.metrics.health.HealthCheckRegistry where created metrics are registered.
The com.codahale.metrics.health.HealthCheckRegistry where created metrics are registered.
A mixin trait for creating a class that publishes metrics and health checks to the "default" registries.
This follows the Dropwizard 1.0.0+ application convention of storing the metric registry to SharedMetricRegistries under the name
"default"
. This was extended with storing the health check registry to SharedHealthCheckRegistries under the same name.After mixing in this trait, metrics and health checks can be defined. For example:
See InstrumentedBuilder for instruction on overriding the metric base name or using hdrhistograms. See CheckedBuilder for instructions on overriding the timeout for scala.concurrent.Future executions.