Skip to content

Commit 352ccbb

Browse files
committed
Merge pull request #9557 from espiegelberg:master
* pr/9557: Polish "Add health indicator for Neo4j" Add health indicator for Neo4j Remove dead code Add missing whitespace in log
2 parents b465d9d + 01272fa commit 352ccbb

File tree

6 files changed

+228
-2
lines changed

6 files changed

+228
-2
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfiguration.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.couchbase.client.java.Bucket;
2727
import com.datastax.driver.core.Cluster;
2828
import org.apache.solr.client.solrj.SolrClient;
29+
import org.neo4j.ogm.session.SessionFactory;
2930

3031
import org.springframework.amqp.rabbit.core.RabbitTemplate;
3132
import org.springframework.beans.factory.InitializingBean;
@@ -42,6 +43,7 @@
4243
import org.springframework.boot.actuate.health.LdapHealthIndicator;
4344
import org.springframework.boot.actuate.health.MailHealthIndicator;
4445
import org.springframework.boot.actuate.health.MongoHealthIndicator;
46+
import org.springframework.boot.actuate.health.Neo4jHealthIndicator;
4547
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
4648
import org.springframework.boot.actuate.health.RabbitHealthIndicator;
4749
import org.springframework.boot.actuate.health.RedisHealthIndicator;
@@ -59,6 +61,7 @@
5961
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
6062
import org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration;
6163
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
64+
import org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration;
6265
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
6366
import org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration;
6467
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@@ -91,6 +94,7 @@
9194
* @author Phillip Webb
9295
* @author Tommy Ludwig
9396
* @author Eddú Meléndez
97+
* @author Eric Spiegelberg
9498
* @since 1.1.0
9599
*/
96100
@Configuration
@@ -101,8 +105,8 @@
101105
JestAutoConfiguration.class, JmsAutoConfiguration.class,
102106
LdapDataAutoConfiguration.class, MailSenderAutoConfiguration.class,
103107
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
104-
RabbitAutoConfiguration.class, RedisAutoConfiguration.class,
105-
SolrAutoConfiguration.class })
108+
Neo4jDataAutoConfiguration.class, RabbitAutoConfiguration.class,
109+
RedisAutoConfiguration.class, SolrAutoConfiguration.class })
106110
@EnableConfigurationProperties({ HealthIndicatorProperties.class })
107111
@Import({
108112
ElasticsearchHealthIndicatorConfiguration.ElasticsearchClientHealthIndicatorConfiguration.class,
@@ -278,6 +282,28 @@ public HealthIndicator mongoHealthIndicator() {
278282

279283
}
280284

285+
@Configuration
286+
@ConditionalOnClass(SessionFactory.class)
287+
@ConditionalOnBean(SessionFactory.class)
288+
@ConditionalOnEnabledHealthIndicator("neo4j")
289+
public static class Neo4jHealthIndicatorConfiguration extends
290+
CompositeHealthIndicatorConfiguration<Neo4jHealthIndicator, SessionFactory> {
291+
292+
private final Map<String, SessionFactory> sessionFactories;
293+
294+
public Neo4jHealthIndicatorConfiguration(
295+
Map<String, SessionFactory> sessionFactories) {
296+
this.sessionFactories = sessionFactories;
297+
}
298+
299+
@Bean
300+
@ConditionalOnMissingBean(name = "neo4jHealthIndicator")
301+
public HealthIndicator neo4jHealthIndicator() {
302+
return createHealthIndicator(this.sessionFactories);
303+
}
304+
305+
}
306+
281307
@Configuration
282308
@ConditionalOnBean(RedisConnectionFactory.class)
283309
@ConditionalOnEnabledHealthIndicator("redis")
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-2017 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 org.springframework.boot.actuate.health;
18+
19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
import org.neo4j.ogm.model.Result;
23+
import org.neo4j.ogm.session.Session;
24+
import org.neo4j.ogm.session.SessionFactory;
25+
26+
/**
27+
* {@link HealthIndicator} that tests the status of a Neo4j by executing a Cypher
28+
* statement.
29+
*
30+
* @author Eric Spiegelberg
31+
* @since 2.0.0
32+
*/
33+
public class Neo4jHealthIndicator extends AbstractHealthIndicator {
34+
35+
/**
36+
* The Cypher statement used to verify Neo4j is up.
37+
*/
38+
static final String CYPHER = "match (n) return count(n) as nodes";
39+
40+
private final SessionFactory sessionFactory;
41+
42+
/**
43+
* Create a new {@link Neo4jHealthIndicator} using the specified
44+
* {@link SessionFactory}.
45+
* @param sessionFactory the SessionFactory
46+
*/
47+
public Neo4jHealthIndicator(SessionFactory sessionFactory) {
48+
this.sessionFactory = sessionFactory;
49+
}
50+
51+
@Override
52+
protected void doHealthCheck(Health.Builder builder) throws Exception {
53+
Session session = this.sessionFactory.openSession();
54+
55+
Result result = session.query(CYPHER, Collections.EMPTY_MAP);
56+
Iterable<Map<String, Object>> results = result.queryResults();
57+
int nodes = (int) results.iterator().next().get("nodes");
58+
59+
builder.up().withDetail("nodes", nodes);
60+
}
61+
62+
}

spring-boot-actuator/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@
181181
"description": "Enable Mail health check.",
182182
"defaultValue": true
183183
},
184+
{
185+
"name": "management.health.neo4j.enabled",
186+
"type": "java.lang.Boolean",
187+
"description": "Enable Neo4j health check.",
188+
"defaultValue": true
189+
},
184190
{
185191
"name": "management.info.build.enabled",
186192
"type": "java.lang.Boolean",

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.searchbox.client.JestClient;
2424
import org.junit.After;
2525
import org.junit.Test;
26+
import org.neo4j.ogm.session.SessionFactory;
2627

2728
import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
2829
import org.springframework.boot.actuate.health.CassandraHealthIndicator;
@@ -38,6 +39,7 @@
3839
import org.springframework.boot.actuate.health.LdapHealthIndicator;
3940
import org.springframework.boot.actuate.health.MailHealthIndicator;
4041
import org.springframework.boot.actuate.health.MongoHealthIndicator;
42+
import org.springframework.boot.actuate.health.Neo4jHealthIndicator;
4143
import org.springframework.boot.actuate.health.RabbitHealthIndicator;
4244
import org.springframework.boot.actuate.health.RedisHealthIndicator;
4345
import org.springframework.boot.actuate.health.SolrHealthIndicator;
@@ -76,6 +78,7 @@
7678
* @author Stephane Nicoll
7779
* @author Andy Wilkinson
7880
* @author Eddú Meléndez
81+
* @author Eric Spiegelberg
7982
*/
8083
public class HealthIndicatorAutoConfigurationTests {
8184

@@ -578,6 +581,34 @@ public void notLdapHealthIndicator() throws Exception {
578581
.isEqualTo(ApplicationHealthIndicator.class);
579582
}
580583

584+
@Test
585+
public void neo4jHealthIndicator() throws Exception {
586+
TestPropertyValues.of("management.health.diskspace.enabled:false")
587+
.applyTo(this.context);
588+
this.context.register(Neo4jConfiguration.class, ManagementServerProperties.class,
589+
HealthIndicatorAutoConfiguration.class);
590+
this.context.refresh();
591+
Map<String, HealthIndicator> beans = this.context
592+
.getBeansOfType(HealthIndicator.class);
593+
assertThat(beans.size()).isEqualTo(1);
594+
assertThat(beans.values().iterator().next().getClass())
595+
.isEqualTo(Neo4jHealthIndicator.class);
596+
}
597+
598+
@Test
599+
public void notNeo4jHealthIndicator() throws Exception {
600+
TestPropertyValues.of("management.health.diskspace.enabled:false",
601+
"management.health.neo4j.enabled:false").applyTo(this.context);
602+
this.context.register(Neo4jConfiguration.class, ManagementServerProperties.class,
603+
HealthIndicatorAutoConfiguration.class);
604+
this.context.refresh();
605+
Map<String, HealthIndicator> beans = this.context
606+
.getBeansOfType(HealthIndicator.class);
607+
assertThat(beans.size()).isEqualTo(1);
608+
assertThat(beans.values().iterator().next().getClass())
609+
.isEqualTo(ApplicationHealthIndicator.class);
610+
}
611+
581612
@Configuration
582613
@EnableConfigurationProperties
583614
protected static class DataSourceConfig {
@@ -659,4 +690,14 @@ public LdapOperations ldapOperations() {
659690

660691
}
661692

693+
@Configuration
694+
protected static class Neo4jConfiguration {
695+
696+
@Bean
697+
public SessionFactory sessionFactory() {
698+
return mock(SessionFactory.class);
699+
}
700+
701+
}
702+
662703
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2012-2017 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 org.springframework.boot.actuate.health;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import org.junit.Assert;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.neo4j.ogm.exception.CypherException;
29+
import org.neo4j.ogm.model.Result;
30+
import org.neo4j.ogm.session.Session;
31+
import org.neo4j.ogm.session.SessionFactory;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.mockito.BDDMockito.given;
35+
import static org.mockito.Mockito.mock;
36+
37+
/**
38+
* Tests for {@link Neo4jHealthIndicator}.
39+
*
40+
* @author Eric Spiegelberg
41+
* @author Stephane Nicoll
42+
*/
43+
public class Neo4jHealthIndicatorTests {
44+
45+
private Session session;
46+
47+
private Neo4jHealthIndicator neo4jHealthIndicator;
48+
49+
@Before
50+
public void before() {
51+
this.session = mock(Session.class);
52+
SessionFactory sessionFactory = mock(SessionFactory.class);
53+
given(sessionFactory.openSession()).willReturn(this.session);
54+
this.neo4jHealthIndicator = new Neo4jHealthIndicator(sessionFactory);
55+
}
56+
57+
@Test
58+
public void neo4jUp() {
59+
Result result = mock(Result.class);
60+
given(this.session.query(Neo4jHealthIndicator.CYPHER, Collections.EMPTY_MAP))
61+
.willReturn(result);
62+
63+
int nodeCount = 500;
64+
Map<String, Object> expectedCypherDetails = new HashMap<>();
65+
expectedCypherDetails.put("nodes", nodeCount);
66+
List<Map<String, Object>> queryResults = new ArrayList<>();
67+
queryResults.add(expectedCypherDetails);
68+
given(result.queryResults()).willReturn(queryResults);
69+
70+
Health health = this.neo4jHealthIndicator.health();
71+
assertThat(health.getStatus()).isEqualTo(Status.UP);
72+
Map<String, Object> details = health.getDetails();
73+
int nodeCountFromDetails = (int) details.get("nodes");
74+
75+
Assert.assertEquals(nodeCount, nodeCountFromDetails);
76+
}
77+
78+
@Test
79+
public void neo4jDown() {
80+
CypherException cypherException = new CypherException("Error executing Cypher",
81+
"Neo.ClientError.Statement.SyntaxError",
82+
"Unable to execute invalid Cypher");
83+
given(this.session.query(Neo4jHealthIndicator.CYPHER, Collections.EMPTY_MAP))
84+
.willThrow(cypherException);
85+
86+
Health health = this.neo4jHealthIndicator.health();
87+
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
88+
}
89+
90+
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,7 @@ content into your application; rather pick only the properties that you need.
12141214
management.health.ldap.enabled=true # Enable LDAP health check.
12151215
management.health.mail.enabled=true # Enable Mail health check.
12161216
management.health.mongo.enabled=true # Enable MongoDB health check.
1217+
management.health.neo4j.enabled=true # Enable Neo4j health check.
12171218
management.health.rabbit.enabled=true # Enable RabbitMQ health check.
12181219
management.health.redis.enabled=true # Enable Redis health check.
12191220
management.health.solr.enabled=true # Enable Solr health check.

0 commit comments

Comments
 (0)