Spring Boot comes with a heathy dose of metrics built in but having used Dropwizard extensively in house we have come to rely on some of the timing metrics it provides out of the box. As we turn our gaze to Spring Boot we wanted to make sure we still had access to the same extensive metrics that Dropwizard provides.
I have provided a basic project on github with metrics embedded and below will describe how you can add the Dropwizard metrics to your own Spring Boot projects.
Basic setup
Add the Dropwizard metrics dependancies to your gradle.build and a dependancy for Ryan Tenney's Spring metrics library. Ryan Tenney's library provides easy integration of the Dropwizard metrics libraries with Spring Boot.
compile ('com.ryantenney.metrics:metrics-spring:3.1.3') {
exclude group: 'com.codahale.metrics'
exclude group: 'org.springframework'
}
compile 'io.dropwizard.metrics:metrics-core:3.1.2'
compile 'io.dropwizard.metrics:metrics-annotation:3.1.2'
Make your SpringBootApplication
extend Ryan Tenney's MetricsConfigurerAdapter
and add his @EnableMetrics(proxyTargetClass = true)
annotation to your Application class.
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableMetrics(proxyTargetClass = true)
public class Application extends MetricsConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Add the @Timer
annotation to any endpoints you wish to add timers for.
import com.codahale.metrics.annotation.Timed;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@Timed
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
You will then find the Dropwizard metrics added to the native Spring Boot metrics found at localhost:8080/metrics
.
{
"mem": 657794,
"mem.free": 455872,
"processors": 4,
"instance.uptime": 25609,
"uptime": 32713,
"systemload.average": 2.64697265625,
"heap.committed": 609792,
"heap.init": 131072,
"heap.used": 153919,
"heap": 1864192,
"nonheap.committed": 49344,
"nonheap.init": 2496,
"nonheap.used": 48002,
"nonheap": 0,
"threads.peak": 14,
"threads.daemon": 4,
"threads.totalStarted": 18,
"threads": 14,
"classes": 6239,
"classes.loaded": 6239,
"classes.unloaded": 0,
"gc.ps_scavenge.count": 9,
"gc.ps_scavenge.time": 126,
"gc.ps_marksweep.count": 1,
"gc.ps_marksweep.time": 54,
"co.instil.HelloController.index.snapshot.75thPercentile": 0,
"co.instil.HelloController.index.snapshot.median": 0,
"co.instil.HelloController.index.snapshot.max": 8,
"co.instil.HelloController.index.fifteenMinuteRate": 0.004432121596082617,
"co.instil.HelloController.index.snapshot.mean": 1,
"co.instil.HelloController.index.meanRate": 0.26342740827887096,
"co.instil.HelloController.index.snapshot.95thPercentile": 8,
"co.instil.HelloController.index.oneMinuteRate": 0.06396446829654137,
"co.instil.HelloController.index.count": 7,
"co.instil.HelloController.index.snapshot.98thPercentile": 8,
"gauge.response.metrics": 184,
"co.instil.HelloController.index.snapshot.999thPercentile": 8,
"gauge.response.root": 4,
"counter.status.200.metrics": 1,
"co.instil.HelloController.index.snapshot.stdDev": 2,
"counter.status.200.root": 7,
"co.instil.HelloController.index.fiveMinuteRate": 0.013222836942706007,
"co.instil.HelloController.index.snapshot.min": 0,
"co.instil.HelloController.index.snapshot.99thPercentile": 8
}