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

@ -34,23 +34,43 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
*/ */
public class MetricExporters implements SchedulingConfigurer { 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 MetricExportProperties properties;
private final Map<String, Exporter> exporters = new HashMap<String, Exporter>(); private final Map<String, Exporter> exporters = new HashMap<String, Exporter>();
public MetricExporters(MetricReader reader, Map<String, MetricWriter> writers, public MetricExporters(MetricExportProperties properties) {
MetricExportProperties properties) {
this.reader = reader;
this.writers = writers;
this.properties = 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 @Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { 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()) { for (Entry<String, MetricWriter> entry : this.writers.entrySet()) {
String name = entry.getKey(); String name = entry.getKey();
MetricWriter writer = entry.getValue(); MetricWriter writer = entry.getValue();
@ -80,9 +100,9 @@ public class MetricExporters implements SchedulingConfigurer {
private static class ExportRunner implements Runnable { 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; this.exporter = exporter;
} }

@ -16,6 +16,10 @@
package org.springframework.boot.actuate.metrics.export; 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.LinkedHashMap;
import java.util.Map; 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.boot.actuate.metrics.writer.MetricWriter;
import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/** /**
* Tests for {@link MetricExporters}. * Tests for {@link MetricExporters}.
* *
@ -47,7 +48,9 @@ public class MetricExportersTests {
@Test @Test
public void emptyWriters() { 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()); this.exporters.configureTasks(new ScheduledTaskRegistrar());
assertNotNull(this.exporters.getExporters()); assertNotNull(this.exporters.getExporters());
assertEquals(0, this.exporters.getExporters().size()); assertEquals(0, this.exporters.getExporters().size());
@ -57,7 +60,20 @@ public class MetricExportersTests {
public void oneWriter() { public void oneWriter() {
this.export.setUpDefaults(); this.export.setUpDefaults();
this.writers.put("foo", this.writer); 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()); this.exporters.configureTasks(new ScheduledTaskRegistrar());
assertNotNull(this.exporters.getExporters()); assertNotNull(this.exporters.getExporters());
assertEquals(1, this.exporters.getExporters().size()); assertEquals(1, this.exporters.getExporters().size());

Loading…
Cancel
Save