Skip to content

Commit 4c97dcb

Browse files
Eric Spiegelbergsnicoll
authored andcommitted
Add health indicator for Neo4j
See gh-9557
1 parent 3b0cbea commit 4c97dcb

File tree

5 files changed

+238
-1
lines changed

5 files changed

+238
-1
lines changed

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

Lines changed: 27 additions & 1 deletion
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
@@ -102,7 +106,7 @@
102106
LdapDataAutoConfiguration.class, MailSenderAutoConfiguration.class,
103107
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
104108
RabbitAutoConfiguration.class, RedisAutoConfiguration.class,
105-
SolrAutoConfiguration.class })
109+
SolrAutoConfiguration.class, Neo4jDataAutoConfiguration.class })
106110
@EnableConfigurationProperties({ HealthIndicatorProperties.class })
107111
@Import({
108112
ElasticsearchHealthIndicatorConfiguration.ElasticsearchClientHealthIndicatorConfiguration.class,
@@ -257,6 +261,28 @@ public HealthIndicator ldapHealthIndicator() {
257261

258262
}
259263

264+
@Configuration
265+
@ConditionalOnClass(SessionFactory.class)
266+
@ConditionalOnBean(SessionFactory.class)
267+
@ConditionalOnEnabledHealthIndicator("neo4j")
268+
public static class Neo4jHealthIndicatorConfiguration extends
269+
CompositeHealthIndicatorConfiguration<Neo4jHealthIndicator, SessionFactory> {
270+
271+
private final Map<String, SessionFactory> sessionFactories;
272+
273+
public Neo4jHealthIndicatorConfiguration(
274+
Map<String, SessionFactory> sessionFactories) {
275+
this.sessionFactories = sessionFactories;
276+
}
277+
278+
@Bean
279+
@ConditionalOnMissingBean(name = "neo4jHealthIndicator")
280+
public HealthIndicator neo4jHealthIndicator() {
281+
return createHealthIndicator(this.sessionFactories);
282+
}
283+
284+
}
285+
260286
@Configuration
261287
@ConditionalOnBean(MongoTemplate.class)
262288
@ConditionalOnEnabledHealthIndicator("mongo")
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
import org.springframework.boot.context.properties.ConfigurationProperties;
27+
28+
/**
29+
* {@link HealthIndicator} that tests the status of a Neo4j by executing a Cypher
30+
* statement.
31+
*
32+
* @author Eric Spiegelberg
33+
*/
34+
@ConfigurationProperties(prefix = "management.health.neo4j", ignoreUnknownFields = false)
35+
public class Neo4jHealthIndicator extends AbstractHealthIndicator {
36+
37+
private final SessionFactory sessionFactory;
38+
39+
/**
40+
* The Cypher statement used to verify Neo4j is up.
41+
*/
42+
public static final String CYPHER = "match (n) return count(n) as nodes";
43+
44+
/**
45+
* Create a new {@link Neo4jHealthIndicator} using the specified
46+
* {@link SessionFactory}.
47+
* @param sessionFactory the SessionFactory
48+
*/
49+
public Neo4jHealthIndicator(SessionFactory sessionFactory) {
50+
this.sessionFactory = sessionFactory;
51+
}
52+
53+
@Override
54+
protected void doHealthCheck(Health.Builder builder) throws Exception {
55+
Session session = this.sessionFactory.openSession();
56+
57+
Result result = session.query(CYPHER, Collections.emptyMap());
58+
Iterable<Map<String, Object>> results = result.queryResults();
59+
int nodes = (int) results.iterator().next().get("nodes");
60+
61+
builder.up().withDetail("nodes", nodes);
62+
}
63+
64+
}

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

0 commit comments

Comments
 (0)