Saturday, July 4, 2015

Configure Graphite with Dropwizard Metrics in Spring Boot

To configure Graphite with Dropwizard Metrics in Spring Boot,

add the following Java Config:

@Configuration
public class GraphiteConfig {

  @Autowired
  private MetricRegistry metricRegistry;

  @Value("${graphite.host}")
  private String graphiteHost;

  @Value("${graphite.port}")
  private int graphitePort;

  @PostConstruct
  public void initialize() {
    Graphite graphite = new Graphite(this.graphiteHost, this.graphitePort);
    GraphiteReporter reporter = GraphiteReporter.forRegistry(this.metricRegistry)
        .prefixedWith(NetworkUtils.HOSTNAME.replace(".", "_"))
        .convertRatesTo(TimeUnit.SECONDS)
        .convertDurationsTo(TimeUnit.MILLISECONDS)
        .filter(MetricFilter.ALL).build(graphite);
    reporter.start(1, TimeUnit.MINUTES);
  }

}

and add the following properties:

graphite.host=1.2.3.4
graphite.port=2003

Reference:
https://dropwizard.github.io/metrics/3.1.0/manual/graphite/

No comments:

Post a Comment