D20 Technical Services


Custom Software Development and Cloud Experts


Dropwizard Metrics with Guice

It turns out that Dropwizard Metrics can be configured on Jersey endpoints almost effortlessly via the @Timed annotation, but applying them other methods/functions in a Dropwizard application takes a bit of additional work to set up. In case you are using the Guice DI framework, do the following:

  1. Add the Metrics Guice Support library to your project’s POM.

  2. Configure your injector setup as described in the project’s README. Basically you’ll need to hook the module in with a line like:

install(MetricsInstrumentationModule.builder().withMetricRegistry(env.metrics()).build());
  1. Add entries to your Dropwizard metrics configuration to filter as needed.
metrics:
  frequency: 5 second
  reporters:
    - type: log
      logger: metrics
      includes:
        - com.mypackage.MyClass1.*
        - com.mypackage.MyClass2.*
      useRegexFilters: true

This will cause metrics for the abovementioned classes to dump out to the logs every few seconds. Add @Timed, @Counted etc. to your code and watch the stats accumulate! You should see log output like:

INFO  [2018-06-25 01:00:40,255] metrics: type=TIMER, name=com.mypackage.MyClass1.myMethod.timer, count=0, min=0.0, max=0.0, mean=0.0, stddev=0.0, median=0.0, p75=0.0, p95=0.0, p98=0.0, p99=0.0, p999=0.0, mean_rate=0.0, m1=0.0, m5=0.0, m15=0.0, rate_unit=events/second, duration_unit=milliseconds