diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricExportProperties.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricExportProperties.java index 0faad713ed..bd399c9cdf 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricExportProperties.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/MetricExportProperties.java @@ -63,7 +63,7 @@ public class MetricExportProperties { } @PostConstruct - public void setDefaults() { + public void setUpDefaults() { Export defaults = null; for (Entry entry : this.writers.entrySet()) { String key = entry.getKey(); @@ -91,13 +91,13 @@ public class MetricExportProperties { } for (Export value : this.writers.values()) { if (value.isIgnoreTimestamps() == null) { - value.setIgnoreTimestamps(false); + value.setIgnoreTimestamps(defaults.isIgnoreTimestamps()); } if (value.isSendLatest() == null) { - value.setSendLatest(true); + value.setSendLatest(defaults.isSendLatest()); } if (value.getDelayMillis() == null) { - value.setDelayMillis(5000); + value.setDelayMillis(defaults.getDelayMillis()); } } } @@ -210,6 +210,6 @@ public class MetricExportProperties { return value; } } - return null; + return this.export; } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/opentsdb/OpenTsdbHttpMetricWriter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/opentsdb/OpenTsdbMetricWriter.java similarity index 92% rename from spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/opentsdb/OpenTsdbHttpMetricWriter.java rename to spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/opentsdb/OpenTsdbMetricWriter.java index bd29f8a6e9..13ae1ad39f 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/opentsdb/OpenTsdbHttpMetricWriter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/opentsdb/OpenTsdbMetricWriter.java @@ -31,6 +31,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.web.client.RestOperations; import org.springframework.web.client.RestTemplate; /** @@ -43,11 +44,11 @@ import org.springframework.web.client.RestTemplate; * * @author Dave Syer */ -public class OpenTsdbHttpMetricWriter implements MetricWriter { +public class OpenTsdbMetricWriter implements MetricWriter { - private static final Log logger = LogFactory.getLog(OpenTsdbHttpMetricWriter.class); + private static final Log logger = LogFactory.getLog(OpenTsdbMetricWriter.class); - private RestTemplate restTemplate = new RestTemplate(); + private RestOperations restTemplate = new RestTemplate(); /** * URL for POSTing data. Defaults to http://localhost:4242/api/put. @@ -69,11 +70,11 @@ public class OpenTsdbHttpMetricWriter implements MetricWriter { private OpenTsdbNamingStrategy namingStrategy = new DefaultOpenTsdbNamingStrategy(); - public RestTemplate getRestTemplate() { + public RestOperations getRestTemplate() { return this.restTemplate; } - public void setRestTemplate(RestTemplate restTemplate) { + public void setRestTemplate(RestOperations restTemplate) { this.restTemplate = restTemplate; } 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 new file mode 100644 index 0000000000..6b0c332ce9 --- /dev/null +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/MetricExportersTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2012-2015 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.export; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.Test; +import org.mockito.Mockito; +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; + +/** + * @author Dave Syer + */ +public class MetricExportersTests { + + private MetricExporters exporters; + private MetricExportProperties export = new MetricExportProperties(); + private Map writers = new LinkedHashMap(); + private MetricReader reader = Mockito.mock(MetricReader.class); + private MetricWriter writer = Mockito.mock(MetricWriter.class); + + @Test + public void emptyWriters() { + this.exporters = new MetricExporters(this.reader, this.writers, this.export); + this.exporters.configureTasks(new ScheduledTaskRegistrar()); + assertNotNull(this.exporters.getExporters()); + assertEquals(0, this.exporters.getExporters().size()); + } + + @Test + public void oneWriter() { + this.export.setUpDefaults(); + this.writers.put("foo", this.writer); + this.exporters = new MetricExporters(this.reader, this.writers, this.export); + this.exporters.configureTasks(new ScheduledTaskRegistrar()); + assertNotNull(this.exporters.getExporters()); + assertEquals(1, this.exporters.getExporters().size()); + } + +} diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/opentsdb/OpenTsdbMetricWriterTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/opentsdb/OpenTsdbMetricWriterTests.java new file mode 100644 index 0000000000..0c68878203 --- /dev/null +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/opentsdb/OpenTsdbMetricWriterTests.java @@ -0,0 +1,82 @@ +/* + * Copyright 2012-2015 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.opentsdb; + +import java.util.Collections; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.springframework.boot.actuate.metrics.Metric; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestOperations; + +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.verify; + +/** + * @author Dave Syer + */ +public class OpenTsdbMetricWriterTests { + + private OpenTsdbMetricWriter writer; + private RestOperations restTemplate = Mockito.mock(RestOperations.class); + + @Before + public void init() { + this.writer = new OpenTsdbMetricWriter(); + this.writer.setRestTemplate(this.restTemplate); + } + + @Test + public void postSuccessfullyOnFlush() { + this.writer.set(new Metric("foo", 2.4)); + given( + this.restTemplate.postForEntity(Matchers.anyString(), + Matchers.any(Object.class), anyMap())) + .willReturn(emptyResponse()); + this.writer.flush(); + verify(this.restTemplate).postForEntity(Matchers.anyString(), + Matchers.any(Object.class), anyMap()); + } + + @Test + public void flushAutomaticlly() { + given( + this.restTemplate.postForEntity(Matchers.anyString(), + Matchers.any(Object.class), anyMap())) + .willReturn(emptyResponse()); + this.writer.setBufferSize(0); + this.writer.set(new Metric("foo", 2.4)); + verify(this.restTemplate).postForEntity(Matchers.anyString(), + Matchers.any(Object.class), anyMap()); + } + + @SuppressWarnings("rawtypes") + private ResponseEntity emptyResponse() { + return new ResponseEntity(Collections.emptyMap(), HttpStatus.OK); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private Class anyMap() { + return Matchers.any(Class.class); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-metrics-opentsdb/src/main/java/sample/metrics/opentsdb/SampleOpenTsdbExportApplication.java b/spring-boot-samples/spring-boot-sample-metrics-opentsdb/src/main/java/sample/metrics/opentsdb/SampleOpenTsdbExportApplication.java index 81376fd64e..06403caad9 100644 --- a/spring-boot-samples/spring-boot-sample-metrics-opentsdb/src/main/java/sample/metrics/opentsdb/SampleOpenTsdbExportApplication.java +++ b/spring-boot-samples/spring-boot-sample-metrics-opentsdb/src/main/java/sample/metrics/opentsdb/SampleOpenTsdbExportApplication.java @@ -18,7 +18,7 @@ package sample.metrics.opentsdb; import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.metrics.opentsdb.DefaultOpenTsdbNamingStrategy; -import org.springframework.boot.actuate.metrics.opentsdb.OpenTsdbHttpMetricWriter; +import org.springframework.boot.actuate.metrics.opentsdb.OpenTsdbMetricWriter; import org.springframework.boot.actuate.metrics.opentsdb.OpenTsdbNamingStrategy; import org.springframework.boot.actuate.metrics.writer.MetricWriter; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -35,7 +35,7 @@ public class SampleOpenTsdbExportApplication { @Bean @ConfigurationProperties("metrics.export") public MetricWriter openTsdbMetricWriter() { - OpenTsdbHttpMetricWriter writer = new OpenTsdbHttpMetricWriter(); + OpenTsdbMetricWriter writer = new OpenTsdbMetricWriter(); writer.setNamingStrategy(namingStrategy()); return writer; }