Download Android App

Alternate Blog View: Timeslide Sidebar Magazine

Wednesday, July 20, 2011

Metrics and more

In my quest for quality open source software, I came across a library for "Capturing JVM and application-level metrics. So you know what's going on."

Meet Metrics @

One great feature about this library is it can be easily integrated with Graphite. Graphite is a a highly scalable real-time graphing system.

A presentation about the library can be viewed here. I must say it is a great presentation. A memorable line from the presentation:

"Business value is anything which makes people more likely to give us money. We want to generate more business value." 

The library is actually very simple to use. Some code snippets to help you get started:

public class EServiceImpl implements EService {

//Measure the # of times the API is invoked
private final MeterMetric eMetric = Metrics.newMeter(
 getClass(), "Send API", "reqs", TimeUnit.SECONDS);

//Measure the rate of event i.e. req/ sec.
private final CounterMetric eCounter = Metrics.newCounter(getClass(),

ConsoleReporter.enable(10, TimeUnit.SECONDS) //Print metrics to STDERR every 10s

public void service1(){;//Increase counter by 1
        eMetric.mark();//Mark the occurrence of an event.

Output on the console:

7/20/11 10:41:06 AM =============================================================

    count = 10

  Send API:
             count = 10
         mean rate = 5.00 reqs/s
     1-minute rate = 2.50 reqs/s
     5-minute rate = 0.50 reqs/s
    15-minute rate = 0.16 reqs/s

If you're writing a Servlet-based web service, you can add MetricsServlet from the metrics-servlet subproject to an internally-accessible context. Add the following lines in web.xml.




Navigate to: http://hostname:port/WebAppName/CodahaleMetrics

The output metrics is dumped in JSON format. You can parse it and pass it to your favorite graphing library.

Note: All metrics are reported via JMX, which you can view using VisualVM or JConsole.

Documentation needs some work but overall, I would rate the project as Very Good.

1 comment:

  1. Hi Chandra,
    I followed this tutorial, along with some other snippets, and got metrics to report to JMX (I can see them from JConsole). However, using Metrics with Graphite, or even Metrics with Jetty, I cannot get the Metrics Servlet to output the stats in JSON format. I do see the JVM stats however.
    Help please?