Search code examples
javadropwizardcodahale-metrics

How Do I Add My Custom Metrics To Dropwizard /metrics


I am using the Dropwizard Metrics library to record the times various actions take in my app, using a Timer and the the ConsoleReporter and that records count, mean rate, etc to the console fine.

I would like these figures to also be available on the /metrics servlet and based on http://metrics.dropwizard.io/3.1.0/manual/servlets/ I need to access the MetricRegistry called com.codahale.metrics.servlets.MetricsServlet.registry. But looking at the docs and code, I can't see how that is done. My existing /metrics only outputs Timers for dropwizard and jetty classes

[EDIT]

private static final MetricRegistry metricRegistry = new MetricRegistry();
...
Timer timer = metricRegistry.timer(name("com.codahale.metrics.servlets.MetricsServlet.registry","testval"));

How do I connect my Timer to the output of /metrics?


Solution

  • Dropwizard displays all metrics on its metric endpoint by default, there is nothing you need to do apart from using Metrics.

    The only thing to notice is, that DW provides the metric registry to use for this. By defining your own, DW can't find the correct metrics. These can be found in the environment passed to your application in the run method:

        @Override
        public void run(Configuration configuration, Environment environment) throws Exception {
    
            MetricRegistry metrics = environment.metrics();
        }
    

    Thanks,

    Artur