Yammer Metrics with Spring tutorial
When you are running long term applications like web applications, batch jobs, or stand-alone status update jobs, it is good to know some statistics about them, like number of requests served or request duration. You can also gather more generic information like the state of your internal collections, how many times some portion of code is being executed, or health checks like database availability, or any kind of connection to an external system.
All this kind of instrumentation can be achieved by using native JMX or using a modular project like yammer Metrics. Metrics provides a powerful way to measure the behaviour of your critical components and reporting them to a variety of systems like, JConsole, System Console, Ganglia, Graphite, CSV, or making them available through a web services as JSON data.
Step 1: Required libraries. The metrics-core, metrics-annotation, and metrics-spring are the key libraries for gathering metrics.
Step 2: Artifacts used.
Step 3: Let's create a very basic TradeEngine, and monitor the number of requests and request execution times.
Take notice of @Gauge, @Timed, and @ExceptionMetered annotations used for monitoring. The Request class used in the TradeEngine.
Step 4: Spring applicationContext.xml file to wire up TradeEngine, metrics-registry, and jmx-reporter.
Step 5: Stand-alone TradeEngineMain class that runs until forcefully stopped. Run continuously so that you can monitor the metrics via JMX compliant jconsole.
You can run the TradeEngineMain application, and then start the jconsole from a command-line and monitor the number of requests processed and the stats on the execution time. I will cover this in the next post.
package com.writtentest12; public interface TradeEngine { abstract void execute(Request... requests); }
package com.writtentest12; import java.util.concurrent.atomic.AtomicInteger; import com.yammer.metrics.annotation.ExceptionMetered; import com.yammer.metrics.annotation.Gauge; import com.yammer.metrics.annotation.Timed; public class TradeEngineImpl implements TradeEngine { @Gauge private final AtomicInteger currentRequests = new AtomicInteger(); @Timed @ExceptionMetered public void execute(Request... requests) { executingRequests(requests.length); try { Thread.sleep(2000); //just to emulate some processing } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void executingRequests(int count) { this.currentRequests.addAndGet(count); } }
Take notice of @Gauge, @Timed, and @ExceptionMetered annotations used for monitoring. The Request class used in the TradeEngine.
package com.writtentest12; public class Request { }
Step 4: Spring applicationContext.xml file to wire up TradeEngine, metrics-registry, and jmx-reporter.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:metrics="http://www.yammer.com/schema/metrics" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.yammer.com/schema/metrics http://www.yammer.com/schema/metrics/metrics.xsd"> <metrics:metrics-registry id="trade-metrics"/> <metrics:health-check-registry id="trade-health"/> <metrics:annotation-driven metrics-registry="trade-metrics" /> <metrics:jmx-reporter metrics-registry="trade-metrics"/> <bean id="tradeEngine" class="com.writtentest12.TradeEngineImpl"/> </beans>
Step 5: Stand-alone TradeEngineMain class that runs until forcefully stopped. Run continuously so that you can monitor the metrics via JMX compliant jconsole.
package com.writtentest12; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TradeEngineMain { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "com/writtentest12/applicationContext.xml" }); TradeEngine tradeEngine = (TradeEngine) context.getBean("tradeEngine"); try { while (true) { Request[] requests = new Request[2]; requests[0] = new Request(); requests[1] = new Request(); tradeEngine.execute(requests); Thread.sleep(5000); } } catch (InterruptedException e) { e.printStackTrace(); } } }
You can run the TradeEngineMain application, and then start the jconsole from a command-line and monitor the number of requests processed and the stats on the execution time. I will cover this in the next post.
Labels: Metrics, Monitoring, Performance
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home