Frage

How do I go about adding custom metrics to the default stuff that Dropwizard provides on the admin port (8081)? I can't find anything in the documentation apart from adding health checks. I'd quite like to incorporate some stats retrieved from MongoDB, and would rather keep it with the admin resources on 8081 than create a custom metrics page on port 8080.

War es hilfreich?

Lösung

Dropwizard's using the Metrics library for all of its metrics. Metric's getting started section has everything you need to start adding your own.

Andere Tipps

Here is an example. Every metric in that instance of the jvm is exposed via JMX. You can also register metric reporters that will do thing liks dump all metrics to logs on interval, or ship to graphite on an interval.

    //this creates or returns the metrics, basically every metric is only created once and registered in a registry
    private final Timer timerCanMakeHold = 
                Metrics.newTimer(MyClass.class, "METRICNAME", TimeUnit.MILLISECONDS, TimeUnit.SECONDS);

        final TimerContext timerContex = timerCanMakeHold.time();
        try{    
             doSomeWork()//this is what you are timing
        }finally{
            timerContex.stop();
        }

If you want your metrics to show up with the metrics servlet that's included in your Dropwizard project, you must use the same MetricRegistry object that the servlet used and register your metrics into it.

You can get the correct MetricRegistry instance from the Environment in your application; or from the Bootstrap object that's passed to its initilaize method during startup.

The Dropwizard documentation fails to mention the scope of the MetricRegistry and the objects it contains. It implies that you just create your own MetricRegistry. That'll work fine for the stand alone "getting started" application, but the document is about adding metrics to an existing Dropwizard application, not a new, standalone application.

I haven't tested it, but perhaps this could help:

    final Graphite graphite = new Graphite(new InetSocketAddress("graphite.url.example", 2003));
    MetricRegistry metrics = new MetricRegistry();
    GraphiteReporter reporter = GraphiteReporter.forRegistry(metrics)
                      .convertRatesTo(TimeUnit.SECONDS)
                      .convertDurationsTo(TimeUnit.MILLISECONDS)
                      .build(graphite);
    reporter.start(1, TimeUnit.SECONDS);
    Counter counter = metrics.counter("nameOfCounter");
    counter.inc();

For ivy you have to add this to your ivy.xml:

        <dependency org="io.dropwizard" name="dropwizard-metrics" rev="0.7.1"/>
        <dependency org="io.dropwizard" name="dropwizard-metrics-graphite" rev="0.7.1"/>

If you put this into your config.yml,

metrics:
  reporters:
    - type: console
      timeZone: UTC
      output: stdout
      durationUnit: milliseconds
      rateUnit: seconds
      frequency: 120 seconds
    - type: graphite
      host: localhost
      port: 9090
      prefix: test.prefix

you can also call the MetricsFactory in your run method:

        MetricRegistry metrics = new MetricRegistry();
        MetricsFactory mfac = configuration.getMetricsFactory();
        mfac.configure(environment.lifecycle(), metrics);
        Counter counter = metrics.counter("nameOfCounter");
        counter.inc();
        counter.inc();
        counter.inc();
        counter.inc();
        counter.inc();

If you build you own socket listener, then you can see this line incoming every xxx seconds:

test.prefix.nameOfCounter.count 5 1411562372
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top