2

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?

matt freake
  • 4,877
  • 4
  • 27
  • 56
  • by default the servlet is listing any metrics that is in the DW metrics registry. Are you registering your metrics with the right registry? – pandaadb May 31 '17 at 11:22
  • Thanks @pandaadb I've added the code where I get the registry and timer. It doesn't look right to me, but can't see how you specifically register it with the MetricsServlet – matt freake May 31 '17 at 12:04
  • 1
    That is your problem matt. The metric registry you use is not the one DW provides. In your startup code, you have an Environment that already provides the metric registry. This ones is used by DW to display the metrics on that servlet – pandaadb May 31 '17 at 12:08
  • 1
    Thanks very much. Not sure how I missed that Environment provided that. If you want to put that as an answer I can accept it – matt freake May 31 '17 at 17:32

1 Answers1

3

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

pandaadb
  • 6,306
  • 2
  • 22
  • 41