Custom Metrics Reporters

1,466 views
Skip to first unread message

Phil Monroe

unread,
Apr 23, 2014, 8:16:17 PM4/23/14
to dropwiz...@googlegroups.com
Hey,

How would I create and register a new custom metrics reporter? I want to extend the current graphite reporter and have it inject the hostname into the prefix when sending metrics. Here is what I have:


MygraphiteReporterFactory.scala:
package io.dropwizard.metrics

import com.fasterxml.jackson.annotation.JsonTypeName
import io.dropwizard.metrics.graphite.GraphiteReporterFactory

@JsonTypeName("mygraphite")
class MygraphiteReporterFactory extends GraphiteReporterFactory {
  override def getPrefix() = s"${super.getPrefix}.HOST"
}



config.yml:
metrics:
  reporters:
    - type: mygraphite
      host: graphite.example.com
      prefix: syman2


error on startup:
config.yml has an error:
  * Failed to parse configuration at: metrics.reporters; Could not resolve type id 'mygraphite' into a subtype of [simple type, class io.dropwizard.metrics.ReporterFactory]
 at [Source: N/A; line: -1, column: -1] (through reference chain: com.example.Config["metrics"]->io.dropwizard.metrics.MetricsFactory["reporters"])


What am I missing?

Thanks!
Phil

Phil Monroe

unread,
Apr 23, 2014, 9:00:45 PM4/23/14
to dropwiz...@googlegroups.com
Ah ha! Figured it out.

Dropwizard uses text files inside of `src/main/resources/META-INF/services/*` to figure out what classes are discoverable. 

So i just needed to add this to `src/main/resources/META-INF/services/io.dropwizard.metrics.ReporterFactory`
io.dropwizard.metrics.MygraphiteReporterFactory

FYI: This is all done through the `DiscoverableSubtypeResolver` class:

Which gets attached to object mappers here:

Nick Telford

unread,
Apr 25, 2014, 11:37:09 AM4/25/14
to dropwiz...@googlegroups.com
Hi Phil,

This is exactly right. We use Java's SPI (Service Provider Interface) for all the pluggable Connectors (jetty), Reporters (metrics) and Appenders (logback).

As for your feature: it might be a neat idea for the GraphiteReporter to automatically replace "patterns" in the prefix; "%host" for example. What do you think?
Reply all
Reply to author
Forward
0 new messages