Add a PrefixMetricWriter interface to cleanly separate write/read
The PrefixMetricGroupExporter only really makes sesne if the writer is aware of the groups, so it seemed better to use a new interface than mix read/write.
This commit is contained in:
parent
94e891e924
commit
ed2876e931
|
@ -81,7 +81,9 @@ public abstract class AbstractMetricExporter implements Exporter {
|
||||||
}
|
}
|
||||||
values.add(value);
|
values.add(value);
|
||||||
}
|
}
|
||||||
write(group, values);
|
if (!values.isEmpty()) {
|
||||||
|
write(group, values);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
|
|
|
@ -23,7 +23,7 @@ import java.util.Set;
|
||||||
import org.springframework.boot.actuate.metrics.Metric;
|
import org.springframework.boot.actuate.metrics.Metric;
|
||||||
import org.springframework.boot.actuate.metrics.reader.PrefixMetricReader;
|
import org.springframework.boot.actuate.metrics.reader.PrefixMetricReader;
|
||||||
import org.springframework.boot.actuate.metrics.repository.MultiMetricRepository;
|
import org.springframework.boot.actuate.metrics.repository.MultiMetricRepository;
|
||||||
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
|
import org.springframework.boot.actuate.metrics.writer.PrefixMetricWriter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A convenient exporter for a group of metrics from a {@link PrefixMetricReader}. Exports
|
* A convenient exporter for a group of metrics from a {@link PrefixMetricReader}. Exports
|
||||||
|
@ -35,7 +35,7 @@ public class PrefixMetricGroupExporter extends AbstractMetricExporter {
|
||||||
|
|
||||||
private final PrefixMetricReader reader;
|
private final PrefixMetricReader reader;
|
||||||
|
|
||||||
private final MetricWriter writer;
|
private final PrefixMetricWriter writer;
|
||||||
|
|
||||||
private Set<String> groups = new HashSet<String>();
|
private Set<String> groups = new HashSet<String>();
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ public class PrefixMetricGroupExporter extends AbstractMetricExporter {
|
||||||
* @param reader a reader as the source of metrics
|
* @param reader a reader as the source of metrics
|
||||||
* @param writer the writer to send the metrics to
|
* @param writer the writer to send the metrics to
|
||||||
*/
|
*/
|
||||||
public PrefixMetricGroupExporter(PrefixMetricReader reader, MetricWriter writer) {
|
public PrefixMetricGroupExporter(PrefixMetricReader reader, PrefixMetricWriter writer) {
|
||||||
this(reader, writer, "");
|
this(reader, writer, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,8 +56,8 @@ public class PrefixMetricGroupExporter extends AbstractMetricExporter {
|
||||||
* @param writer the writer to send the metrics to
|
* @param writer the writer to send the metrics to
|
||||||
* @param prefix the prefix for metrics to export
|
* @param prefix the prefix for metrics to export
|
||||||
*/
|
*/
|
||||||
public PrefixMetricGroupExporter(PrefixMetricReader reader, MetricWriter writer,
|
public PrefixMetricGroupExporter(PrefixMetricReader reader,
|
||||||
String prefix) {
|
PrefixMetricWriter writer, String prefix) {
|
||||||
super(prefix);
|
super(prefix);
|
||||||
this.reader = reader;
|
this.reader = reader;
|
||||||
this.writer = writer;
|
this.writer = writer;
|
||||||
|
@ -72,6 +72,9 @@ public class PrefixMetricGroupExporter extends AbstractMetricExporter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Iterable<String> groups() {
|
protected Iterable<String> groups() {
|
||||||
|
if ((this.reader instanceof MultiMetricRepository) && this.groups.isEmpty()) {
|
||||||
|
return ((MultiMetricRepository) this.reader).groups();
|
||||||
|
}
|
||||||
return this.groups;
|
return this.groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,14 +85,7 @@ public class PrefixMetricGroupExporter extends AbstractMetricExporter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void write(String group, Collection<Metric<?>> values) {
|
protected void write(String group, Collection<Metric<?>> values) {
|
||||||
if (this.writer instanceof MultiMetricRepository && !values.isEmpty()) {
|
this.writer.save(group, values);
|
||||||
((MultiMetricRepository) this.writer).save(group, values);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for (Metric<?> value : values) {
|
|
||||||
this.writer.set(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,7 +72,15 @@ public class InMemoryMetricRepository implements MetricRepository, MultiMetricRe
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(String group, Collection<Metric<?>> values) {
|
public void save(String group, Collection<Metric<?>> values) {
|
||||||
|
String prefix = group;
|
||||||
|
if (!prefix.endsWith(".")) {
|
||||||
|
prefix = prefix + ".";
|
||||||
|
}
|
||||||
for (Metric<?> metric : values) {
|
for (Metric<?> metric : values) {
|
||||||
|
if (!metric.getName().startsWith(prefix)) {
|
||||||
|
metric = new Metric<Number>(prefix + metric.getName(), metric.getValue(),
|
||||||
|
metric.getTimestamp());
|
||||||
|
}
|
||||||
set(metric);
|
set(metric);
|
||||||
}
|
}
|
||||||
this.groups.add(group);
|
this.groups.add(group);
|
||||||
|
|
|
@ -16,10 +16,8 @@
|
||||||
|
|
||||||
package org.springframework.boot.actuate.metrics.repository;
|
package org.springframework.boot.actuate.metrics.repository;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import org.springframework.boot.actuate.metrics.Metric;
|
|
||||||
import org.springframework.boot.actuate.metrics.reader.PrefixMetricReader;
|
import org.springframework.boot.actuate.metrics.reader.PrefixMetricReader;
|
||||||
|
import org.springframework.boot.actuate.metrics.writer.PrefixMetricWriter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A repository for metrics that allows efficient storage and retrieval of groups of
|
* A repository for metrics that allows efficient storage and retrieval of groups of
|
||||||
|
@ -27,21 +25,7 @@ import org.springframework.boot.actuate.metrics.reader.PrefixMetricReader;
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
public interface MultiMetricRepository extends PrefixMetricReader {
|
public interface MultiMetricRepository extends PrefixMetricReader, PrefixMetricWriter {
|
||||||
|
|
||||||
/**
|
|
||||||
* Save some metric values and associate them with a group name.
|
|
||||||
* @param group the name of the group
|
|
||||||
* @param values the metric values to save
|
|
||||||
*/
|
|
||||||
void save(String group, Collection<Metric<?>> values);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Rest the values of all metrics in the group. Implementations may choose to discard
|
|
||||||
* the old values.
|
|
||||||
* @param group reset the whole group
|
|
||||||
*/
|
|
||||||
void reset(String group);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The names of all the groups known to this repository
|
* The names of all the groups known to this repository
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class RedisMultiMetricRepository implements MultiMetricRepository {
|
||||||
|
|
||||||
private String prefix = DEFAULT_METRICS_PREFIX;
|
private String prefix = DEFAULT_METRICS_PREFIX;
|
||||||
|
|
||||||
private String keys = this.prefix + "keys";
|
private String keys = "keys." + this.prefix;
|
||||||
|
|
||||||
private final BoundZSetOperations<String, String> zSetOperations;
|
private final BoundZSetOperations<String, String> zSetOperations;
|
||||||
|
|
||||||
|
@ -61,8 +61,11 @@ public class RedisMultiMetricRepository implements MultiMetricRepository {
|
||||||
* @param prefix the prefix to set for all metrics keys
|
* @param prefix the prefix to set for all metrics keys
|
||||||
*/
|
*/
|
||||||
public void setPrefix(String prefix) {
|
public void setPrefix(String prefix) {
|
||||||
|
if (!prefix.endsWith(".")) {
|
||||||
|
prefix = prefix + ".";
|
||||||
|
}
|
||||||
this.prefix = prefix;
|
this.prefix = prefix;
|
||||||
this.keys = this.prefix + "keys";
|
this.keys = "keys." + this.prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2013 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.Collection;
|
||||||
|
|
||||||
|
import org.springframework.boot.actuate.metrics.Metric;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A writer for metrics that allows efficient storage of groups of metrics with a common
|
||||||
|
* name prefix (their group name).
|
||||||
|
*
|
||||||
|
* @author Dave Syer
|
||||||
|
*/
|
||||||
|
public interface PrefixMetricWriter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save some metric values and associate them with a group name.
|
||||||
|
* @param group the name of the group
|
||||||
|
* @param values the metric values to save
|
||||||
|
*/
|
||||||
|
void save(String group, Collection<Metric<?>> values);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rest the values of all metrics in the group. Implementations may choose to discard
|
||||||
|
* the old values.
|
||||||
|
* @param group reset the whole group
|
||||||
|
*/
|
||||||
|
void reset(String group);
|
||||||
|
|
||||||
|
}
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.actuate.metrics.export;
|
package org.springframework.boot.actuate.metrics.export;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -57,6 +58,15 @@ public class PrefixMetricGroupExporterTests {
|
||||||
assertEquals(0, Iterables.collection(this.writer.groups()).size());
|
assertEquals(0, Iterables.collection(this.writer.groups()).size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void multiMetricGroupsCopiedAsDefault() {
|
||||||
|
this.reader.save("foo", Arrays.<Metric<?>> asList(new Metric<Number>("bar", 2.3),
|
||||||
|
new Metric<Number>("spam", 1.3)));
|
||||||
|
this.exporter.export();
|
||||||
|
assertEquals(1, this.writer.countGroups());
|
||||||
|
assertEquals(2, Iterables.collection(this.writer.findAll("foo")).size());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onlyPrefixedMetricsCopied() {
|
public void onlyPrefixedMetricsCopied() {
|
||||||
this.reader.set(new Metric<Number>("foo.bar", 2.3));
|
this.reader.set(new Metric<Number>("foo.bar", 2.3));
|
||||||
|
|
Loading…
Reference in New Issue