Commit ee9d86cb authored by Stephane Nicoll's avatar Stephane Nicoll

Merge branch '1.5.x'

parents 576d5dd5 8b705571
...@@ -16,10 +16,7 @@ ...@@ -16,10 +16,7 @@
package org.springframework.boot.actuate.metrics.repository; package org.springframework.boot.actuate.metrics.repository;
import java.util.Collection;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentNavigableMap;
import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.Metric;
...@@ -28,17 +25,15 @@ import org.springframework.boot.actuate.metrics.util.SimpleInMemoryRepository.Ca ...@@ -28,17 +25,15 @@ import org.springframework.boot.actuate.metrics.util.SimpleInMemoryRepository.Ca
import org.springframework.boot.actuate.metrics.writer.Delta; import org.springframework.boot.actuate.metrics.writer.Delta;
/** /**
* {@link MetricRepository} and {@link MultiMetricRepository} implementation that stores * {@link MetricRepository} implementation that stores metrics in memory.
* metrics in memory.
* *
* @author Dave Syer * @author Dave Syer
* @author Stephane Nicoll
*/ */
public class InMemoryMetricRepository implements MetricRepository, MultiMetricRepository { public class InMemoryMetricRepository implements MetricRepository {
private final SimpleInMemoryRepository<Metric<?>> metrics = new SimpleInMemoryRepository<Metric<?>>(); private final SimpleInMemoryRepository<Metric<?>> metrics = new SimpleInMemoryRepository<Metric<?>>();
private final Collection<String> groups = new HashSet<String>();
public void setValues(ConcurrentNavigableMap<String, Metric<?>> values) { public void setValues(ConcurrentNavigableMap<String, Metric<?>> values) {
this.metrics.setValues(values); this.metrics.setValues(values);
} }
...@@ -52,12 +47,11 @@ public class InMemoryMetricRepository implements MetricRepository, MultiMetricRe ...@@ -52,12 +47,11 @@ public class InMemoryMetricRepository implements MetricRepository, MultiMetricRe
@Override @Override
public Metric<?> modify(Metric<?> current) { public Metric<?> modify(Metric<?> current) {
if (current != null) { if (current != null) {
Metric<? extends Number> metric = current;
return new Metric<Long>(metricName, return new Metric<Long>(metricName,
metric.increment(amount).getValue(), timestamp); current.increment(amount).getValue(), timestamp);
} }
else { else {
return new Metric<Long>(metricName, Long.valueOf(amount), timestamp); return new Metric<Long>(metricName, (long) amount, timestamp);
} }
} }
}); });
...@@ -68,51 +62,11 @@ public class InMemoryMetricRepository implements MetricRepository, MultiMetricRe ...@@ -68,51 +62,11 @@ public class InMemoryMetricRepository implements MetricRepository, MultiMetricRe
this.metrics.set(value.getName(), value); this.metrics.set(value.getName(), value);
} }
@Override
public void set(String group, Collection<Metric<?>> values) {
String prefix = group;
if (!prefix.endsWith(".")) {
prefix = prefix + ".";
}
for (Metric<?> metric : values) {
if (!metric.getName().startsWith(prefix)) {
metric = new Metric<Number>(prefix + metric.getName(), metric.getValue(),
metric.getTimestamp());
}
set(metric);
}
this.groups.add(group);
}
@Override
public void increment(String group, Delta<?> delta) {
String prefix = group;
if (!prefix.endsWith(".")) {
prefix = prefix + ".";
}
if (!delta.getName().startsWith(prefix)) {
delta = new Delta<Number>(prefix + delta.getName(), delta.getValue(),
delta.getTimestamp());
}
increment(delta);
this.groups.add(group);
}
@Override
public Iterable<String> groups() {
return Collections.unmodifiableCollection(this.groups);
}
@Override @Override
public long count() { public long count() {
return this.metrics.count(); return this.metrics.count();
} }
@Override
public long countGroups() {
return this.groups.size();
}
@Override @Override
public void reset(String metricName) { public void reset(String metricName) {
this.metrics.remove(metricName); this.metrics.remove(metricName);
...@@ -128,9 +82,8 @@ public class InMemoryMetricRepository implements MetricRepository, MultiMetricRe ...@@ -128,9 +82,8 @@ public class InMemoryMetricRepository implements MetricRepository, MultiMetricRe
return this.metrics.findAll(); return this.metrics.findAll();
} }
@Override public Iterable<Metric<?>> findAllWithPrefix(String prefix) {
public Iterable<Metric<?>> findAll(String metricNamePrefix) { return this.metrics.findAllWithPrefix(prefix);
return this.metrics.findAllWithPrefix(metricNamePrefix);
} }
} }
/*
* Copyright 2012-2016 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.repository;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.writer.Delta;
/**
* {@link MultiMetricRepository} implementation backed by a
* {@link InMemoryMetricRepository}.
*
* @author Stephane Nicoll
* @since 1.5.0
*/
public class InMemoryMultiMetricRepository implements MultiMetricRepository {
private final InMemoryMetricRepository repository;
private final Collection<String> groups = new HashSet<String>();
public InMemoryMultiMetricRepository(InMemoryMetricRepository repository) {
this.repository = repository;
}
public InMemoryMultiMetricRepository() {
this(new InMemoryMetricRepository());
}
@Override
public void set(String group, Collection<Metric<?>> values) {
String prefix = group;
if (!prefix.endsWith(".")) {
prefix = prefix + ".";
}
for (Metric<?> metric : values) {
if (!metric.getName().startsWith(prefix)) {
metric = new Metric<Number>(prefix + metric.getName(), metric.getValue(),
metric.getTimestamp());
}
this.repository.set(metric);
}
this.groups.add(group);
}
@Override
public void increment(String group, Delta<?> delta) {
String prefix = group;
if (!prefix.endsWith(".")) {
prefix = prefix + ".";
}
if (!delta.getName().startsWith(prefix)) {
delta = new Delta<Number>(prefix + delta.getName(), delta.getValue(),
delta.getTimestamp());
}
this.repository.increment(delta);
this.groups.add(group);
}
@Override
public Iterable<String> groups() {
return Collections.unmodifiableCollection(this.groups);
}
@Override
public long countGroups() {
return this.groups.size();
}
@Override
public void reset(String group) {
for (Metric<?> metric : findAll(group)) {
this.repository.reset(metric.getName());
}
this.groups.remove(group);
}
@Override
public Iterable<Metric<?>> findAll(String metricNamePrefix) {
return this.repository.findAllWithPrefix(metricNamePrefix);
}
}
...@@ -23,7 +23,7 @@ import org.junit.Test; ...@@ -23,7 +23,7 @@ import org.junit.Test;
import org.springframework.boot.actuate.metrics.Iterables; import org.springframework.boot.actuate.metrics.Iterables;
import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository; import org.springframework.boot.actuate.metrics.repository.InMemoryMultiMetricRepository;
import org.springframework.boot.actuate.metrics.writer.Delta; import org.springframework.boot.actuate.metrics.writer.Delta;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -35,17 +35,17 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -35,17 +35,17 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class PrefixMetricGroupExporterTests { public class PrefixMetricGroupExporterTests {
private final InMemoryMetricRepository reader = new InMemoryMetricRepository(); private final InMemoryMultiMetricRepository reader = new InMemoryMultiMetricRepository();
private final InMemoryMetricRepository writer = new InMemoryMetricRepository(); private final InMemoryMultiMetricRepository writer = new InMemoryMultiMetricRepository();
private final PrefixMetricGroupExporter exporter = new PrefixMetricGroupExporter( private final PrefixMetricGroupExporter exporter = new PrefixMetricGroupExporter(
this.reader, this.writer); this.reader, this.writer);
@Test @Test
public void prefixedMetricsCopied() { public void prefixedMetricsCopied() {
this.reader.set(new Metric<Number>("foo.bar", 2.3)); this.reader.set("foo", Arrays.<Metric<?>>asList(new Metric<Number>("bar", 2.3),
this.reader.set(new Metric<Number>("foo.spam", 1.3)); new Metric<Number>("spam", 1.3)));
this.exporter.setGroups(Collections.singleton("foo")); this.exporter.setGroups(Collections.singleton("foo"));
this.exporter.export(); this.exporter.export();
assertThat(Iterables.collection(this.writer.groups())).hasSize(1); assertThat(Iterables.collection(this.writer.groups())).hasSize(1);
...@@ -54,7 +54,8 @@ public class PrefixMetricGroupExporterTests { ...@@ -54,7 +54,8 @@ public class PrefixMetricGroupExporterTests {
@Test @Test
public void countersIncremented() { public void countersIncremented() {
this.writer.increment("counter.foo", new Delta<Long>("bar", 1L)); this.writer.increment("counter.foo", new Delta<Long>("bar", 1L));
this.reader.set(new Metric<Number>("counter.foo.bar", 1)); this.reader.set("counter", Collections.<Metric<?>>singletonList(
new Metric<Number>("counter.foo.bar", 1)));
this.exporter.setGroups(Collections.singleton("counter.foo")); this.exporter.setGroups(Collections.singleton("counter.foo"));
this.exporter.export(); this.exporter.export();
assertThat(this.writer.findAll("counter.foo").iterator().next().getValue()) assertThat(this.writer.findAll("counter.foo").iterator().next().getValue())
...@@ -63,8 +64,9 @@ public class PrefixMetricGroupExporterTests { ...@@ -63,8 +64,9 @@ public class PrefixMetricGroupExporterTests {
@Test @Test
public void unprefixedMetricsNotCopied() { public void unprefixedMetricsNotCopied() {
this.reader.set(new Metric<Number>("foo.bar", 2.3)); this.reader.set("foo", Arrays.<Metric<?>>asList(
this.reader.set(new Metric<Number>("foo.spam", 1.3)); new Metric<Number>("foo.bar", 2.3),
new Metric<Number>("foo.spam", 1.3)));
this.exporter.setGroups(Collections.singleton("bar")); this.exporter.setGroups(Collections.singleton("bar"));
this.exporter.export(); this.exporter.export();
assertThat(Iterables.collection(this.writer.groups())).isEmpty(); assertThat(Iterables.collection(this.writer.groups())).isEmpty();
...@@ -81,9 +83,11 @@ public class PrefixMetricGroupExporterTests { ...@@ -81,9 +83,11 @@ public class PrefixMetricGroupExporterTests {
@Test @Test
public void onlyPrefixedMetricsCopied() { public void onlyPrefixedMetricsCopied() {
this.reader.set(new Metric<Number>("foo.bar", 2.3)); this.reader.set("foo", Arrays.<Metric<?>>asList(
this.reader.set(new Metric<Number>("foo.spam", 1.3)); new Metric<Number>("foo.bar", 2.3),
this.reader.set(new Metric<Number>("foobar.spam", 1.3)); new Metric<Number>("foo.spam", 1.3)));
this.reader.set("foobar", Collections.<Metric<?>>singletonList(
new Metric<Number>("foobar.spam", 1.3)));
this.exporter.setGroups(Collections.singleton("foo")); this.exporter.setGroups(Collections.singleton("foo"));
this.exporter.export(); this.exporter.export();
assertThat(Iterables.collection(this.writer.groups())).hasSize(1); assertThat(Iterables.collection(this.writer.groups())).hasSize(1);
......
...@@ -20,7 +20,7 @@ import org.junit.Test; ...@@ -20,7 +20,7 @@ import org.junit.Test;
import org.springframework.boot.actuate.metrics.Iterables; import org.springframework.boot.actuate.metrics.Iterables;
import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository; import org.springframework.boot.actuate.metrics.repository.InMemoryMultiMetricRepository;
import org.springframework.boot.actuate.metrics.rich.InMemoryRichGaugeRepository; import org.springframework.boot.actuate.metrics.rich.InMemoryRichGaugeRepository;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -34,7 +34,7 @@ public class RichGaugeExporterTests { ...@@ -34,7 +34,7 @@ public class RichGaugeExporterTests {
private final InMemoryRichGaugeRepository reader = new InMemoryRichGaugeRepository(); private final InMemoryRichGaugeRepository reader = new InMemoryRichGaugeRepository();
private final InMemoryMetricRepository writer = new InMemoryMetricRepository(); private final InMemoryMultiMetricRepository writer = new InMemoryMultiMetricRepository();
private final RichGaugeExporter exporter = new RichGaugeExporter(this.reader, private final RichGaugeExporter exporter = new RichGaugeExporter(this.reader,
this.writer); this.writer);
......
...@@ -16,7 +16,9 @@ ...@@ -16,7 +16,9 @@
package org.springframework.boot.actuate.metrics.repository; package org.springframework.boot.actuate.metrics.repository;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.junit.Test; import org.junit.Test;
...@@ -29,15 +31,16 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -29,15 +31,16 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* @author Dave Syer * @author Dave Syer
*/ */
public class InMemoryPrefixMetricRepositoryTests { public class InMemoryMultiMetricRepositoryTests {
private final InMemoryMetricRepository repository = new InMemoryMetricRepository(); private final InMemoryMultiMetricRepository repository =
new InMemoryMultiMetricRepository();
@Test @Test
public void registeredPrefixCounted() { public void registeredPrefixCounted() {
this.repository.increment(new Delta<Number>("foo.bar", 1)); this.repository.increment("foo", new Delta<Number>("bar", 1));
this.repository.increment(new Delta<Number>("foo.bar", 1)); this.repository.increment("foo", new Delta<Number>("bar", 1));
this.repository.increment(new Delta<Number>("foo.spam", 1)); this.repository.increment("foo", new Delta<Number>("spam", 1));
Set<String> names = new HashSet<String>(); Set<String> names = new HashSet<String>();
for (Metric<?> metric : this.repository.findAll("foo")) { for (Metric<?> metric : this.repository.findAll("foo")) {
names.add(metric.getName()); names.add(metric.getName());
...@@ -48,7 +51,7 @@ public class InMemoryPrefixMetricRepositoryTests { ...@@ -48,7 +51,7 @@ public class InMemoryPrefixMetricRepositoryTests {
@Test @Test
public void prefixWithWildcard() { public void prefixWithWildcard() {
this.repository.increment(new Delta<Number>("foo.bar", 1)); this.repository.increment("foo", new Delta<Number>("bar", 1));
Set<String> names = new HashSet<String>(); Set<String> names = new HashSet<String>();
for (Metric<?> metric : this.repository.findAll("foo.*")) { for (Metric<?> metric : this.repository.findAll("foo.*")) {
names.add(metric.getName()); names.add(metric.getName());
...@@ -59,7 +62,7 @@ public class InMemoryPrefixMetricRepositoryTests { ...@@ -59,7 +62,7 @@ public class InMemoryPrefixMetricRepositoryTests {
@Test @Test
public void prefixWithPeriod() { public void prefixWithPeriod() {
this.repository.increment(new Delta<Number>("foo.bar", 1)); this.repository.increment("foo", new Delta<Number>("bar", 1));
Set<String> names = new HashSet<String>(); Set<String> names = new HashSet<String>();
for (Metric<?> metric : this.repository.findAll("foo.")) { for (Metric<?> metric : this.repository.findAll("foo.")) {
names.add(metric.getName()); names.add(metric.getName());
...@@ -70,8 +73,8 @@ public class InMemoryPrefixMetricRepositoryTests { ...@@ -70,8 +73,8 @@ public class InMemoryPrefixMetricRepositoryTests {
@Test @Test
public void onlyRegisteredPrefixCounted() { public void onlyRegisteredPrefixCounted() {
this.repository.increment(new Delta<Number>("foo.bar", 1)); this.repository.increment("foo", new Delta<Number>("bar", 1));
this.repository.increment(new Delta<Number>("foobar.spam", 1)); this.repository.increment("foobar", new Delta<Number>("spam", 1));
Set<String> names = new HashSet<String>(); Set<String> names = new HashSet<String>();
for (Metric<?> metric : this.repository.findAll("foo")) { for (Metric<?> metric : this.repository.findAll("foo")) {
names.add(metric.getName()); names.add(metric.getName());
...@@ -85,13 +88,13 @@ public class InMemoryPrefixMetricRepositoryTests { ...@@ -85,13 +88,13 @@ public class InMemoryPrefixMetricRepositoryTests {
this.repository.increment("foo", new Delta<Number>("foo.bar", 1)); this.repository.increment("foo", new Delta<Number>("foo.bar", 1));
this.repository.increment("foo", new Delta<Number>("foo.bar", 2)); this.repository.increment("foo", new Delta<Number>("foo.bar", 2));
this.repository.increment("foo", new Delta<Number>("foo.spam", 1)); this.repository.increment("foo", new Delta<Number>("foo.spam", 1));
Set<String> names = new HashSet<String>(); Map<String, Metric<?>> metrics = new HashMap<String, Metric<?>>();
for (Metric<?> metric : this.repository.findAll("foo")) { for (Metric<?> metric : this.repository.findAll("foo")) {
names.add(metric.getName()); metrics.put(metric.getName(), metric);
} }
assertThat(names).hasSize(2); assertThat(metrics).hasSize(2);
assertThat(names.contains("foo.bar")).isTrue(); assertThat(metrics).containsKeys("foo.bar", "foo.spam");
assertThat(this.repository.findOne("foo.bar").getValue()).isEqualTo(3L); assertThat(metrics.get("foo.bar").getValue()).isEqualTo(3L);
} }
} }
...@@ -20,7 +20,7 @@ import org.junit.Test; ...@@ -20,7 +20,7 @@ import org.junit.Test;
import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.export.RichGaugeExporter; import org.springframework.boot.actuate.metrics.export.RichGaugeExporter;
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository; import org.springframework.boot.actuate.metrics.repository.InMemoryMultiMetricRepository;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class MultiMetricRichGaugeReaderTests { public class MultiMetricRichGaugeReaderTests {
private InMemoryMetricRepository repository = new InMemoryMetricRepository(); private InMemoryMultiMetricRepository repository = new InMemoryMultiMetricRepository();
private MultiMetricRichGaugeReader reader = new MultiMetricRichGaugeReader( private MultiMetricRichGaugeReader reader = new MultiMetricRichGaugeReader(
this.repository); this.repository);
...@@ -47,7 +47,7 @@ public class MultiMetricRichGaugeReaderTests { ...@@ -47,7 +47,7 @@ public class MultiMetricRichGaugeReaderTests {
this.data.set(new Metric<Integer>("foo", 1)); this.data.set(new Metric<Integer>("foo", 1));
this.exporter.export(); this.exporter.export();
// Check the exporter worked // Check the exporter worked
assertThat(this.repository.count()).isEqualTo(6); assertThat(this.repository.countGroups()).isEqualTo(1);
assertThat(this.reader.count()).isEqualTo(1); assertThat(this.reader.count()).isEqualTo(1);
RichGauge one = this.reader.findOne("foo"); RichGauge one = this.reader.findOne("foo");
assertThat(one).isNotNull(); assertThat(one).isNotNull();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment