Skip to content

Commit

Permalink
Issue ReactiveX#547: Added new metrics to Spring Boot HealthIndicator. (
Browse files Browse the repository at this point in the history
  • Loading branch information
RobWin authored Aug 16, 2019
1 parent b439290 commit c5771c1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,56 @@ public void shouldReleasePermissionWhenExceptionIgnored() {
assertCircuitBreakerMetricsEqualTo(-1f, 0, 0, 0, 0L);
}

@Test
public void shouldIgnoreExceptionsAndThenTransitionToClosed() {
circuitBreaker.transitionToOpenState();
circuitBreaker.transitionToHalfOpenState();
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
// Should ignore NumberFormatException and release permission
circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new NumberFormatException());
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
// Should ignore NumberFormatException and release permission
circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new NumberFormatException());
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
// Should ignore NumberFormatException and release permission
circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new NumberFormatException());
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
// Should ignore NumberFormatException and release permission
circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new NumberFormatException());
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);

assertCircuitBreakerMetricsEqualTo(-1f, 0, 0, 0, 0L);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS); //
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS); //
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS); //
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);

assertCircuitBreakerMetricsEqualTo(-1f, 3, 3, 0, 0L);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS); //
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);

assertCircuitBreakerMetricsEqualTo(-1f, 0, 0, 0, 0L);
}


@Test
public void shouldNotAllowTransitionFromClosedToHalfOpen() {
assertThatThrownBy(() -> circuitBreaker.transitionToHalfOpenState()).isInstanceOf(IllegalStateTransitionException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
public class CircuitBreakerHealthIndicator implements HealthIndicator {

private static final String FAILURE_RATE = "failureRate";
private static final String FAILURE_RATE_THRESHOLD = "setFailureRateThreshold";
private static final String SLOW_CALL_RATE = "slowCallRate";
private static final String FAILURE_RATE_THRESHOLD = "failureRateThreshold";
private static final String SLOW_CALL_RATE_THRESHOLD = "slowCallRateThreshold";
private static final String BUFFERED_CALLS = "bufferedCalls";
private static final String FAILED_CALLS = "failedCalls";
private static final String SLOW_CALLS = "slowCalls";
private static final String NOT_PERMITTED = "notPermittedCalls";
private static final String STATE = "state";
private final CircuitBreaker circuitBreaker;
Expand Down Expand Up @@ -65,7 +68,10 @@ private Health.Builder addDetails(Health.Builder builder, CircuitBreaker circuit
CircuitBreakerConfig config = circuitBreaker.getCircuitBreakerConfig();
builder.withDetail(FAILURE_RATE, metrics.getFailureRate() + "%")
.withDetail(FAILURE_RATE_THRESHOLD, config.getFailureRateThreshold() + "%")
.withDetail(SLOW_CALL_RATE, metrics.getSlowCallRate() + "%")
.withDetail(SLOW_CALL_RATE_THRESHOLD, config.getSlowCallRateThreshold() + "%")
.withDetail(BUFFERED_CALLS, metrics.getNumberOfBufferedCalls())
.withDetail(SLOW_CALLS, metrics.getNumberOfSlowCalls())
.withDetail(FAILED_CALLS, metrics.getNumberOfFailedCalls())
.withDetail(NOT_PERMITTED, metrics.getNumberOfNotPermittedCalls())
.withDetail(STATE, circuitBreaker.getState());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public void healthMetricsAndConfig() {
CircuitBreakerHealthIndicator healthIndicator = new CircuitBreakerHealthIndicator(circuitBreaker);

//when
when(config.getFailureRateThreshold()).thenReturn(0.3f);

when(metrics.getFailureRate()).thenReturn(0.2f);
when(config.getFailureRateThreshold()).thenReturn(30f);
when(metrics.getFailureRate()).thenReturn(20f);
when(metrics.getSlowCallRate()).thenReturn(20f);
when(config.getSlowCallRateThreshold()).thenReturn(50f);
when(metrics.getNumberOfBufferedCalls()).thenReturn(100);
when(metrics.getNumberOfFailedCalls()).thenReturn(20);
when(metrics.getNumberOfSlowCalls()).thenReturn(20);
when(metrics.getNumberOfNotPermittedCalls()).thenReturn(0L);

when(circuitBreaker.getCircuitBreakerConfig()).thenReturn(config);
Expand All @@ -45,9 +47,12 @@ public void healthMetricsAndConfig() {
then(health.getStatus()).isEqualTo(Status.UP);
then(health.getDetails())
.contains(
entry("failureRate", "0.2%"),
entry("setFailureRateThreshold", "0.3%"),
entry("failureRate", "20.0%"),
entry("slowCallRate", "20.0%"),
entry("slowCallRateThreshold", "50.0%"),
entry("failureRateThreshold", "30.0%"),
entry("bufferedCalls", 100),
entry("slowCalls", 20),
entry("failedCalls", 20),
entry("notPermittedCalls", 0L)
);
Expand Down

0 comments on commit c5771c1

Please sign in to comment.