Spring Boot Actuator + Dropwizard + Cloudwatch
Exporting spring boot metrics to cloudwatch may not be that straightforward. Since I’ve done that for my project, I will show how we can do it easily with the help of a couple of libraries.
First, spring boot is using its own metrics mechanism. The first step to do that is to swap it with dropwizard’s metrics library which is more powerful and has more feature. Thankfully, the actuator has complete support for the library from what it’s written in the documentation. Spring will just swap its interface to use the implementation which wraps the dropwizard metrics.
In order to integrate dropwizard metrics, we just have to add the library ryantenney’s created for spring boot:
<dependency>
<groupId>com.ryantenney.metrics</groupId>
<artifactId>metrics-spring</artifactId>
<version>3.1.3</version>
</dependency>
And that’s all we have to do to use dropwizard’s metrics. The next step is to upload the metrics to cloudwatch. There’s also already an existing library for this.
<dependency>
<groupId>com.damick</groupId>
<artifactId>dropwizard-metrics-cloudwatch</artifactId>
<version>0.1.5</version>
</dependency>
And after this, we just need to register a bean for to export the metrics to cloudwatch:
@Configuration
public class MetricsConfig extends MetricsConfigurerAdapter {
@Value("${spring.application.name}")
private String applicationName;
@Value("${spring.profiles.active}")
private String environment;
@Override
public void configureReporters(MetricRegistry metricRegistry) {
AmazonCloudWatchAsync amazonCloudWatchAsync = new AmazonCloudWatchAsyncClient();
amazonCloudWatchAsync.setRegion(Region.getRegion(Regions.EU_WEST_1));
String nameSpace = environment + "." + applicationName;
CloudWatchReporter cloudWatchReporter =
new CloudWatchReporter(metricRegistry, nameSpace, amazonCloudWatchAsync);
registerReporter(cloudWatchReporter).start(1, TimeUnit.MINUTES);
}
}
In here, you can change the regions and add more configuration. For my settings I use the environment name and application name as the namespace. After this, the metrics should be in cloudwatch, and now we can start creating dashboard and alarms.
For the version of the library, I have tested it with spring boot 1.2.7.RELEASE, 1.2.8.RELEASE, and 1.3.0.RELEASE. I think it will work for most of the spring boot version though, although I can’t guarantee everything. Better check it yourself.
Hopefully this article is useful for some of you.