Download Android App


Alternate Blog View: Timeslide Sidebar Magazine
Showing posts with label codahale. Show all posts
Showing posts with label codahale. Show all posts

Sunday, December 21, 2014

Codahale Metrics and Spring

This is a revision of my previous post on Coda Hale Metrics available here.

Goal: Integrate Metrics (v 3.1.0) and Spring (v 4.1.x) in a JEE environment.

Metrics-spring resides here. Code snippets to help you get started below:

1. Add the following dependency in pom.xml

<dependency>
     <groupid>io.dropwizard.metrics</groupid>
     <artifactid>metrics-servlets</artifactid>
     <version>3.1.0</version>
</dependency>
<dependency>
     <groupid>com.ryantenney.metrics</groupid>
     <artifactid>metrics-spring</artifactid>
     <version>3.0.3</version>
</dependency>
..
Other metrics dependencies

2. Add the following in web.xml

<servlet>
 <servlet-name>metrics-admin</servlet-name>
 <servlet-class>com.codahale.metrics.servlets.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>metrics-admin</servlet-name>
 <url-pattern>/metrics/admin/*</url-pattern>
</servlet-mapping>


3. Define metrics.xml and include it in your main spring configuration file.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:metrics="http://www.ryantenney.com/schema/metrics"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.ryantenney.com/schema/metrics
               http://www.ryantenney.com/schema/metrics/metrics-3.0.xsd">

    <!-- Registry should be defined in only one context XML file -->
    <metrics:metric-registry id="metrics" />

    <metrics:health-check-registry id="healthCheck" />

    <!-- annotation-driven must be included in all context files -->
    <metrics:annotation-driven metric-registry="metrics"
        health-check-registry="healthCheck" />

    <!-- (Optional) Registry should be defined in only one context XML file -->
    <metrics:reporter type="console" metric-registry="metrics"
        period="1m" />

    <bean
        class="org.springframework.web.context.support.ServletContextAttributeExporter">
        <property name="attributes">
            <map>
                <entry key="com.codahale.metrics.servlets.MetricsServlet.registry">
                    <ref bean="metrics" />
                </entry>
                <entry key="com.codahale.metrics.servlets.HealthCheckServlet.registry">
                    <ref bean="healthCheck" />
                </entry>
            </map>
        </property>
    </bean>
</beans>


4. Define HealthCheck classes and annotate methods with @Timed as per Metrics documentation. And we are done!

Navigate to: http://hostname:port/<webappname>/metrics/admin to view "Operational Menu"

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 Console. You can easily enable other reporters in metrics.xml.

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 @ https://github.com/codahale/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(),
 "ECounter");

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

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


Output on the console:

7/20/11 10:41:06 AM =============================================================
com.impl.EServiceImpl:

  ECounter:
    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.



<servlet>
 <servlet-name>CodahaleMetrics</servlet-name>
 <servlet-class>com.yammer.metrics.reporting.MetricsServlet</servlet-class>
 <init-param>
  <param-name>metrics-uri</param-name>
  <param-value>/metrics</param-value>
 </init-param>
 <init-param>
  <param-name>ping-uri</param-name>
  <param-value>/ping</param-value>
 </init-param>
 <init-param>
  <param-name>healthcheck-uri</param-name>
  <param-value>/health</param-value>
 </init-param>
 <init-param>
  <param-name>threads-uri</param-name>
  <param-value>/threads</param-value>
 </init-param>
</servlet>

....

<servlet-mapping>
 <servlet-name>CodahaleMetrics</servlet-name>
 <url-pattern>/CodahaleMetrics/*</url-pattern>
</servlet-mapping>

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.