Skip to content

Commit 18928a6

Browse files
dsyerwilkinsona
authored andcommitted
Add sample for Redis metric exporter
1 parent a32c530 commit 18928a6

File tree

10 files changed

+309
-0
lines changed

10 files changed

+309
-0
lines changed

Diff for: spring-boot-samples/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<module>spring-boot-sample-jta-bitronix</module>
5353
<module>spring-boot-sample-jta-jndi</module>
5454
<module>spring-boot-sample-liquibase</module>
55+
<module>spring-boot-sample-metrics-redis</module>
5556
<module>spring-boot-sample-parent-context</module>
5657
<module>spring-boot-sample-profile</module>
5758
<module>spring-boot-sample-secure</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Spring Boot sample with Redis export for metrics.
2+
3+
Start redis, e.g. with [Docker Compose]()
4+
5+
[source,indent=0]
6+
----
7+
$ docker-compose up
8+
----
9+
10+
Run the app and ping the home page (http://localhost:8080) a few times. Go and look at
11+
the result in Redis, e.g.
12+
13+
[source,indent=0]
14+
----
15+
$ redis-cli
16+
127.0.0.1:6379> keys *
17+
1) "keys.spring.metrics"
18+
2) "spring.metrics.counter.status.200.root"
19+
3) "spring.metrics.gauge.response.root"
20+
127.0.0.1:6379> zrange keys.spring.metrics 0 0 WITHSCORES
21+
1) "spring.metrics.counter.status.200.root"
22+
2) "4"
23+
----
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
redis:
2+
image: redis
3+
ports:
4+
- "6379:6379"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<!-- Your own application should inherit from spring-boot-starter-parent -->
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-samples</artifactId>
8+
<version>1.3.0.BUILD-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>spring-boot-sample-metrics-redis</artifactId>
11+
<name>spring-boot-sample-metrics-redis</name>
12+
<description>Spring Boot Metrics Redis Sample</description>
13+
<url>http://projects.spring.io/spring-boot/</url>
14+
<organization>
15+
<name>Pivotal Software, Inc.</name>
16+
<url>http://www.spring.io</url>
17+
</organization>
18+
<properties>
19+
<main.basedir>${basedir}/../..</main.basedir>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-actuator</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-redis</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-maven-plugin</artifactId>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.metrics.redis;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
@ConfigurationProperties("metrics.export")
22+
class ExportProperties {
23+
24+
private String prefix = "spring.metrics";
25+
private String key = "keys.spring.metrics";
26+
27+
public String getPrefix() {
28+
return this.prefix;
29+
}
30+
31+
public void setPrefix(String prefix) {
32+
this.prefix = prefix;
33+
}
34+
35+
public String getKey() {
36+
return this.key;
37+
}
38+
39+
public void setKey(String key) {
40+
this.key = key;
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.metrics.redis;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
import org.springframework.stereotype.Component;
21+
22+
@Component
23+
@ConfigurationProperties(prefix = "service", ignoreUnknownFields = false)
24+
public class HelloWorldService {
25+
26+
private String name = "World";
27+
28+
public String getName() {
29+
return this.name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public String getHelloMessage() {
37+
return "Hello " + this.name;
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.metrics.redis;
18+
19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
import org.hibernate.validator.constraints.NotBlank;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Description;
25+
import org.springframework.stereotype.Controller;
26+
import org.springframework.web.bind.annotation.RequestMapping;
27+
import org.springframework.web.bind.annotation.RequestMethod;
28+
import org.springframework.web.bind.annotation.ResponseBody;
29+
30+
@Controller
31+
@Description("A controller for handling requests for hello messages")
32+
public class SampleController {
33+
34+
@Autowired
35+
private HelloWorldService helloWorldService;
36+
37+
@RequestMapping(value = "/", method = RequestMethod.GET)
38+
@ResponseBody
39+
public Map<String, String> hello() {
40+
return Collections.singletonMap("message",
41+
this.helloWorldService.getHelloMessage());
42+
}
43+
44+
protected static class Message {
45+
46+
@NotBlank(message = "Message value cannot be empty")
47+
private String value;
48+
49+
public String getValue() {
50+
return this.value;
51+
}
52+
53+
public void setValue(String value) {
54+
this.value = value;
55+
}
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.metrics.redis;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.actuate.metrics.repository.redis.RedisMetricRepository;
22+
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.data.redis.connection.RedisConnectionFactory;
27+
28+
@SpringBootApplication
29+
@EnableConfigurationProperties(ExportProperties.class)
30+
public class SampleRedisExportApplication {
31+
32+
@Autowired
33+
private ExportProperties export;
34+
35+
public static void main(String[] args) throws Exception {
36+
SpringApplication.run(SampleRedisExportApplication.class, args);
37+
}
38+
39+
@Bean
40+
public MetricWriter redisMetricWriter(RedisConnectionFactory connectionFactory) {
41+
return new RedisMetricRepository(connectionFactory, this.export.getPrefix(),
42+
this.export.getKey());
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
service.name: Phil
2+
metrics.export.prefix: ${spring.application.name:application}:${random.value:0000}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.metrics.redis;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.springframework.boot.test.IntegrationTest;
22+
import org.springframework.boot.test.SpringApplicationConfiguration;
23+
import org.springframework.test.annotation.DirtiesContext;
24+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
25+
import org.springframework.test.context.web.WebAppConfiguration;
26+
27+
/**
28+
* Basic integration tests for {@link SampleRedisExportApplication}.
29+
*
30+
* @author Dave Syer
31+
*/
32+
@RunWith(SpringJUnit4ClassRunner.class)
33+
@SpringApplicationConfiguration(classes = SampleRedisExportApplication.class)
34+
@WebAppConfiguration
35+
@IntegrationTest("server.port=0")
36+
@DirtiesContext
37+
public class SampleRedisExportApplicationTests {
38+
39+
@Test
40+
public void contextLoads() {
41+
42+
}
43+
44+
}

0 commit comments

Comments
 (0)