diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricRepositoryAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricRepositoryAutoConfiguration.java index 5e368ce9c05..b3e8a4ab816 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricRepositoryAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricRepositoryAutoConfiguration.java @@ -29,10 +29,10 @@ import org.springframework.boot.actuate.metrics.export.Exporter; import org.springframework.boot.actuate.metrics.reader.MetricRegistryMetricReader; import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository; import org.springframework.boot.actuate.metrics.repository.MetricRepository; -import org.springframework.boot.actuate.metrics.writer.CodahaleMetricWriter; import org.springframework.boot.actuate.metrics.writer.CompositeMetricWriter; import org.springframework.boot.actuate.metrics.writer.DefaultCounterService; import org.springframework.boot.actuate.metrics.writer.DefaultGaugeService; +import org.springframework.boot.actuate.metrics.writer.DropwizardMetricWriter; import org.springframework.boot.actuate.metrics.writer.MessageChannelMetricWriter; import org.springframework.boot.actuate.metrics.writer.MetricWriter; import org.springframework.boot.actuate.metrics.writer.MetricWriterMessageHandler; @@ -83,7 +83,7 @@ import com.codahale.metrics.MetricRegistry; * @see CounterService * @see MetricWriter * @see InMemoryMetricRepository - * @see CodahaleMetricWriter + * @see DropwizardMetricWriter * @see Exporter * * @author Dave Syer @@ -153,7 +153,7 @@ public class MetricRepositoryAutoConfiguration { @Configuration @ConditionalOnClass(MetricRegistry.class) - static class CodahaleMetricRegistryConfiguration { + static class DropwizardMetricRegistryConfiguration { @Bean @ConditionalOnMissingBean @@ -162,8 +162,8 @@ public class MetricRepositoryAutoConfiguration { } @Bean - public CodahaleMetricWriter codahaleMetricWriter(MetricRegistry metricRegistry) { - return new CodahaleMetricWriter(metricRegistry); + public DropwizardMetricWriter dropwizardMetricWriter(MetricRegistry metricRegistry) { + return new DropwizardMetricWriter(metricRegistry); } @Bean @@ -175,7 +175,7 @@ public class MetricRepositoryAutoConfiguration { } @Bean - public PublicMetrics codahalePublicMetrics(MetricRegistry metricRegistry) { + public PublicMetrics dropwizardPublicMetrics(MetricRegistry metricRegistry) { MetricRegistryMetricReader reader = new MetricRegistryMetricReader( metricRegistry); return new MetricReaderPublicMetrics(reader); diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/CodahaleMetricWriter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/CodahaleMetricWriter.java index 17e44bec57c..f93c29d4f35 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/CodahaleMetricWriter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/CodahaleMetricWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,6 @@ package org.springframework.boot.actuate.metrics.writer; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.TimeUnit; - import org.springframework.boot.actuate.metrics.Metric; import com.codahale.metrics.Counter; @@ -46,90 +42,17 @@ import com.codahale.metrics.Timer; * * * @author Dave Syer + * @deprecated since 1.2.2 in favor of {@link DropwizardMetricWriter} */ -public class CodahaleMetricWriter implements MetricWriter { - - private final MetricRegistry registry; - - private final ConcurrentMap gaugeLocks = new ConcurrentHashMap(); +@Deprecated +public class CodahaleMetricWriter extends DropwizardMetricWriter { /** - * Create a new {@link CodahaleMetricWriter} instance. + * Create a new {@link DropwizardMetricWriter} instance. * @param registry the underlying metric registry */ public CodahaleMetricWriter(MetricRegistry registry) { - this.registry = registry; - } - - @Override - public void increment(Delta delta) { - String name = delta.getName(); - long value = delta.getValue().longValue(); - if (name.startsWith("meter")) { - Meter meter = this.registry.meter(name); - meter.mark(value); - } - else { - Counter counter = this.registry.counter(name); - counter.inc(value); - } - } - - @Override - public void set(Metric value) { - String name = value.getName(); - if (name.startsWith("histogram")) { - long longValue = value.getValue().longValue(); - Histogram metric = this.registry.histogram(name); - metric.update(longValue); - } - else if (name.startsWith("timer")) { - long longValue = value.getValue().longValue(); - Timer metric = this.registry.timer(name); - metric.update(longValue, TimeUnit.MILLISECONDS); - } - else { - final double gauge = value.getValue().doubleValue(); - // Ensure we synchronize to avoid another thread pre-empting this thread after - // remove causing an error in CodaHale metrics - // NOTE: CodaHale provides no way to do this atomically - synchronized (getGuageLock(name)) { - this.registry.remove(name); - this.registry.register(name, new SimpleGauge(gauge)); - } - } - } - - private Object getGuageLock(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 metricName) { - this.registry.remove(metricName); - } - - /** - * Simple {@link Gauge} implementation to {@literal double} value. - */ - private static class SimpleGauge implements Gauge { - - private final double value; - - private SimpleGauge(double value) { - this.value = value; - } - - @Override - public Double getValue() { - return this.value; - } + super(registry); } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/DropwizardMetricWriter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/DropwizardMetricWriter.java new file mode 100644 index 00000000000..e3045976ace --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/DropwizardMetricWriter.java @@ -0,0 +1,135 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.metrics.writer; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.TimeUnit; + +import org.springframework.boot.actuate.metrics.Metric; + +import com.codahale.metrics.Counter; +import com.codahale.metrics.Gauge; +import com.codahale.metrics.Histogram; +import com.codahale.metrics.Meter; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; + +/** + * A {@link MetricWriter} that send data to a Codahale {@link MetricRegistry} based on a + * naming convention: + * + *
    + *
  • Updates to {@link #increment(Delta)} with names in "meter.*" are treated as + * {@link Meter} events
  • + *
  • Other deltas are treated as simple {@link Counter} values
  • + *
  • Inputs to {@link #set(Metric)} with names in "histogram.*" are treated as + * {@link Histogram} updates
  • + *
  • Inputs to {@link #set(Metric)} with names in "timer.*" are treated as {@link Timer} + * updates
  • + *
  • Other metrics are treated as simple {@link Gauge} values (single valued + * measurements of type double)
  • + *
+ * + * @author Dave Syer + */ +public class DropwizardMetricWriter implements MetricWriter { + + private final MetricRegistry registry; + + private final ConcurrentMap gaugeLocks = new ConcurrentHashMap(); + + /** + * Create a new {@link DropwizardMetricWriter} instance. + * @param registry the underlying metric registry + */ + public DropwizardMetricWriter(MetricRegistry registry) { + this.registry = registry; + } + + @Override + public void increment(Delta delta) { + String name = delta.getName(); + long value = delta.getValue().longValue(); + if (name.startsWith("meter")) { + Meter meter = this.registry.meter(name); + meter.mark(value); + } + else { + Counter counter = this.registry.counter(name); + counter.inc(value); + } + } + + @Override + public void set(Metric value) { + String name = value.getName(); + if (name.startsWith("histogram")) { + long longValue = value.getValue().longValue(); + Histogram metric = this.registry.histogram(name); + metric.update(longValue); + } + else if (name.startsWith("timer")) { + long longValue = value.getValue().longValue(); + Timer metric = this.registry.timer(name); + metric.update(longValue, TimeUnit.MILLISECONDS); + } + else { + final double gauge = value.getValue().doubleValue(); + // Ensure we synchronize to avoid another thread pre-empting this thread after + // remove causing an error in CodaHale metrics + // NOTE: CodaHale provides no way to do this atomically + synchronized (getGuageLock(name)) { + this.registry.remove(name); + this.registry.register(name, new SimpleGauge(gauge)); + } + } + } + + private Object getGuageLock(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 metricName) { + this.registry.remove(metricName); + } + + /** + * Simple {@link Gauge} implementation to {@literal double} value. + */ + private static class SimpleGauge implements Gauge { + + private final double value; + + private SimpleGauge(double value) { + this.value = value; + } + + @Override + public Double getValue() { + return this.value; + } + } + +} diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/writer/CodahaleMetricWriterTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/writer/DropwizardMetricWriterTests.java similarity index 93% rename from spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/writer/CodahaleMetricWriterTests.java rename to spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/writer/DropwizardMetricWriterTests.java index 51d4e40c032..896fee979af 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/writer/CodahaleMetricWriterTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/writer/DropwizardMetricWriterTests.java @@ -29,12 +29,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; /** + * Tests for {@link DropwizardMetricWriter}. + * * @author Dave Syer */ -public class CodahaleMetricWriterTests { +public class DropwizardMetricWriterTests { private final MetricRegistry registry = new MetricRegistry(); - private final CodahaleMetricWriter writer = new CodahaleMetricWriter(this.registry); + private final DropwizardMetricWriter writer = new DropwizardMetricWriter(this.registry); @Test public void incrementCounter() { @@ -110,9 +112,9 @@ public class CodahaleMetricWriterTests { public static class WriterThread extends Thread { private int index; private boolean failed; - private CodahaleMetricWriter writer; + private DropwizardMetricWriter writer; - public WriterThread(ThreadGroup group, int index, CodahaleMetricWriter writer) { + public WriterThread(ThreadGroup group, int index, DropwizardMetricWriter writer) { super(group, "Writer-" + index); this.index = index;