@WebListener
public class MetricsServletContextListener extends MetricsServlet.ContextListener {
public final MetricRegistry METRIC_REGISTRY=new MetricRegistry();
@Override
protected MetricRegistry getMetricRegistry() {
registerAll("gc", new GarbageCollectorMetricSet(), METRIC_REGISTRY);
registerAll("buffers", new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer()), METRIC_REGISTRY);
registerAll("memory", new MemoryUsageGaugeSet(), METRIC_REGISTRY);
registerAll("threads", new ThreadStatesGaugeSet(), METRIC_REGISTRY);
return METRIC_REGISTRY;
}
private void registerAll(String prefix, MetricSet metricSet, MetricRegistry registry) {
for (Map.Entry<String, Metric> entry : metricSet.getMetrics().entrySet()) {
if (entry.getValue() instanceof MetricSet) {
registerAll(prefix + "." + entry.getKey(), (MetricSet) entry.getValue(), registry);
} else {
registry.register(prefix + "." + entry.getKey(), entry.getValue());
}
}
}
}
I have a class annotated as a service and I am allowing spring to inject the metrics registry as such:
@Service
public class ImageTransformer implements MediaTransformer<MediaAsset> {
@Autowired
MetricRegistry registry;for (String objectId : transformObjects) {
Timer timer= registry.timer(name(ImageTransformer.class, "image.transformer.setup"));
Timer.Context context= timer.time();
if (!setupTransform(mediaAsset, objectId)) {
// Bail if cannot setup environment.
context.stop();
return retVal;
}
context.stop();}
"threads.waiting.count" : { "value" : 8 } }, "counters" : { }, "histograms" : { }, "meters" : { }, "timers" : { } }
@SpringBootApplication
@EnableJms
@EnableMetrics(proxyTargetClass=true)
@EnableMBeanExport
public class MediaTransformApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MediaTransformApplication.class, args);
}
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new AdminServlet(), "/metrics/*");
}
@Bean
public MetricRegistry metricRegistry(){
return new MetricRegistry();
}
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'imageTransformer': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.codahale.metrics.MetricRegistry com.pearson.media.ImageTransformer.registry; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.codahale.metrics.MetricRegistry] is defined: expected single matching bean but found 2: getMetricRegistry,metricRegistry
Michael Griffith
Sr. Software Architect
M +44 (1) 507-299-0004
michael....@pearson.com
Learn more at pearson.com
ALWAYS LEARNING
--
You received this message because you are subscribed to a topic in the Google Groups "metrics-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/metrics-user/puWVXHcYEf8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to metrics-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.