ryantenney / metrics-spring

Spring integration for Metrics
http://www.ryantenney.com/metrics-spring/
Apache License 2.0
701 stars 226 forks source link

@Timed not working on a spring application (not a webapp) #156

Closed bhuvanalakshmi closed 9 years ago

bhuvanalakshmi commented 9 years ago

I am using Java annotation to configure.

@Configuration
@EnableMetrics
public class MetricsConfig extends MetricsConfigurerAdapter{

    @Bean
    public MetricRegistry metricRegistry(){
        return new MetricRegistry();
    }

    @Override
    public void configureReporters(MetricRegistry metricRegistry) {

        metricRegistry.registerAll(new GarbageCollectorMetricSet());
        metricRegistry.registerAll(new MemoryUsageGaugeSet());
        metricRegistry.registerAll(new ThreadStatesGaugeSet());

        // registerReporter allows the MetricsConfigurerAdapter to
        // shut down the reporter when the Spring context is closed
        registerReporter(ConsoleReporter
                .forRegistry(metricRegistry)
                .build())
        .start(1, TimeUnit.MINUTES);

    }

}

JVM metrics are reported in the console. But when i use @Timed annotation on another class. I am getting Bean Creation exception and application won't start. When the @Timed annotation is removed, application starts and JVM metrics are reported in the console. Is there an issue with configuration ?

015-12-02 11:18:48,820  INFO [main] .AppStarter - Spring context initialized.
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [.SampleDataMgmt] is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1063)
    at .AppStarter.init(AppStarter.java:51)
    at .AppStarter.main(AppStarter.java:24)
ryantenney commented 9 years ago

Change @EnableMetrics to @EnableMetrics(proxyTargetClass = true)

ryantenney commented 9 years ago

Your SampleDataMgmt bean likely implements an interface, and somewhere else you're referencing the class, not the interface. By default Spring AOP uses Java Dynamic Proxies when proxying a class which implements an interface, so instead of an instance of SampleDataMgmt: class SampleDataMgmt implements SomeInterface { … } Spring will have "generated" a class class Proxy$47 implements SomeInterface { … }. Proxy Target Class will force that proxy to be class Proxy$47 extends SampleDataMgm { … }

Hope that helps.

bhuvanalakshmi commented 9 years ago

Thank you, that helped.