@ -51,7 +51,7 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
@@ -51,7 +51,7 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
private final MetricRegistry registry ;
private final ConcurrentMap < String , Object > gaugeLock s = new ConcurrentHashMap < String , Object > ( ) ;
private final ConcurrentMap < String , SimpleGauge > gauges = new ConcurrentHashMap < String , SimpleGauge > ( ) ;
private final ConcurrentHashMap < String , String > names = new ConcurrentHashMap < String , String > ( ) ;
@ -99,15 +99,22 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
@@ -99,15 +99,22 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
}
else {
name = wrapGaugeName ( name ) ;
final double gauge = value ;
// Ensure we synchronize to avoid another thread pre-empting this thread after
// remove causing an error in Dropwizard metrics
// NOTE: Dropwizard provides no way to do this atomically
synchronized ( getGaugeLock ( name ) ) {
this . registry . remove ( name ) ;
this . registry . register ( name , new SimpleGauge ( gauge ) ) ;
setGaugeValue ( name , value ) ;
}
}
private void setGaugeValue ( final String name , final double value ) {
// NOTE: Dropwizard provides no way to do this atomically
SimpleGauge gauge = this . gauges . get ( name ) ;
if ( gauge = = null ) {
final SimpleGauge newGauge = new SimpleGauge ( value ) ;
gauge = this . gauges . putIfAbsent ( name , newGauge ) ;
if ( gauge = = null ) {
gauge = newGauge ;
this . registry . register ( name , gauge ) ;
}
}
gauge . setValue ( value ) ;
}
private String wrapGaugeName ( String metricName ) {
@ -130,16 +137,6 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
@@ -130,16 +137,6 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
return name ;
}
private Object getGaugeLock ( String name ) {
Object lock = this . gaugeLocks . get ( name ) ;
if ( lock = = null ) {
Object newLock = new Object ( ) ;
lock = this . gaugeLocks . putIfAbsent ( name , newLock ) ;
lock = ( lock = = null ? newLock : lock ) ;
}
return lock ;
}
@Override
public void reset ( String name ) {
if ( ! name . startsWith ( "meter" ) ) {
@ -153,7 +150,7 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
@@ -153,7 +150,7 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
* /
private final static class SimpleGauge implements Gauge < Double > {
private final double value ;
private volatile double value ;
private SimpleGauge ( double value ) {
this . value = value ;
@ -163,6 +160,10 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
@@ -163,6 +160,10 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
public Double getValue ( ) {
return this . value ;
}
public void setValue ( final double value ) {
this . value = value ;
}
}
}