Merge branch 'gh-3977'

pull/3918/merge
Andy Wilkinson 9 years ago
commit 968caf05b6

@ -46,12 +46,14 @@ import com.codahale.metrics.Timer;
* </ul> * </ul>
* *
* @author Dave Syer * @author Dave Syer
* @author Jay Anderson
* @author Andy Wilkinson
*/ */
public class DropwizardMetricServices implements CounterService, GaugeService { public class DropwizardMetricServices implements CounterService, GaugeService {
private final MetricRegistry registry; private final MetricRegistry registry;
private final ConcurrentMap<String, Object> gaugeLocks = new ConcurrentHashMap<String, Object>(); private final ConcurrentMap<String, SimpleGauge> gauges = new ConcurrentHashMap<String, SimpleGauge>();
private final ConcurrentHashMap<String, String> names = new ConcurrentHashMap<String, String>(); private final ConcurrentHashMap<String, String> names = new ConcurrentHashMap<String, String>();
@ -99,15 +101,22 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
} }
else { else {
name = wrapGaugeName(name); name = wrapGaugeName(name);
final double gauge = value; setGaugeValue(name, 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)) { private void setGaugeValue(String name, double value) {
this.registry.remove(name); // NOTE: Dropwizard provides no way to do this atomically
this.registry.register(name, new SimpleGauge(gauge)); SimpleGauge gauge = this.gauges.get(name);
if (gauge == null) {
SimpleGauge newGauge = new SimpleGauge(value);
gauge = this.gauges.putIfAbsent(name, newGauge);
if (gauge == null) {
this.registry.register(name, newGauge);
return;
} }
} }
gauge.setValue(value);
} }
private String wrapGaugeName(String metricName) { private String wrapGaugeName(String metricName) {
@ -130,16 +139,6 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
return name; 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 @Override
public void reset(String name) { public void reset(String name) {
if (!name.startsWith("meter")) { if (!name.startsWith("meter")) {
@ -153,7 +152,7 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
*/ */
private final static class SimpleGauge implements Gauge<Double> { private final static class SimpleGauge implements Gauge<Double> {
private final double value; private volatile double value;
private SimpleGauge(double value) { private SimpleGauge(double value) {
this.value = value; this.value = value;
@ -163,6 +162,10 @@ public class DropwizardMetricServices implements CounterService, GaugeService {
public Double getValue() { public Double getValue() {
return this.value; return this.value;
} }
public void setValue(double value) {
this.value = value;
}
} }
} }

@ -65,9 +65,10 @@ public class DropwizardMetricServicesTests {
@Test @Test
public void setGauge() { public void setGauge() {
this.writer.submit("foo", 2.1); this.writer.submit("foo", 2.1);
this.writer.submit("foo", 2.3);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Gauge<Double> gauge = (Gauge<Double>) this.registry.getMetrics().get("gauge.foo"); Gauge<Double> gauge = (Gauge<Double>) this.registry.getMetrics().get("gauge.foo");
assertEquals(new Double(2.1), gauge.getValue());
this.writer.submit("foo", 2.3);
assertEquals(new Double(2.3), gauge.getValue()); assertEquals(new Double(2.3), gauge.getValue());
} }

Loading…
Cancel
Save