Java Code Examples for io.github.resilience4j.circuitbreaker.CircuitBreaker#getMetrics()
The following examples show how to use
io.github.resilience4j.circuitbreaker.CircuitBreaker#getMetrics() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CircularEventConsumerTest.java From resilience4j with Apache License 2.0 | 7 votes |
@Test public void shouldNotBufferEvents() { CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName"); CircularEventConsumer<CircuitBreakerEvent> ringBuffer = new CircularEventConsumer<>(2); assertThat(ringBuffer.getBufferedEvents()).isEmpty(); circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla")); circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla")); circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla")); //Subscription is too late circuitBreaker.getEventPublisher().onEvent(ringBuffer); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(3); //Because Subscription was too late assertThat(ringBuffer.getBufferedEvents()).hasSize(0); }
Example 2
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecorateCheckedSupplierWithFallback() throws Throwable { CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); circuitBreaker.transitionToOpenState(); CheckedFunction0<String> checkedSupplier = Decorators .ofCheckedSupplier(() -> helloWorldService.returnHelloWorldWithException()) .withCircuitBreaker(circuitBreaker) .withFallback(CallNotPermittedException.class, e -> "Fallback") .decorate(); String result = checkedSupplier.apply(); assertThat(result).isEqualTo("Fallback"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfNotPermittedCalls()).isEqualTo(1); then(helloWorldService).should(never()).returnHelloWorld(); }
Example 3
Source File: CircularEventConsumerTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void shouldBufferErrorEvents() { // tag::shouldBufferEvents[] CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName"); CircularEventConsumer<CircuitBreakerEvent> ringBuffer = new CircularEventConsumer<>(2); circuitBreaker.getEventPublisher().onEvent(ringBuffer); // end::shouldBufferEvents[] assertThat(ringBuffer.getBufferedEvents()).isEmpty(); circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla")); circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla")); circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla")); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(3); //Because capacity is 2 assertThat(ringBuffer.getBufferedEvents()).hasSize(2); }
Example 4
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecorateFunction() { given(helloWorldService.returnHelloWorldWithName("Name")).willReturn("Hello world Name"); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Function<String, String> decoratedFunction = Decorators .ofFunction(helloWorldService::returnHelloWorldWithName) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); String result = decoratedFunction.apply("Name"); assertThat(result).isEqualTo("Hello world Name"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); }
Example 5
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testExecuteConsumer() { CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Decorators.DecorateConsumer<String> decoratedConsumer = Decorators.ofConsumer((String input) -> helloWorldService .sayHelloWorldWithName(input)) .withCircuitBreaker(circuitBreaker) .withBulkhead(Bulkhead.ofDefaults("testName")) .withRateLimiter(RateLimiter.ofDefaults("testName")); decoratedConsumer.accept("test"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); then(helloWorldService).should(times(1)).sayHelloWorldWithName("test"); }
Example 6
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecorateCompletionStage() throws ExecutionException, InterruptedException { given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Supplier<CompletionStage<String>> completionStageSupplier = () -> CompletableFuture.supplyAsync(helloWorldService::returnHelloWorld); CompletionStage<String> completionStage = Decorators .ofCompletionStage(completionStageSupplier) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id"), Executors.newSingleThreadScheduledExecutor()) .withBulkhead(Bulkhead.ofDefaults("testName")) .get(); String value = completionStage.toCompletableFuture().get(); assertThat(value).isEqualTo("Hello world"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); then(helloWorldService).should(times(1)).returnHelloWorld(); }
Example 7
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecorateCheckedRunnable() throws IOException { CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); CheckedRunnable decoratedRunnable = Decorators .ofCheckedRunnable(() -> helloWorldService.sayHelloWorldWithException()) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); Try.run(decoratedRunnable); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); then(helloWorldService).should(times(1)).sayHelloWorldWithException(); }
Example 8
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecorateRunnable() { CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Runnable decoratedRunnable = Decorators .ofRunnable(() -> helloWorldService.sayHelloWorld()) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); decoratedRunnable.run(); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); then(helloWorldService).should(times(1)).sayHelloWorld(); }
Example 9
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecorateCompletionStageWithTimeoutExceptionFallback() throws ExecutionException, InterruptedException { TimeLimiter timeLimiter = TimeLimiter.of("helloBackend", TimeLimiterConfig.custom() .timeoutDuration(Duration.ofMillis(100)).build()); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend"); CompletionStage<String> completionStage = Decorators .ofCallable(() -> { Thread.sleep(1000); return "Bla"; }) .withThreadPoolBulkhead(bulkhead) .withTimeLimiter(timeLimiter, Executors.newSingleThreadScheduledExecutor()) .withCircuitBreaker(circuitBreaker) .withFallback(TimeoutException.class, (e) -> "Fallback") .get(); String result = completionStage.toCompletableFuture().get(); assertThat(result).isEqualTo("Fallback"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1); }
Example 10
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecorateCompletionStageWithCallNotPermittedExceptionFallback() throws ExecutionException, InterruptedException { CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); circuitBreaker.transitionToOpenState(); ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend"); CompletionStage<String> completionStage = Decorators .ofSupplier(() -> helloWorldService.returnHelloWorld()) .withThreadPoolBulkhead(bulkhead) .withCircuitBreaker(circuitBreaker) .withFallback(CallNotPermittedException.class, (e) -> "Fallback") .get(); String result = completionStage.toCompletableFuture().get(); assertThat(result).isEqualTo("Fallback"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfNotPermittedCalls()).isEqualTo(1); }
Example 11
Source File: FeignDecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testWithCircuitBreaker() throws Throwable { final CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("test"); final CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); final FeignDecorators testSubject = FeignDecorators.builder() .withCircuitBreaker(circuitBreaker).build(); final Object result = testSubject.decorate(args -> args[0], null, null, null) .apply(new Object[]{"test01"}); assertThat(result) .describedAs("Returned result is correct") .isEqualTo("test01"); assertThat(metrics.getNumberOfSuccessfulCalls()) .describedAs("Successful Calls") .isEqualTo(1); }
Example 12
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecorateCheckedSupplier() throws IOException { given(helloWorldService.returnHelloWorldWithException()).willReturn("Hello world"); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); CheckedFunction0<String> decoratedSupplier = Decorators .ofCheckedSupplier(() -> helloWorldService.returnHelloWorldWithException()) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); String result = Try.of(decoratedSupplier).get(); assertThat(result).isEqualTo("Hello world"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); then(helloWorldService).should(times(1)).returnHelloWorldWithException(); }
Example 13
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecoratorBuilderWithRetry() { given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!")); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Supplier<String> decoratedSupplier = Decorators .ofSupplier(() -> helloWorldService.returnHelloWorld()) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); Try.of(decoratedSupplier::get); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(3); then(helloWorldService).should(times(3)).returnHelloWorld(); }
Example 14
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecorateCallable() throws Exception { given(helloWorldService.returnHelloWorldWithException()).willReturn("Hello world"); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Callable<String> decoratedCallable = Decorators .ofCallable(() -> helloWorldService.returnHelloWorldWithException()) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); String result = decoratedCallable.call(); assertThat(result).isEqualTo("Hello world"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); then(helloWorldService).should(times(1)).returnHelloWorldWithException(); }
Example 15
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecorateRunnableWithThreadPoolBulkhead() throws ExecutionException, InterruptedException { CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); CompletableFuture<Void> future = Decorators .ofRunnable(() -> helloWorldService.sayHelloWorld()) .withThreadPoolBulkhead(ThreadPoolBulkhead.ofDefaults("helloBackend")) .withCircuitBreaker(circuitBreaker) .get().toCompletableFuture(); future.get(); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); then(helloWorldService).should(times(1)).sayHelloWorld(); }
Example 16
Source File: DecoratorsTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void testDecorateSupplierWithThreadPoolBulkhead() throws ExecutionException, InterruptedException { given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); CompletableFuture<String> future = Decorators .ofSupplier(() -> helloWorldService.returnHelloWorld()) .withThreadPoolBulkhead(ThreadPoolBulkhead.ofDefaults("helloBackend")) .withTimeLimiter(TimeLimiter.ofDefaults(), Executors.newSingleThreadScheduledExecutor()) .withCircuitBreaker(circuitBreaker) .get().toCompletableFuture(); String result = future.get(); assertThat(result).isEqualTo("Hello world"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); then(helloWorldService).should(times(1)).returnHelloWorld(); }
Example 17
Source File: CircuitBreakersHealthIndicator.java From resilience4j with Apache License 2.0 | 6 votes |
private static Health.Builder addDetails(Health.Builder builder, CircuitBreaker circuitBreaker) { CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); 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(SLOW_FAILED_CALLS, metrics.getNumberOfSlowFailedCalls()) .withDetail(FAILED_CALLS, metrics.getNumberOfFailedCalls()) .withDetail(NOT_PERMITTED, metrics.getNumberOfNotPermittedCalls()) .withDetail(STATE, circuitBreaker.getState()); return builder; }
Example 18
Source File: CircularEventConsumerTest.java From resilience4j with Apache License 2.0 | 5 votes |
@Test public void shouldBufferAllEvents() { CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom() .slidingWindowSize(3) .ignoreExceptions(IOException.class) .build(); CircuitBreaker circuitBreaker = CircuitBreaker.of("testName", circuitBreakerConfig); CircularEventConsumer<CircuitBreakerEvent> ringBuffer = new CircularEventConsumer<>(10); circuitBreaker.getEventPublisher().onEvent(ringBuffer); assertThat(ringBuffer.getBufferedEvents()).isEmpty(); circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS); circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla")); circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new IOException("Bla")); circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla")); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(2); circuitBreaker.reset(); CircuitBreaker.Metrics resetMetrics = circuitBreaker.getMetrics(); assertThat(resetMetrics.getNumberOfBufferedCalls()).isEqualTo(0); assertThat(resetMetrics.getNumberOfFailedCalls()).isEqualTo(0); //Because circuit emits 2 error events and one state transition event assertThat(ringBuffer.getBufferedEvents()).hasSize(8); assertThat(ringBuffer.getBufferedEvents()).extracting("eventType") .containsExactly(Type.SUCCESS, Type.ERROR, Type.IGNORED_ERROR, Type.ERROR, Type.FAILURE_RATE_EXCEEDED, Type.STATE_TRANSITION, Type.STATE_TRANSITION, Type.RESET); }
Example 19
Source File: CircuitBreakerStateMachineTest.java From resilience4j with Apache License 2.0 | 5 votes |
private void assertCircuitBreakerMetricsEqualTo(CircuitBreaker toTest, Float expectedFailureRate, Integer expectedSuccessCalls, Integer expectedBufferedCalls, Integer expectedFailedCalls, Long expectedNotPermittedCalls) { final CircuitBreaker.Metrics metrics = toTest.getMetrics(); assertThat(metrics.getFailureRate()).isEqualTo(expectedFailureRate); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(expectedSuccessCalls); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(expectedBufferedCalls); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(expectedFailedCalls); assertThat(metrics.getNumberOfNotPermittedCalls()).isEqualTo(expectedNotPermittedCalls); }
Example 20
Source File: AbstractCircuitBreakerMetrics.java From resilience4j with Apache License 2.0 | 4 votes |
protected List<MetricFamilySamples> collectGaugeSamples(List<CircuitBreaker> circuitBreakers) { GaugeMetricFamily stateFamily = new GaugeMetricFamily( names.getStateMetricName(), "The state of the circuit breaker:", NAME_AND_STATE ); GaugeMetricFamily bufferedCallsFamily = new GaugeMetricFamily( names.getBufferedCallsMetricName(), "The number of buffered calls", LabelNames.NAME_AND_KIND ); GaugeMetricFamily slowCallsFamily = new GaugeMetricFamily( names.getSlowCallsMetricName(), "The number of slow calls", LabelNames.NAME_AND_KIND ); GaugeMetricFamily failureRateFamily = new GaugeMetricFamily( names.getFailureRateMetricName(), "The failure rate", LabelNames.NAME ); GaugeMetricFamily slowCallRateFamily = new GaugeMetricFamily( names.getSlowCallRateMetricName(), "The slow call rate", LabelNames.NAME ); for (CircuitBreaker circuitBreaker : circuitBreakers) { final CircuitBreaker.State[] states = CircuitBreaker.State.values(); for (CircuitBreaker.State state : states) { stateFamily.addMetric(asList(circuitBreaker.getName(), state.name().toLowerCase()), circuitBreaker.getState() == state ? 1 : 0); } List<String> nameLabel = Collections.singletonList(circuitBreaker.getName()); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); bufferedCallsFamily.addMetric(asList(circuitBreaker.getName(), KIND_SUCCESSFUL), metrics.getNumberOfSuccessfulCalls()); bufferedCallsFamily.addMetric(asList(circuitBreaker.getName(), KIND_FAILED), metrics.getNumberOfFailedCalls()); slowCallsFamily.addMetric(asList(circuitBreaker.getName(), KIND_SUCCESSFUL), metrics.getNumberOfSlowSuccessfulCalls()); slowCallsFamily.addMetric(asList(circuitBreaker.getName(), KIND_FAILED), metrics.getNumberOfSlowFailedCalls()); failureRateFamily.addMetric(nameLabel, metrics.getFailureRate()); slowCallRateFamily.addMetric(nameLabel, metrics.getSlowCallRate()); } return asList(stateFamily, bufferedCallsFamily, slowCallsFamily, failureRateFamily, slowCallRateFamily); }