From 18928a62dff343fdaea06053880d4ccfff0c2425 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 1 May 2015 22:04:10 +0100 Subject: [PATCH] Add sample for Redis metric exporter --- spring-boot-samples/pom.xml | 1 + .../README.adoc | 24 ++++++++ .../docker-compose.yml | 4 ++ .../spring-boot-sample-metrics-redis/pom.xml | 48 +++++++++++++++ .../metrics/redis/ExportProperties.java | 43 ++++++++++++++ .../metrics/redis/HelloWorldService.java | 40 +++++++++++++ .../metrics/redis/SampleController.java | 58 +++++++++++++++++++ .../redis/SampleRedisExportApplication.java | 45 ++++++++++++++ .../src/main/resources/application.properties | 2 + .../SampleRedisExportApplicationTests.java | 44 ++++++++++++++ 10 files changed, 309 insertions(+) create mode 100644 spring-boot-samples/spring-boot-sample-metrics-redis/README.adoc create mode 100644 spring-boot-samples/spring-boot-sample-metrics-redis/docker-compose.yml create mode 100644 spring-boot-samples/spring-boot-sample-metrics-redis/pom.xml create mode 100644 spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/ExportProperties.java create mode 100644 spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/HelloWorldService.java create mode 100644 spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/SampleController.java create mode 100644 spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/SampleRedisExportApplication.java create mode 100644 spring-boot-samples/spring-boot-sample-metrics-redis/src/main/resources/application.properties create mode 100644 spring-boot-samples/spring-boot-sample-metrics-redis/src/test/java/sample/metrics/redis/SampleRedisExportApplicationTests.java diff --git a/spring-boot-samples/pom.xml b/spring-boot-samples/pom.xml index 85fb2ae60d2..0c7e18ef661 100644 --- a/spring-boot-samples/pom.xml +++ b/spring-boot-samples/pom.xml @@ -52,6 +52,7 @@ spring-boot-sample-jta-bitronix spring-boot-sample-jta-jndi spring-boot-sample-liquibase + spring-boot-sample-metrics-redis spring-boot-sample-parent-context spring-boot-sample-profile spring-boot-sample-secure diff --git a/spring-boot-samples/spring-boot-sample-metrics-redis/README.adoc b/spring-boot-samples/spring-boot-sample-metrics-redis/README.adoc new file mode 100644 index 00000000000..a50efbf7520 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-metrics-redis/README.adoc @@ -0,0 +1,24 @@ +Spring Boot sample with Redis export for metrics. + +Start redis, e.g. with [Docker Compose]() + +[source,indent=0] +---- + $ docker-compose up +---- + +Run the app and ping the home page (http://localhost:8080) a few times. Go and look at +the result in Redis, e.g. + +[source,indent=0] +---- + $ redis-cli + 127.0.0.1:6379> keys * + 1) "keys.spring.metrics" + 2) "spring.metrics.counter.status.200.root" + 3) "spring.metrics.gauge.response.root" + 127.0.0.1:6379> zrange keys.spring.metrics 0 0 WITHSCORES + 1) "spring.metrics.counter.status.200.root" + 2) "4" +---- + diff --git a/spring-boot-samples/spring-boot-sample-metrics-redis/docker-compose.yml b/spring-boot-samples/spring-boot-sample-metrics-redis/docker-compose.yml new file mode 100644 index 00000000000..e5a62d0ef49 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-metrics-redis/docker-compose.yml @@ -0,0 +1,4 @@ +redis: + image: redis + ports: + - "6379:6379" diff --git a/spring-boot-samples/spring-boot-sample-metrics-redis/pom.xml b/spring-boot-samples/spring-boot-sample-metrics-redis/pom.xml new file mode 100644 index 00000000000..dded5607f98 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-metrics-redis/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-samples + 1.3.0.BUILD-SNAPSHOT + + spring-boot-sample-metrics-redis + spring-boot-sample-metrics-redis + Spring Boot Metrics Redis Sample + http://projects.spring.io/spring-boot/ + + Pivotal Software, Inc. + http://www.spring.io + + + ${basedir}/../.. + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-redis + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/ExportProperties.java b/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/ExportProperties.java new file mode 100644 index 00000000000..3e27955f8d5 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/ExportProperties.java @@ -0,0 +1,43 @@ +/* + * 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 sample.metrics.redis; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("metrics.export") +class ExportProperties { + + private String prefix = "spring.metrics"; + private String key = "keys.spring.metrics"; + + public String getPrefix() { + return this.prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public String getKey() { + return this.key; + } + + public void setKey(String key) { + this.key = key; + } + +} \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/HelloWorldService.java b/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/HelloWorldService.java new file mode 100644 index 00000000000..9b08174c008 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/HelloWorldService.java @@ -0,0 +1,40 @@ +/* + * 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 sample.metrics.redis; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "service", ignoreUnknownFields = false) +public class HelloWorldService { + + private String name = "World"; + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public String getHelloMessage() { + return "Hello " + this.name; + } + +} diff --git a/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/SampleController.java b/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/SampleController.java new file mode 100644 index 00000000000..b72a7591d53 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/SampleController.java @@ -0,0 +1,58 @@ +/* + * 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 sample.metrics.redis; + +import java.util.Collections; +import java.util.Map; + +import org.hibernate.validator.constraints.NotBlank; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Description; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +@Description("A controller for handling requests for hello messages") +public class SampleController { + + @Autowired + private HelloWorldService helloWorldService; + + @RequestMapping(value = "/", method = RequestMethod.GET) + @ResponseBody + public Map hello() { + return Collections.singletonMap("message", + this.helloWorldService.getHelloMessage()); + } + + protected static class Message { + + @NotBlank(message = "Message value cannot be empty") + private String value; + + public String getValue() { + return this.value; + } + + public void setValue(String value) { + this.value = value; + } + } + +} diff --git a/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/SampleRedisExportApplication.java b/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/SampleRedisExportApplication.java new file mode 100644 index 00000000000..28915ed6590 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/java/sample/metrics/redis/SampleRedisExportApplication.java @@ -0,0 +1,45 @@ +/* + * 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 sample.metrics.redis; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.actuate.metrics.repository.redis.RedisMetricRepository; +import org.springframework.boot.actuate.metrics.writer.MetricWriter; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.data.redis.connection.RedisConnectionFactory; + +@SpringBootApplication +@EnableConfigurationProperties(ExportProperties.class) +public class SampleRedisExportApplication { + + @Autowired + private ExportProperties export; + + public static void main(String[] args) throws Exception { + SpringApplication.run(SampleRedisExportApplication.class, args); + } + + @Bean + public MetricWriter redisMetricWriter(RedisConnectionFactory connectionFactory) { + return new RedisMetricRepository(connectionFactory, this.export.getPrefix(), + this.export.getKey()); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/resources/application.properties new file mode 100644 index 00000000000..01ba46e43df --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-metrics-redis/src/main/resources/application.properties @@ -0,0 +1,2 @@ +service.name: Phil +metrics.export.prefix: ${spring.application.name:application}:${random.value:0000} \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-metrics-redis/src/test/java/sample/metrics/redis/SampleRedisExportApplicationTests.java b/spring-boot-samples/spring-boot-sample-metrics-redis/src/test/java/sample/metrics/redis/SampleRedisExportApplicationTests.java new file mode 100644 index 00000000000..9009dc390ab --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-metrics-redis/src/test/java/sample/metrics/redis/SampleRedisExportApplicationTests.java @@ -0,0 +1,44 @@ +/* + * 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 sample.metrics.redis; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +/** + * Basic integration tests for {@link SampleRedisExportApplication}. + * + * @author Dave Syer + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = SampleRedisExportApplication.class) +@WebAppConfiguration +@IntegrationTest("server.port=0") +@DirtiesContext +public class SampleRedisExportApplicationTests { + + @Test + public void contextLoads() { + + } + +}