Add support for custom Exporter with declarative schedule

pull/3473/head
Dave Syer 9 years ago
parent 58509ed7c5
commit 82da28f627

@ -24,6 +24,7 @@ import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.endpoint.MetricsEndpointMetricReader;
import org.springframework.boot.actuate.metrics.export.Exporter;
import org.springframework.boot.actuate.metrics.export.MetricExportProperties;
import org.springframework.boot.actuate.metrics.export.MetricExporters;
import org.springframework.boot.actuate.metrics.reader.CompositeMetricReader;
@ -66,6 +67,9 @@ public class MetricExportAutoConfiguration {
@ExportMetricWriter
private Map<String, MetricWriter> writers = Collections.emptyMap();
@Autowired(required = false)
private Map<String, Exporter> exporters = Collections.emptyMap();
@Bean
@ConditionalOnMissingBean(name = "metricWritersMetricExporter")
public SchedulingConfigurer metricWritersMetricExporter() {
@ -75,9 +79,21 @@ public class MetricExportAutoConfiguration {
reader = new CompositeMetricReader(
this.readers.toArray(new MetricReader[this.readers.size()]));
}
MetricExporters exporters = null;
if (reader != null) {
exporters = new MetricExporters(this.properties);
writers.putAll(this.writers);
return new MetricExporters(reader, writers, this.properties);
exporters.setReader(reader);
exporters.setWriters(writers);
}
if (!this.exporters.isEmpty()) {
if (exporters==null) {
exporters = new MetricExporters(this.properties);
}
exporters.setExporters(this.exporters);
}
if (exporters!=null) {
return exporters;
}
return new NoOpSchedulingConfigurer();
}

@ -34,23 +34,43 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
*/
public class MetricExporters implements SchedulingConfigurer {
private final MetricReader reader;
private MetricReader reader;
private final Map<String, MetricWriter> writers;
private Map<String, MetricWriter> writers = new HashMap<String, MetricWriter>();
private final MetricExportProperties properties;
private final Map<String, Exporter> exporters = new HashMap<String, Exporter>();
public MetricExporters(MetricReader reader, Map<String, MetricWriter> writers,
MetricExportProperties properties) {
this.reader = reader;
this.writers = writers;
public MetricExporters(MetricExportProperties properties) {
this.properties = properties;
}
public void setReader(MetricReader reader) {
this.reader = reader;
}
public void setWriters(Map<String, MetricWriter> writers) {
this.writers.putAll(writers);
}
public void setExporters(Map<String, Exporter> exporters) {
this.exporters.putAll(exporters);
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
for (Entry<String, Exporter> entry : this.exporters.entrySet()) {
String name = entry.getKey();
Exporter exporter = entry.getValue();
TriggerProperties trigger = this.properties.findTrigger(name);
if (trigger != null) {
ExportRunner runner = new ExportRunner(exporter);
IntervalTask task = new IntervalTask(runner, trigger.getDelayMillis(),
trigger.getDelayMillis());
taskRegistrar.addFixedDelayTask(task);
}
}
for (Entry<String, MetricWriter> entry : this.writers.entrySet()) {
String name = entry.getKey();
MetricWriter writer = entry.getValue();
@ -80,9 +100,9 @@ public class MetricExporters implements SchedulingConfigurer {
private static class ExportRunner implements Runnable {
private final MetricCopyExporter exporter;
private final Exporter exporter;
public ExportRunner(MetricCopyExporter exporter) {
public ExportRunner(Exporter exporter) {
this.exporter = exporter;
}

@ -16,6 +16,10 @@
package org.springframework.boot.actuate.metrics.export;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
@ -25,9 +29,6 @@ import org.springframework.boot.actuate.metrics.reader.MetricReader;
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Tests for {@link MetricExporters}.
*
@ -47,7 +48,9 @@ public class MetricExportersTests {
@Test
public void emptyWriters() {
this.exporters = new MetricExporters(this.reader, this.writers, this.export);
this.exporters = new MetricExporters(this.export);
this.exporters.setReader(this.reader);
this.exporters.setWriters(this.writers);
this.exporters.configureTasks(new ScheduledTaskRegistrar());
assertNotNull(this.exporters.getExporters());
assertEquals(0, this.exporters.getExporters().size());
@ -57,7 +60,20 @@ public class MetricExportersTests {
public void oneWriter() {
this.export.setUpDefaults();
this.writers.put("foo", this.writer);
this.exporters = new MetricExporters(this.reader, this.writers, this.export);
this.exporters = new MetricExporters(this.export);
this.exporters.setReader(this.reader);
this.exporters.setWriters(this.writers);
this.exporters.configureTasks(new ScheduledTaskRegistrar());
assertNotNull(this.exporters.getExporters());
assertEquals(1, this.exporters.getExporters().size());
}
@Test
public void exporter() {
this.export.setUpDefaults();
this.exporters = new MetricExporters(this.export);
this.exporters.setExporters(Collections.<String, Exporter> singletonMap("foo",
new MetricCopyExporter(this.reader, this.writer)));
this.exporters.configureTasks(new ScheduledTaskRegistrar());
assertNotNull(this.exporters.getExporters());
assertEquals(1, this.exporters.getExporters().size());

Loading…
Cancel
Save