Skip to content

Commit 1819ce5

Browse files
authored
chore(health): ignore health indicator when dependency not exists (#126)
* fix(health): ignore health indicator when dependency not exists
1 parent d8b4c2f commit 1819ce5

File tree

15 files changed

+104
-47
lines changed

15 files changed

+104
-47
lines changed

Diff for: starters/async-commons-starter/src/main/java/org/reactivecommons/async/starter/broker/BrokerProvider.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import org.reactivecommons.api.domain.DomainEventBus;
44
import org.reactivecommons.async.api.DirectAsyncGateway;
55
import org.reactivecommons.async.commons.HandlerResolver;
6+
import org.reactivecommons.async.starter.config.health.RCHealth;
67
import org.reactivecommons.async.starter.props.GenericAsyncProps;
7-
import org.springframework.boot.actuate.health.Health;
88
import reactor.core.publisher.Mono;
99

1010
@SuppressWarnings("rawtypes")
@@ -25,5 +25,5 @@ public interface BrokerProvider<T extends GenericAsyncProps> {
2525

2626
void listenReplies(HandlerResolver resolver);
2727

28-
Mono<Health> healthCheck();
28+
Mono<RCHealth> healthCheck();
2929
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.reactivecommons.async.starter.config.health;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
@Getter
11+
@Builder
12+
@AllArgsConstructor
13+
public class RCHealth {
14+
private final Status status;
15+
private final Map<String, Object> details;
16+
17+
public enum Status {
18+
UP,
19+
DOWN
20+
}
21+
22+
public static class RCHealthBuilder {
23+
public RCHealthBuilder() {
24+
this.details = new HashMap<>();
25+
}
26+
27+
public RCHealthBuilder up() {
28+
this.status = Status.UP;
29+
return this;
30+
}
31+
32+
public RCHealthBuilder down() {
33+
this.status = Status.DOWN;
34+
return this;
35+
}
36+
37+
public RCHealthBuilder withDetail(String key, Object value) {
38+
this.details.put(key, value);
39+
return this;
40+
}
41+
42+
public RCHealth build() {
43+
return new RCHealth(this.status, this.details);
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.reactivecommons.async.starter.config.health;
2+
3+
import reactor.core.publisher.Mono;
4+
5+
public abstract class RCHealthIndicator {
6+
7+
public Mono<RCHealth> health() {
8+
return doHealthCheck(RCHealth.builder())
9+
.onErrorResume(e -> Mono.just(RCHealth.builder().down().withDetail("error", e.getMessage()).build()));
10+
}
11+
12+
public abstract Mono<RCHealth> doHealthCheck(RCHealth.RCHealthBuilder builder);
13+
}

Diff for: starters/async-commons-starter/src/main/java/org/reactivecommons/async/starter/config/health/ReactiveCommonsHealthConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import org.springframework.context.annotation.Configuration;
99

1010
@Configuration
11+
@ConditionalOnClass(AbstractReactiveHealthIndicator.class)
1112
public class ReactiveCommonsHealthConfig {
1213

1314
@Bean
1415
@ConditionalOnProperty(prefix = "management.health.reactive-commons", name = "enabled", havingValue = "true",
1516
matchIfMissing = true)
16-
@ConditionalOnClass(AbstractReactiveHealthIndicator.class)
1717
public ReactiveCommonsHealthIndicator reactiveCommonsHealthIndicator(ConnectionManager manager) {
1818
return new ReactiveCommonsHealthIndicator(manager);
1919
}

Diff for: starters/async-commons-starter/src/main/java/org/reactivecommons/async/starter/config/health/ReactiveCommonsHealthIndicator.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import lombok.AllArgsConstructor;
44
import lombok.extern.log4j.Log4j2;
5-
import org.reactivecommons.async.starter.config.ConnectionManager;
65
import org.reactivecommons.async.starter.broker.BrokerProvider;
6+
import org.reactivecommons.async.starter.config.ConnectionManager;
77
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator;
88
import org.springframework.boot.actuate.health.Health;
9-
import org.springframework.boot.actuate.health.Status;
109
import reactor.core.publisher.Flux;
1110
import reactor.core.publisher.Mono;
1211

@@ -22,14 +21,14 @@ public class ReactiveCommonsHealthIndicator extends AbstractReactiveHealthIndica
2221
protected Mono<Health> doHealthCheck(Health.Builder builder) {
2322
return Flux.fromIterable(manager.getProviders().values())
2423
.flatMap(BrokerProvider::healthCheck)
25-
.reduceWith(Health::up, (health, status) -> reduceHealth((Health.Builder) health, (Health) status))
24+
.reduceWith(Health::up, (health, status) -> reduceHealth((Health.Builder) health, (RCHealth) status))
2625
.map(b -> ((Health.Builder) b).build());
2726

2827
}
2928

30-
private Health.Builder reduceHealth(Health.Builder builder, Health status) {
29+
private Health.Builder reduceHealth(Health.Builder builder, RCHealth status) {
3130
String domain = status.getDetails().get(DOMAIN).toString();
32-
if (status.getStatus().equals(Status.DOWN)) {
31+
if (status.getStatus().equals(RCHealth.Status.DOWN)) {
3332
log.error("Broker of domain {} is down", domain);
3433
return builder.down().withDetail(domain, status.getDetails());
3534
}

Diff for: starters/async-commons-starter/src/test/java/org/reactivecommons/async/starter/config/health/ReactiveCommonsHealthIndicatorTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ void setUp() {
3737
@Test
3838
void shouldBeUp() {
3939
// Arrange
40-
when(brokerProvider.healthCheck()).thenReturn(Mono.just(Health.up()
40+
when(brokerProvider.healthCheck()).thenReturn(Mono.just(RCHealth.builder().up()
4141
.withDetail(DOMAIN, DEFAULT_DOMAIN)
4242
.withDetail(VERSION, "123")
4343
.build()));
44-
when(brokerProvider2.healthCheck()).thenReturn(Mono.just(Health.up()
44+
when(brokerProvider2.healthCheck()).thenReturn(Mono.just(RCHealth.builder().up()
4545
.withDetail(DOMAIN, OTHER)
4646
.withDetail(VERSION, "1234")
4747
.build()));
@@ -56,11 +56,11 @@ void shouldBeUp() {
5656
@Test
5757
void shouldBeDown() {
5858
// Arrange
59-
when(brokerProvider.healthCheck()).thenReturn(Mono.just(Health.up()
59+
when(brokerProvider.healthCheck()).thenReturn(Mono.just(RCHealth.builder().down()
6060
.withDetail(DOMAIN, DEFAULT_DOMAIN)
6161
.withDetail(VERSION, "123")
6262
.build()));
63-
when(brokerProvider2.healthCheck()).thenReturn(Mono.just(Health.down()
63+
when(brokerProvider2.healthCheck()).thenReturn(Mono.just(RCHealth.builder().up()
6464
.withDetail(DOMAIN, OTHER)
6565
.withDetail(VERSION, "1234")
6666
.build()));

Diff for: starters/async-commons-starter/src/test/java/org/reactivecommons/async/starter/mybroker/MyBrokerProvider.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import org.reactivecommons.async.commons.HandlerResolver;
77
import org.reactivecommons.async.starter.broker.BrokerProvider;
88
import org.reactivecommons.async.starter.broker.DiscardProvider;
9+
import org.reactivecommons.async.starter.config.health.RCHealth;
910
import org.reactivecommons.async.starter.mybroker.props.MyBrokerAsyncProps;
10-
import org.springframework.boot.actuate.health.Health;
1111
import reactor.core.publisher.Mono;
1212

1313
@AllArgsConstructor
@@ -57,7 +57,7 @@ public void listenReplies(HandlerResolver resolver) {
5757
}
5858

5959
@Override
60-
public Mono<Health> healthCheck() {
60+
public Mono<RCHealth> healthCheck() {
6161
return null;
6262
}
6363
}

Diff for: starters/async-kafka-starter/src/main/java/org/reactivecommons/async/kafka/KafkaBrokerProvider.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import org.reactivecommons.async.kafka.listeners.ApplicationEventListener;
2020
import org.reactivecommons.async.kafka.listeners.ApplicationNotificationsListener;
2121
import org.reactivecommons.async.starter.broker.BrokerProvider;
22-
import org.springframework.boot.actuate.health.Health;
22+
import org.reactivecommons.async.starter.config.health.RCHealth;
2323
import org.springframework.boot.ssl.SslBundles;
2424
import reactor.core.publisher.Mono;
2525

@@ -100,7 +100,7 @@ public void listenReplies(HandlerResolver resolver) {
100100
}
101101

102102
@Override
103-
public Mono<Health> healthCheck() {
103+
public Mono<RCHealth> healthCheck() {
104104
return healthIndicator.health();
105105
}
106106
}

Diff for: starters/async-kafka-starter/src/main/java/org/reactivecommons/async/kafka/health/KafkaReactiveHealthIndicator.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
import lombok.AllArgsConstructor;
44
import lombok.extern.log4j.Log4j2;
55
import org.apache.kafka.clients.admin.AdminClient;
6-
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator;
7-
import org.springframework.boot.actuate.health.Health;
6+
import org.reactivecommons.async.starter.config.health.RCHealth;
7+
import org.reactivecommons.async.starter.config.health.RCHealthIndicator;
88
import reactor.core.publisher.Mono;
99

1010
import static org.reactivecommons.async.starter.config.health.ReactiveCommonsHealthIndicator.DOMAIN;
1111
import static org.reactivecommons.async.starter.config.health.ReactiveCommonsHealthIndicator.VERSION;
1212

1313
@Log4j2
1414
@AllArgsConstructor
15-
public class KafkaReactiveHealthIndicator extends AbstractReactiveHealthIndicator {
15+
public class KafkaReactiveHealthIndicator extends RCHealthIndicator {
1616
private final String domain;
1717
private final AdminClient adminClient;
1818

1919
@Override
20-
protected Mono<Health> doHealthCheck(Health.Builder builder) {
20+
public Mono<RCHealth> doHealthCheck(RCHealth.RCHealthBuilder builder) {
2121
builder.withDetail(DOMAIN, domain);
2222
return checkKafkaHealth()
2323
.map(clusterId -> builder.up().withDetail(VERSION, clusterId).build())

Diff for: starters/async-kafka-starter/src/test/java/org/reactivecommons/async/kafka/KafkaBrokerProviderTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.reactivecommons.async.kafka.converters.json.KafkaJacksonMessageConverter;
2121
import org.reactivecommons.async.kafka.health.KafkaReactiveHealthIndicator;
2222
import org.reactivecommons.async.starter.broker.BrokerProvider;
23-
import org.springframework.boot.actuate.health.Health;
23+
import org.reactivecommons.async.starter.config.health.RCHealth;
2424
import org.springframework.boot.ssl.DefaultSslBundleRegistry;
2525
import org.springframework.boot.ssl.SslBundles;
2626
import reactor.core.publisher.Flux;
@@ -136,12 +136,12 @@ void shouldListenNotificationEvents() {
136136

137137
@Test
138138
void shouldProxyHealthCheck() {
139-
when(healthIndicator.health()).thenReturn(Mono.fromSupplier(() -> Health.up().build()));
139+
when(healthIndicator.health()).thenReturn(Mono.fromSupplier(() -> RCHealth.builder().up().build()));
140140
// Act
141-
Mono<Health> flow = brokerProvider.healthCheck();
141+
Mono<RCHealth> flow = brokerProvider.healthCheck();
142142
// Assert
143143
StepVerifier.create(flow)
144-
.expectNextMatches(health -> health.getStatus().getCode().equals("UP"))
144+
.expectNextMatches(health -> health.getStatus().equals(RCHealth.Status.UP))
145145
.verifyComplete();
146146
}
147147
}

Diff for: starters/async-kafka-starter/src/test/java/org/reactivecommons/async/kafka/health/KafkaReactiveHealthIndicatorTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.junit.jupiter.api.extension.ExtendWith;
1010
import org.mockito.Mock;
1111
import org.mockito.junit.jupiter.MockitoExtension;
12+
import org.reactivecommons.async.starter.config.health.RCHealth;
1213
import org.springframework.boot.actuate.health.Health;
1314
import org.springframework.boot.actuate.health.Health.Builder;
1415
import org.springframework.boot.actuate.health.Status;
@@ -39,13 +40,13 @@ void shouldBeUp() {
3940
when(adminClient.describeCluster()).thenReturn(describeClusterResult);
4041
when(describeClusterResult.clusterId()).thenReturn(KafkaFuture.completedFuture("cluster123"));
4142
// Act
42-
Mono<Health> result = indicator.doHealthCheck(new Builder());
43+
Mono<RCHealth> result = indicator.doHealthCheck(RCHealth.builder());
4344
// Assert
4445
StepVerifier.create(result)
4546
.assertNext(health -> {
4647
assertEquals(DEFAULT_DOMAIN, health.getDetails().get("domain"));
4748
assertEquals("cluster123", health.getDetails().get("version"));
48-
assertEquals(Status.UP, health.getStatus());
49+
assertEquals(RCHealth.Status.UP, health.getStatus());
4950
})
5051
.verifyComplete();
5152
}
@@ -58,12 +59,12 @@ void shouldBeDown() {
5859
future.completeExceptionally(new RuntimeException("simulate error"));
5960
when(describeClusterResult.clusterId()).thenReturn(future);
6061
// Act
61-
Mono<Health> result = indicator.doHealthCheck(new Builder());
62+
Mono<RCHealth> result = indicator.doHealthCheck(RCHealth.builder());
6263
// Assert
6364
StepVerifier.create(result)
6465
.expectNextMatches(health -> {
6566
assertEquals(DEFAULT_DOMAIN, health.getDetails().get("domain"));
66-
assertEquals(Status.DOWN, health.getStatus());
67+
assertEquals(RCHealth.Status.DOWN, health.getStatus());
6768
return true;
6869
})
6970
.verifyComplete();

Diff for: starters/async-rabbit-starter/src/main/java/org/reactivecommons/async/rabbit/RabbitMQBrokerProvider.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.reactivecommons.async.rabbit.listeners.ApplicationQueryListener;
2323
import org.reactivecommons.async.rabbit.listeners.ApplicationReplyListener;
2424
import org.reactivecommons.async.starter.broker.BrokerProvider;
25-
import org.springframework.boot.actuate.health.Health;
25+
import org.reactivecommons.async.starter.config.health.RCHealth;
2626
import reactor.core.publisher.Mono;
2727

2828
import static reactor.rabbitmq.ExchangeSpecification.exchange;
@@ -154,7 +154,7 @@ public void listenReplies(HandlerResolver resolver) {
154154
}
155155

156156
@Override
157-
public Mono<Health> healthCheck() {
157+
public Mono<RCHealth> healthCheck() {
158158
return healthIndicator.health();
159159
}
160160
}

Diff for: starters/async-rabbit-starter/src/main/java/org/reactivecommons/async/rabbit/health/RabbitReactiveHealthIndicator.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import com.rabbitmq.client.ConnectionFactory;
55
import lombok.SneakyThrows;
66
import lombok.extern.log4j.Log4j2;
7-
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator;
8-
import org.springframework.boot.actuate.health.Health;
7+
import org.reactivecommons.async.starter.config.health.RCHealth;
8+
import org.reactivecommons.async.starter.config.health.RCHealthIndicator;
99
import reactor.core.publisher.Mono;
1010

1111
import java.net.SocketException;
@@ -14,7 +14,7 @@
1414
import static org.reactivecommons.async.starter.config.health.ReactiveCommonsHealthIndicator.VERSION;
1515

1616
@Log4j2
17-
public class RabbitReactiveHealthIndicator extends AbstractReactiveHealthIndicator {
17+
public class RabbitReactiveHealthIndicator extends RCHealthIndicator {
1818
private final String domain;
1919
private final ConnectionFactory connectionFactory;
2020

@@ -25,7 +25,7 @@ public RabbitReactiveHealthIndicator(String domain, ConnectionFactory connection
2525
}
2626

2727
@Override
28-
protected Mono<Health> doHealthCheck(Health.Builder builder) {
28+
public Mono<RCHealth> doHealthCheck(RCHealth.RCHealthBuilder builder) {
2929
builder.withDetail(DOMAIN, domain);
3030
return Mono.fromCallable(() -> getRawVersion(connectionFactory))
3131
.map(status -> builder.up().withDetail(VERSION, status).build());

Diff for: starters/async-rabbit-starter/src/test/java/org/reactivecommons/async/rabbit/RabbitMQBrokerProviderTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.reactivecommons.async.rabbit.converters.json.RabbitJacksonMessageConverter;
2424
import org.reactivecommons.async.rabbit.health.RabbitReactiveHealthIndicator;
2525
import org.reactivecommons.async.starter.broker.BrokerProvider;
26-
import org.springframework.boot.actuate.health.Health;
26+
import org.reactivecommons.async.starter.config.health.RCHealth;
2727
import reactor.core.publisher.Flux;
2828
import reactor.core.publisher.Mono;
2929
import reactor.rabbitmq.BindingSpecification;
@@ -181,12 +181,12 @@ void shouldListenQueries() {
181181

182182
@Test
183183
void shouldProxyHealthCheck() {
184-
when(healthIndicator.health()).thenReturn(Mono.fromSupplier(() -> Health.up().build()));
184+
when(healthIndicator.health()).thenReturn(Mono.fromSupplier(() -> RCHealth.builder().up().build()));
185185
// Act
186-
Mono<Health> flow = brokerProvider.healthCheck();
186+
Mono<RCHealth> flow = brokerProvider.healthCheck();
187187
// Assert
188188
StepVerifier.create(flow)
189-
.expectNextMatches(health -> health.getStatus().getCode().equals("UP"))
189+
.expectNextMatches(health -> health.getStatus().equals(RCHealth.Status.UP))
190190
.verifyComplete();
191191
}
192192
}

0 commit comments

Comments
 (0)