Class DropwizardMetricsModule

  • All Implemented Interfaces:
    com.google.inject.Module

    public class DropwizardMetricsModule
    extends ConfigurableModule<DropwizardMetricsConfig>
    An extension module that provides support for Dropwizard Metrics.

    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 DropwizardMetricsConfig.jmx(Action) one would write: (Groovy DSL)

     import ratpack.dropwizard.metrics.DropwizardMetricsModule
     import static ratpack.groovy.Groovy.ratpack
    
     ratpack {
       bindings {
         module new DropwizardMetricsModule(), { it.jmx() }
       }
     }
     

    To enable the capturing and reporting of metrics to JMX and the console, one would write: (Groovy DSL)

     import ratpack.dropwizard.metrics.DropwizardMetricsModule
     import static ratpack.groovy.Groovy.ratpack
    
     ratpack {
       bindings {
         module new DropwizardMetricsModule(), { it.jmx().console() }
       }
     }
     

    External Configuration

    The module can also be configured via external configuration using the ratpack-config extension. For example, to enable the capturing and reporting of metrics to jmx via an external property file which can be overridden with system properties one would write: (Groovy DSL)

     import com.google.common.collect.ImmutableMap
     import ratpack.dropwizard.metrics.DropwizardMetricsModule
     import ratpack.dropwizard.metrics.DropwizardMetricsConfig
     import ratpack.config.ConfigData
     import static ratpack.groovy.Groovy.ratpack
    
     ratpack {
       serverConfig {
         props(ImmutableMap.of("metrics.jmx.enabled", "true")) // for demo purposes we are using a map to easily see the properties being set
         sysProps()
         require("/metrics", DropwizardMetricsConfig)
       }
    
       bindings {
         module DropwizardMetricsModule
       }
     }
     

    Metric Collection

    By default Timer metrics are collected for all requests received and Counter metrics for response codes. The module adds a default 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.

    The module also adds a default BlockingExecTimingInterceptor to the execution path. This will add timers that will account for time spent on blocking io calls.

    Both the request timing handler and the blocking execution timing interceptor can be disabled:

    
     import ratpack.dropwizard.metrics.DropwizardMetricsModule
     import static ratpack.groovy.Groovy.ratpack
    
     ratpack {
       bindings {
         module new DropwizardMetricsModule(), { it.requestTimingMetrics(false).blockingTimingMetrics(false) }
       }
    
       handlers {
         all {
           render ""
         }
       }
     }
     

    Additional custom metrics can be registered with the provided MetricRegistry instance

    Example custom metrics: (Groovy DSL)

    
     import ratpack.dropwizard.metrics.DropwizardMetricsModule
     import com.codahale.metrics.MetricRegistry
     import static ratpack.groovy.Groovy.ratpack
    
     ratpack {
       bindings {
         module new DropwizardMetricsModule(), { it.jmx() }
       }
    
       handlers { MetricRegistry metricRegistry ->
         all {
           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.

    See Also:
    Dropwizard Metrics
    • Constructor Detail

      • DropwizardMetricsModule

        public DropwizardMetricsModule()
    • Method Detail

      • configure

        protected void configure()
        Overrides:
        configure in class com.google.inject.AbstractModule