Dropwizard Metrics is a Java library that provides tools to measure an application.
Dropwizard offer various tools that help you with distinct metrics, like gauges, counters, histograms and other.
The library has two phases, the first is the recollection of data and the second is the reporting of the collected data to a data collector, in this post we will integrate the recollection of data to an existing standard Java EE application.
First we need to create a MetricRegistry
instance that will allows us to create the various objects than implement the metric recollection, this registry can be stored in a Application Bean if you are using CDI, or in an Singleton bean if you are using only EJB.
This class can look like:
In this example we provide only two collectors, you can implement the others in a similar way.
Simple usage
Now, we can use this bean, suppose we has a redis connection managed by Jedis, Jedis provides us with a class called JedisPool
that is a pool of jedis instances.
This pool can have multiple jedis instances inside, and we are interested in the number of instances that are used, and how many instances are available at any moment, for this we can use metrics.
So we create a class that has a @Produces
the jedis instances, something like this:
With this, every time a new @Inject Jedis jedis
is processed, the counter will increase, and after the bean has been used, the counter will decrement.
For this example we can also use gauges.
Using JaxRS request and response filters
Metrics provide a interesting measure, the Timer. This measure has a annotation called @Timed
, unfortunately I was unable to find any implementation that works will JaxRS (and Resteasy, there is an Jersey implementation, but use custom Jersey function and classes).
We can create our custom @Timed
annotation, and annotate it with @NameBinding
, so we can use it as a binding annotation and is available to us in the JaxRS filters.
The annotation looks like:
And we can implement a simple JaxRS filter like this:
This will intercept every petition to a class (or method) annotated with @Timed
, and it will create a timer that will measure the duration of the request.
Important notes:
- If we don’t use the
@PreMatching
annotation, this will be invoked after JaxRS find outs what method to invoke, so the time to find the correct method is not calculated. - This will not use any client side information, and only rely on the time that our methods take to execute
- This will store the
Context
object in the servlet request, if we don’t want to populate our request, and later calculate the time.
We can use this like this: