If you're going down the microservice rabbit whole using frameworks like Spring Boot and Dropwizard it is imperative you can monitor what is going on, part of that is pushing metrics to some type of metrics system.
The last set of applications I built used Graphite for this purpose, and fortunately the DataStax Java driver stores lots of interesting metrics using the brilliant dropwizard metrics library.
Here's what it takes to get the Cassandra metrics and your custom metrics from a Spring boot application into Graphite.
This article assumes you know how to use the DataStax Cassandra driver and the Dropwizard metrics library and you're familiar with tools like Maven and Gradle. If you don't go read up on those first.
First let's get the Cassandra driver and metrics libraries on our classapth, here is my example using Gradle:
I've included the Actuator from Spring boot as well.
Assuming you have a bean that is your Cassandra Session add a bean to expose the MetricRegistry and to create a GraphiteReporter:
Here I have a graphite server running on 192.168.10.120. If you don't want to install Graphite to try this out I have a Vagrant VM on my
which launches Graphtie + Graphana.
If we had the Cluster as a bean rather than the Session we'd have injected that. We've now set it up so that all the metrics the DataStax Java driver records will be published to Graphite every 30 seconds.
Now we can plot all kinds of graphs:
For instance we can plot request times, number of errors, number of requests, etc. This becomes even more powerful when you are deploying multiple versions of your application and you pre-fix each instance with a identifier such as its IP.
Adding our own metrics with annotations
The next step is to add more metrics, as the ones in the DataStax library aren't very fine grained, for example we
might want to time particular queries, or look at our response times.
You can do this manually but it is easier with annotations. We can do this with the Metric-Spring
project. This project integrates Spring AOP
with drop wizard metrics.
However it is quite fiddly to get working as we now have three libraries that want to create a MetricRegistry:
SpringBoot, Cassandra Driver and Metric-Spring.
To get everyone to use the Cassandra driver's MetricRegistry we need to create a MetricsConfigurerAdapter:
The reason we're injecting the Session is we can no longer register a bean for the MetricRegistry as Spring-Metric
does this and we don't want to end up with two. To get this to work we have to remove the metricRegistry bean from
the code above. The other thing we do is add the EnableMetric annotation to our Application class:
Once all this is done we can annotate our public methods with @Timed like this:
Then in Graphite we can see them, their name is derived from the fully qualified method name.
So now our Spring Boot application has Cassandra metrics
and our own custom application metrics all pushing to Graphite!
The whole application is on GitHub
if you want the full Spring config and dependencies.