[Java Tip] Using Ehcache with Dropwizard Metrics package

Dropwizard Metrics aka Codahale Metrics is a very popular Metrics collection and reporting framework. It comes along with a host of goodies. If you already have the Metrics used in your application, you can easily integrate it with Ehcache or if you plan to use Ehcache and need to integrate it with your existing monitoring infrastructure, Metrics library is a great way to start.

We shall be looking at a simple example of integration. The same can be adopted to your needs.

The code can be found at EhcacheWithMetrics.java

We need to add Metrics Ehcache module to make it work. Here is the pom.xml entry, assuming a maven project

 
<dependency>
      <groupId>io.dropwizard.metrics</groupId>
      <artifactId>metrics-core</artifactId>
      <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>io.dropwizard.metrics</groupId>
      <artifactId>metrics-ehcache</artifactId>
      <version>3.1.0</version>
    </dependency>

Lets quickly look at the code snipper

public static void metricsCacheDemo() {

    MetricRegistry metricRegistry = new MetricRegistry();

    // lets add a Console reporter to the metrics
    ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry)
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .build();
    reporter.start(1, TimeUnit.SECONDS);

    CacheManager cacheManager = CacheManager.newInstance();
    Ehcache cache = cacheManager.addCacheIfAbsent("testCache");

    Ehcache instrumentedCache = InstrumentedEhcache.instrument(metricRegistry, cache);

    // lets add 10K elemnets

    for (int i = 0; i < 100000; i++) {
      Element cacheElement = new Element("Key-"+i, "Value-"+i);
      instrumentedCache.put(cacheElement);
      instrumentedCache.get("Key-"+i);
    }

    cacheManager.shutdown();
  }

To integrate Ehcache with Metrics package, we need to use the instrumented cache (shown in line #15). The cache that need to be integrated need to be decorated with Cache Wrapper provided by Metrics package. Rest code is glue code to display metrics on console. Here is a sample output of the run

-- Gauges ------------------------------------------
net.sf.ehcache.Cache.testCache.eviction-count
             value = 0
net.sf.ehcache.Cache.testCache.hits
             value = 100000
net.sf.ehcache.Cache.testCache.in-memory-hits
             value = 10000
net.sf.ehcache.Cache.testCache.in-memory-misses
             value = 90000
net.sf.ehcache.Cache.testCache.in-memory-objects
             value = 0
net.sf.ehcache.Cache.testCache.mean-get-time
             value = 14491.703423674926
net.sf.ehcache.Cache.testCache.mean-search-time
             value = NaN
net.sf.ehcache.Cache.testCache.misses
             value = 0
net.sf.ehcache.Cache.testCache.objects
             value = 100000
net.sf.ehcache.Cache.testCache.off-heap-hits
             value = 0
net.sf.ehcache.Cache.testCache.off-heap-misses
             value = 0
net.sf.ehcache.Cache.testCache.off-heap-objects
             value = 0
net.sf.ehcache.Cache.testCache.on-disk-hits
             value = 90000
net.sf.ehcache.Cache.testCache.on-disk-misses
             value = 0
net.sf.ehcache.Cache.testCache.on-disk-objects
             value = 100000
net.sf.ehcache.Cache.testCache.searches-per-second
             value = 0.0
net.sf.ehcache.Cache.testCache.writer-queue-size
             value = 0

-- Timers --------------------------------------------
net.sf.ehcache.Cache.testCache.gets
             count = 100000
         mean rate = 23844.67 calls/second
     1-minute rate = 0.00 calls/second
     5-minute rate = 0.00 calls/second
    15-minute rate = 0.00 calls/second
               min = 0.00 milliseconds
               max = 2.60 milliseconds
              mean = 0.02 milliseconds
            stddev = 0.09 milliseconds
            median = 0.01 milliseconds
              75% <= 0.02 milliseconds
              95% <= 0.02 milliseconds
              98% <= 0.04 milliseconds
              99% <= 0.10 milliseconds
            99.9% <= 0.83 milliseconds
net.sf.ehcache.Cache.testCache.puts
             count = 100000
         mean rate = 23833.00 calls/second
     1-minute rate = 0.00 calls/second
     5-minute rate = 0.00 calls/second
    15-minute rate = 0.00 calls/second
               min = 0.00 milliseconds
               max = 1.69 milliseconds
              mean = 0.01 milliseconds
            stddev = 0.06 milliseconds
            median = 0.00 milliseconds
              75% <= 0.00 milliseconds
              95% <= 0.01 milliseconds
              98% <= 0.04 milliseconds
              99% <= 0.06 milliseconds
            99.9% <= 0.56 milliseconds

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.