I am trying to add JMX metric in my Dropwizard application, But when I am trying make it up locally it gives me error. I added following in my Application class in initalize method :
guiceBundle.addModule(new MetricsInstrumentationModule(bootstrap.getMetricRegistry()))
bootstrap.addBundle(new JmxMetricsBundle());
and in guice module file, I have added provider:
@Provides
@Singleton
MetricRegistry providesMetricRegistry(Environment environment) {
return environment.metrics();
}
added below dependency in pom.xml
<dependency>
<groupId>com.palominolabs.metrics</groupId>
<artifactId>metrics-guice</artifactId>
<version>3.1.3</version>
</dependency>
In my class I am using it below way:
private Timer getTimer(String name) {
return metricRegistry
.timer(MetricRegistry.name(DocumentService.class, name));
}
public void method(){
final Timer.Context context = getTimer("methodMetric").time();
try{
//do something
}finally {
context.stop();
}
}
but when I am making my service up it is throwing following error:
DEBUG [2017-06-02 09:01:58,059] com.codahale.metrics.JmxReporter:
Unable to register timer! javax.management.InstanceAlreadyExistsException:
metrics:name=io.dropwizard.jetty.MutableServletContextHandler.connect-requests
DEBUG [2017-06-02 09:01:58,066] com.codahale.metrics.JmxReporter:
Unable to register gauge! javax.management.InstanceAlreadyExistsException:
metrics:name=io.dropwizard.jetty.MutableServletContextHandler.percent-
4xx-1m
And similar error messages, although my application is getting up successfully. I have no idea why this error is occurring, is it some localhost setup issue which will not happen on production or some genuine issue which I am missing. please help.
EDIT: JmxMetricsBundle class:
public class JmxMetricsBundle implements Bundle {
public void initialize(Bootstrap<?> bootstrap) {
JmxReporter reporter = JmxReporter.forRegistry(bootstrap.getMetricRegistry()).build();
reporter.start();
}
@Override
public void run(Environment environment) {
}
}