question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@Metered not working on Spring MVC Controller

See original GitHub issue

Hi there,

I tried to add @Metered to a Spring MVC Controller but do not get any metrics. I read the section about Spring AOP limitations but not sure if this the problem in this case.

My Spring configuration below and the annotations I added to the controller method. Is there any other configuration or annotations required to make this work?

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

<metrics:annotation-driven metric-registry="metrics" health-check-registry="healthCheck" />

<metrics:reporter type="slf4j" metric-registry="metrics" period="1m" />

<metrics:register metric-registry="metrics">
  <bean metrics:name="jvm.gc" class="com.codahale.metrics.jvm.GarbageCollectorMetricSet" />
  <bean metrics:name="jvm.memory" class="com.codahale.metrics.jvm.MemoryUsageGaugeSet" />
</metrics:register>

and my MVC Controller is annotated like this:

@Controller
public class BlaController {

    @Timed
    @Counted
        @Metered
    @ResponseBody
    @RequestMapping(value = "bla", method = RequestMethod.POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
    public SomeResponse bla(@RequestBody Request request) {
      // do something I want to measure
    }

}

Many thanks for pointers. Niels

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bertramncommented, Nov 14, 2015

Thanks a million, I did have indeed a subcontext in my web application. Adding the <metrics:annotation-driven /> element to the dispatcher context did the trick. Do you think it would be worth adding a little note about this to the README? Somethign like below:

If your spring application uses nested spring contexts and each context loads classes annotated with metrics annotations, add the <metrics:annotation-driven /> directive to each spring context configuration. This will ensure all metrics annotated classes are registered for metrics collection. A typical example is a spring mvc web application. You may configure the metrics registry and reporters in the main web application context whilst metered controllers are defined in the dispatch servlet context.

the application wide context file applicationContext.xml contains the metrics setup and may also load metered services:

<beans>
  <!-- application wide metrics configuration -->
  <metrics:metric-registry id="metrics" />
  <metrics:annotation-driven metric-registry="fancy-metrics" />
  <metrics:reporter type="slf4j" metric-registry="fancy-metrics" period="1m" />
  <!-- metered services here --->
</beans>

and in the dispatcher servlet specific context file api-v1-dispatcher-servlet.xml repeat the annotation driven configuration:

<beans>
  <!-- ensures that all controllers in this subcontext are registered -->
  <metrics:annotation-driven metric-registry="fancy-metrics" />
  <!-- metered controllers here --->
</beans>
1reaction
joakimhopcommented, Jan 7, 2016

I had a similar issue. This saved my day!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do i fix my controller not working in spring mvc?
Your Java code is right, but first you need to make sure that there is 'user' in the session, probably when logging in,...
Read more >
Quick Guide to Spring Controllers - Baeldung
A quick and practical guide to Spring Controllers - both for typical MVC apps and for REST APIs.
Read more >
Getting Started | Serving Web Content with Spring MVC
In Spring's approach to building web sites, HTTP requests are handled by a controller. You can easily identify the controller by the @Controller...
Read more >
[FIXED] Spring MVC Faulty URL Root cause Analysis - YouTube
Welcome to another spring MVC framework tutorial for beginners where I will be solving your issues with examples, This video is recorded ...
Read more >
Troubleshooting Spring MVC RequestMapping - YouTube
In this video I troubleshoot a problem with how the RequestMapping is configured in Spring MVC. A student of mine was having a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found