Wednesday, April 1, 2015

Runtime metrics of Java applications through Metrics by Yammer

Metrics by yammer provide handy ways to capture runtime metrics and statistics of various applications. Metrics provide various types of measurements like counters, gauges, histograms, timers etc. More on the same can be read from the Metrics documentation

This post is about creating a sample Java application which uses a Metrics counter and monitoring the counter through jconsole.

Maven dependency :
 <dependencies>  
   <dependency>  
     <groupId>io.dropwizard.metrics</groupId>  
     <artifactId>metrics-core</artifactId>  
     <version>3.1.0</version>  
   </dependency>  
 </dependencies>  

Java class :
 import com.codahale.metrics.Counter;  
 import com.codahale.metrics.JmxReporter;  
 import com.codahale.metrics.MetricRegistry;  
 import static com.codahale.metrics.MetricRegistry.name;  
 public class YammerTest {  
  static final MetricRegistry metrics = new MetricRegistry();  
  private static final Counter iterations = metrics.counter(name(YammerTest.class,"iterations"));  
  private static JmxReporter reporter = JmxReporter.forRegistry(metrics).build();  
  public static void main(String[] args) {  
   //Counter Example  
   int counter = 0;  
   reporter.start();  
   while(true){  
    System.out.println("hello world");  
    iterations.inc();  
   }  
  }  
 }  


Now run this code in a shell and it will start printing "hello world" continuously to the console. Open another terminal and start "jconsole", and connect to the local process "YammerTest". 

Screen shot:





You should be able to see the metrics variable through the jconsole under the managed beans tab. This means that the Java application is successfully exposing the metrics counter using JMX, and we can easily ship the counters to any metrics store like Graphite or Ganglia. 

Also read:
Oracle doc
yammer blog 1
Yammer blog 2



No comments:

Post a Comment