diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java index 4699151b10..c34377a6ba 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java @@ -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 writers = Collections.emptyMap(); + @Autowired(required = false) + private Map 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(); } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricExporters.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricExporters.java index 9d068d6f6c..ed6021e319 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricExporters.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricExporters.java @@ -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 writers; + private Map writers = new HashMap(); private final MetricExportProperties properties; private final Map exporters = new HashMap(); - public MetricExporters(MetricReader reader, Map 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 writers) { + this.writers.putAll(writers); + } + + public void setExporters(Map exporters) { + this.exporters.putAll(exporters); + } + @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + for (Entry 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 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; } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/MetricExportersTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/MetricExportersTests.java index 34dea74373..da552be611 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/MetricExportersTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/MetricExportersTests.java @@ -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. singletonMap("foo", + new MetricCopyExporter(this.reader, this.writer))); this.exporters.configureTasks(new ScheduledTaskRegistrar()); assertNotNull(this.exporters.getExporters()); assertEquals(1, this.exporters.getExporters().size());