Yammer @Timed leaving values ​​at zero

This is a continuation of my struggle using yammer sync annotations, as described here .

The context file My spring has simply:

<metrics:annotation-driven /> 

I have the following class:

 import com.yammer.metrics.annotation.ExceptionMetered; import com.yammer.metrics.annotation.Metered; import com.yammer.metrics.annotation.Timed; ... @Component public class GetSessionServlet extends HttpServlet { private final static Logger log = LoggerFactory.getLogger(GetSessionServlet.class); @Override public void init(final ServletConfig config) throws ServletException { super.init(config); SpringBeanAutowiringSupport.processInjectionBasedOnServletContext( this, config.getServletContext()); } @Override @Timed(name = "get-session", rateUnit = TimeUnit.MILLISECONDS) @Metered @ExceptionMetered(name = "get-session-failures", rateUnit = TimeUnit.MILLISECONDS) public void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final String sessionId = req.getParameter("sessionId"); final String fields = req.getParameter("fields"); final String format = req.getParameter("format"); if (StringUtils.isEmpty(sessionId)) { resp.getWriter().write("sessionId parameter missing!\n"); return; } ... } 

I run my application locally using mvn clean tomcat7:run , and then connect jconsole.

On the MBeans tab, I can see the entry with the package name of my GetSessionServlet class with three subfolders doGet (for @Metered numbers), get-session and get-session-failure.

However, no matter how many times I call my servlet, all the values ​​in the subfolders above remain at zero. What am I missing? In addition, any documentation on these metrics that will be more detailed than the official documentation will be evaluated.

+6
source share
2 answers

I think I did it.

The first thing I noticed was that the URI in the schemaLocation attribute of the spring test file: http://www.yammer.com/schema/metrics/metrics.xsd returns a 404 error.

I looked for this problem to find out if anyone regretted the unavailability of the scheme and found this thread github.com/codahale/metrics/issues/322, in which the user comments that the spring metrics module has been removed from the main metrics repository and moved at: github.com/ryantenney/metrics-spring.

Following the example on this website, I removed the old spring metric from my pump and added the following:

  <dependency> <groupId>com.ryantenney.metrics</groupId> <artifactId>metrics-spring</artifactId> <version>2.1.4</version> </dependency> 

Then I updated the xmlns declaration in spring -context.xml:

 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:metrics="http://www.ryantenney.com/schema/metrics" xsi:schemaLocation=" http://www.ryantenney.com/schema/metrics http://www.ryantenney.com/schema/metrics/metrics.xsd ...other schemas..."> 

Added annotation driven element:

 <metrics:annotation-driven proxy-target-class="true" /> 

I have included proxy-target-class for working with class-based startup.

And in the bean, I wanted Time to add the @Timed annotation and the @PostConstruct method to allow ConsoleReporter to reset statistics every 10 seconds:

 @PostConstruct public void init() { ConsoleReporter.enable(10, TimeUnit.SECONDS); } @Timed public void doService(...) { .... do stuff .... } 

Then I ran my webapp .... and got an unpleasant error from the sax parser while parsing the spring context.xml file.

It looks like www.ryantenney.com/schema/metrics/metrics.xsd 404s!

The workaround for this was simple, however I downloaded xsd from the github repository: https://github.com/ryantenney/metrics-spring/blob/master/src/main/resources/com/ryantenney/metrics/spring/config/metrics -3.0.xsd

and then the following files are added to the src / main / resources / META-INF directory of my maven project:

 META-INF/ - spring.schemas - xsd/ - metrics.xsd 

where metrics.xsd is the xsd file downloaded from github and spring.schemas is a text file containing:

 http\://www.ryantenney.com/schema/metrics/metrics.xsd=META-INF/xsd/metrics.xsd 

The spring.schemas file is read by spring at startup and tells it to output xsd from the local file.

With the above, I was able to successfully launch my application and click on the endpoint that used the service method.

Watching the console, I really saw the following resetting every 10 seconds:

 com.sample.service.MyServiceClass: doService: count = 8 mean rate = 0.27 calls/s 1-minute rate = 0.23 calls/s 5-minute rate = 0.21 calls/s 15-minute rate = 0.20 calls/s min = 0.13ms max = 19.59ms mean = 2.59ms stddev = 6.87ms median = 0.18ms 
+5
source

I posted a similar question here and received not much more. I was told that I probably get 0 numbers because "Spring cannot intercept an annotated method when this method is called from the class itself." Although I read this comment earlier, I did not understand that I was in this situation, but looking at the source of the HttpServlet, I really see methods like doHead that call doGet.

Although this restriction is a failure, I started calling from my doGet method in another class to see if I could make the timing this way (cumbersome, but wishing to try):

 import org.springframework.stereotype.Component; import com.yammer.metrics.annotation.Metered; import com.yammer.metrics.annotation.Timed; @Component public class Delay { // @Metered @Timed public void time() { System.out.println("Timing..."); try { Thread.sleep(1000); } catch (final InterruptedException e) { e.printStackTrace(); } } } 

However, calling this method from my servlet receiver is as follows:

 public void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final Delay delay = new Delay().time(); 

still not showing me a nonzero number for the Delay.time method no matter how many times i get.

At this point, I am going to take on the provocative position that due to any limitations with these annotations and the lack of complete documentation and useful code examples, these annotations are not useful, at least in the context of time synchronizers.

I want to be wrong and I’ll gladly change my voice to accept a different answer because these annotations looked promising and more practical to use than Metrics.newTimer and similar methods.

+3
source

All Articles