Commit b7435016 authored by Stephane Nicoll's avatar Stephane Nicoll

Improve Couchbase health indicator

This commit improve the couchbase health indicator to list the available
nodes. Doing so improves the reliability of the indicator as accessing
the bucket forces a remote call and better detect the cases where the
broker is down.

Closes gh-7369
parent 2ed162a0
...@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.couchbase; ...@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.couchbase;
import java.util.List; import java.util.List;
import com.couchbase.client.java.bucket.BucketInfo;
import com.couchbase.client.java.util.features.Version; import com.couchbase.client.java.util.features.Version;
import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.AbstractHealthIndicator;
...@@ -31,6 +32,7 @@ import org.springframework.util.StringUtils; ...@@ -31,6 +32,7 @@ import org.springframework.util.StringUtils;
* {@link HealthIndicator} for Couchbase. * {@link HealthIndicator} for Couchbase.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll
* @since 2.0.0 * @since 2.0.0
*/ */
public class CouchbaseHealthIndicator extends AbstractHealthIndicator { public class CouchbaseHealthIndicator extends AbstractHealthIndicator {
...@@ -46,8 +48,13 @@ public class CouchbaseHealthIndicator extends AbstractHealthIndicator { ...@@ -46,8 +48,13 @@ public class CouchbaseHealthIndicator extends AbstractHealthIndicator {
protected void doHealthCheck(Health.Builder builder) throws Exception { protected void doHealthCheck(Health.Builder builder) throws Exception {
List<Version> versions = this.couchbaseOperations.getCouchbaseClusterInfo() List<Version> versions = this.couchbaseOperations.getCouchbaseClusterInfo()
.getAllVersions(); .getAllVersions();
builder.up().withDetail("versions", BucketInfo bucketInfo = this.couchbaseOperations.getCouchbaseBucket()
StringUtils.collectionToCommaDelimitedString(versions)); .bucketManager().info();
builder.up()
.withDetail("versions", StringUtils.collectionToCommaDelimitedString(
versions))
.withDetail("nodes", StringUtils.collectionToCommaDelimitedString(
bucketInfo.nodeList()));
} }
} }
...@@ -16,8 +16,13 @@ ...@@ -16,8 +16,13 @@
package org.springframework.boot.actuate.couchbase; package org.springframework.boot.actuate.couchbase;
import java.util.Arrays; import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.bucket.BucketInfo;
import com.couchbase.client.java.bucket.BucketManager;
import com.couchbase.client.java.cluster.ClusterInfo; import com.couchbase.client.java.cluster.ClusterInfo;
import com.couchbase.client.java.util.features.Version; import com.couchbase.client.java.util.features.Version;
import org.junit.Test; import org.junit.Test;
...@@ -27,6 +32,7 @@ import org.springframework.boot.actuate.health.Status; ...@@ -27,6 +32,7 @@ import org.springframework.boot.actuate.health.Status;
import org.springframework.data.couchbase.core.CouchbaseOperations; import org.springframework.data.couchbase.core.CouchbaseOperations;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
...@@ -35,22 +41,33 @@ import static org.mockito.Mockito.verify; ...@@ -35,22 +41,33 @@ import static org.mockito.Mockito.verify;
* Tests for {@link CouchbaseHealthIndicator} * Tests for {@link CouchbaseHealthIndicator}
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll
*/ */
public class CouchbaseHealthIndicatorTests { public class CouchbaseHealthIndicatorTests {
@Test @Test
public void couchbaseIsUp() { public void couchbaseIsUp() throws UnknownHostException {
CouchbaseOperations couchbaseOperations = mock(CouchbaseOperations.class); BucketInfo bucketInfo = mock(BucketInfo.class);
given(bucketInfo.nodeList()).willReturn(Collections.singletonList(
InetAddress.getByName("127.0.0.1")));
BucketManager bucketManager = mock(BucketManager.class);
given(bucketManager.info()).willReturn(bucketInfo);
Bucket bucket = mock(Bucket.class);
given(bucket.bucketManager()).willReturn(bucketManager);
ClusterInfo clusterInfo = mock(ClusterInfo.class); ClusterInfo clusterInfo = mock(ClusterInfo.class);
given(clusterInfo.getAllVersions()) given(clusterInfo.getAllVersions())
.willReturn(Arrays.asList(new Version(1, 2, 3))); .willReturn(Collections.singletonList(new Version(1, 2, 3)));
CouchbaseOperations couchbaseOperations = mock(CouchbaseOperations.class);
given(couchbaseOperations.getCouchbaseBucket()).willReturn(bucket);
given(couchbaseOperations.getCouchbaseClusterInfo()).willReturn(clusterInfo); given(couchbaseOperations.getCouchbaseClusterInfo()).willReturn(clusterInfo);
CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator( CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator(
couchbaseOperations); couchbaseOperations);
Health health = healthIndicator.health(); Health health = healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP); assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails().get("versions")).isEqualTo("1.2.3"); assertThat(health.getDetails()).containsOnly(entry("versions", "1.2.3"),
entry("nodes", "/127.0.0.1"));
verify(clusterInfo).getAllVersions(); verify(clusterInfo).getAllVersions();
verify(bucketInfo).nodeList();
} }
@Test @Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment