Search in sources :

Example 21 with CircuitBreaker

use of io.github.resilience4j.circuitbreaker.CircuitBreaker in project resilience4j by resilience4j.

the class CircularEventConsumerTest method shouldBufferErrorEvents.

@Test
public void shouldBufferErrorEvents() {
    // Given
    // tag::shouldBufferEvents[]
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
    CircularEventConsumer<CircuitBreakerEvent> ringBuffer = new CircularEventConsumer<>(2);
    circuitBreaker.getEventPublisher().onEvent(ringBuffer);
    // end::shouldBufferEvents[]
    assertThat(ringBuffer.getBufferedEvents()).isEmpty();
    // When
    circuitBreaker.onError(0, new RuntimeException("Bla"));
    circuitBreaker.onError(0, new RuntimeException("Bla"));
    circuitBreaker.onError(0, new RuntimeException("Bla"));
    // Then
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(3);
    // Should only store 2 events, because capacity is 2
    assertThat(ringBuffer.getBufferedEvents()).hasSize(2);
// ringBuffer.getBufferedEvents().forEach(event -> LOG.info(event.toString()));
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) Test(org.junit.Test)

Example 22 with CircuitBreaker

use of io.github.resilience4j.circuitbreaker.CircuitBreaker in project resilience4j by resilience4j.

the class CircuitBreakerExports method collect.

/**
 * {@inheritDoc}
 */
@Override
public List<MetricFamilySamples> collect() {
    final GaugeMetricFamily states = new GaugeMetricFamily(prefix + "_states", "Circuit Breaker States", asList("name", "state"));
    final GaugeMetricFamily calls = new GaugeMetricFamily(prefix + "_calls", "Circuit Breaker Call Stats", asList("name", "call_result"));
    for (CircuitBreaker circuitBreaker : circuitBreakersSupplier.get()) {
        STATE_NAME_MAP.forEach(e -> {
            final CircuitBreaker.State state = e._1;
            final String name = e._2;
            final double value = state == circuitBreaker.getState() ? 1.0 : 0.0;
            states.addMetric(asList(circuitBreaker.getName(), name), value);
        });
        final CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
        calls.addMetric(asList(circuitBreaker.getName(), "successful"), metrics.getNumberOfSuccessfulCalls());
        calls.addMetric(asList(circuitBreaker.getName(), "failed"), metrics.getNumberOfFailedCalls());
        calls.addMetric(asList(circuitBreaker.getName(), "not_permitted"), metrics.getNumberOfNotPermittedCalls());
        calls.addMetric(asList(circuitBreaker.getName(), "buffered"), metrics.getNumberOfBufferedCalls());
        calls.addMetric(asList(circuitBreaker.getName(), "buffered_max"), metrics.getMaxNumberOfBufferedCalls());
    }
    return asList(calls, states);
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) GaugeMetricFamily(io.prometheus.client.GaugeMetricFamily)

Example 23 with CircuitBreaker

use of io.github.resilience4j.circuitbreaker.CircuitBreaker in project resilience4j by resilience4j.

the class CircuitBreakerExportsTest method testExportsCircuitBreakerMetrics.

@Test
public void testExportsCircuitBreakerMetrics() {
    // Given
    final CollectorRegistry registry = new CollectorRegistry();
    final CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("foo");
    CircuitBreakerExports.ofIterable("boo_circuit_breaker", singletonList(circuitBreaker)).register(registry);
    final Supplier<Map<String, Double>> values = () -> HashSet.of("successful", "failed", "not_permitted", "buffered", "buffered_max").map(callType -> Tuple.of(callType, registry.getSampleValue("boo_circuit_breaker_calls", new String[] { "name", "call_result" }, new String[] { "foo", callType }))).toMap(t -> t);
    // When
    final Map<String, Double> initialValues = values.get();
    circuitBreaker.executeRunnable(() -> {
    });
    final Map<String, Double> afterSuccessValues = values.get();
    try {
        circuitBreaker.executeRunnable(() -> {
            throw new SomeAppException("Some exception");
        });
    } catch (RuntimeException e) {
    // expected
    }
    final Map<String, Double> afterFailureValues = values.get();
    circuitBreaker.transitionToOpenState();
    try {
        circuitBreaker.executeRunnable(() -> {
        });
    } catch (CircuitBreakerOpenException e) {
    // expected
    }
    final Map<String, Double> afterDeclinedValues = values.get();
    // Then
    assertThat(initialValues).isEqualTo(HashMap.of("successful", 0.0, "failed", 0.0, "not_permitted", 0.0, "buffered", 0.0, "buffered_max", 100.0));
    assertThat(afterSuccessValues).isEqualTo(HashMap.of("successful", 1.0, "failed", 0.0, "not_permitted", 0.0, "buffered", 1.0, "buffered_max", 100.0));
    assertThat(afterFailureValues).isEqualTo(HashMap.of("successful", 1.0, "failed", 1.0, "not_permitted", 0.0, "buffered", 2.0, "buffered_max", 100.0));
    assertThat(afterDeclinedValues).isEqualTo(HashMap.of("successful", 1.0, "failed", 1.0, "not_permitted", 1.0, "buffered", 2.0, "buffered_max", 100.0));
}
Also used : Tuple(io.vavr.Tuple) CollectorRegistry(io.prometheus.client.CollectorRegistry) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) InMemoryCircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry) HashMap(io.vavr.collection.HashMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Supplier(java.util.function.Supplier) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) HashSet(io.vavr.collection.HashSet) Map(io.vavr.collection.Map) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CollectorRegistry(io.prometheus.client.CollectorRegistry) HashMap(io.vavr.collection.HashMap) Map(io.vavr.collection.Map) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException) Test(org.junit.Test)

Example 24 with CircuitBreaker

use of io.github.resilience4j.circuitbreaker.CircuitBreaker in project resilience4j by resilience4j.

the class CircuitBreakerMetrics method bindTo.

@Override
public void bindTo(MeterRegistry registry) {
    for (CircuitBreaker circuitBreaker : circuitBreakers) {
        final String name = circuitBreaker.getName();
        Gauge.builder(getName(prefix, name, STATE), circuitBreaker, (cb) -> cb.getState().getOrder()).register(registry);
        Gauge.builder(getName(prefix, name, BUFFERED_MAX), circuitBreaker, (cb) -> cb.getMetrics().getMaxNumberOfBufferedCalls()).register(registry);
        Gauge.builder(getName(prefix, name, BUFFERED), circuitBreaker, (cb) -> cb.getMetrics().getNumberOfBufferedCalls()).register(registry);
        Gauge.builder(getName(prefix, name, FAILED), circuitBreaker, (cb) -> cb.getMetrics().getNumberOfFailedCalls()).register(registry);
        Gauge.builder(getName(prefix, name, NOT_PERMITTED), circuitBreaker, (cb) -> cb.getMetrics().getNumberOfNotPermittedCalls()).register(registry);
        Gauge.builder(getName(prefix, name, SUCCESSFUL), circuitBreaker, (cb) -> cb.getMetrics().getNumberOfSuccessfulCalls()).register(registry);
    }
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Gauge(io.micrometer.core.instrument.Gauge) MetricUtils.getName(io.github.resilience4j.micrometer.MetricUtils.getName) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Objects.requireNonNull(java.util.Objects.requireNonNull) MeterBinder(io.micrometer.core.instrument.binder.MeterBinder) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) MetricNames(io.github.resilience4j.circuitbreaker.utils.MetricNames) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker)

Example 25 with CircuitBreaker

use of io.github.resilience4j.circuitbreaker.CircuitBreaker in project resilience4j by resilience4j.

the class CircuitBreakerChain method execute.

@Override
public void execute(Chain chain) throws Exception {
    String prefix = chain.getRegistry().get(Resilience4jConfig.class).getEndpoints().getCircuitBreakers().getPath();
    chain.prefix(prefix, chain1 -> {
        chain1.get("events", ctx -> Promise.<CircuitBreakerEventsEndpointResponse>async(d -> {
            CircuitBreakerEventsEndpointResponse response = new CircuitBreakerEventsEndpointResponse(eventConsumerRegistry.getAllEventConsumer().flatMap(CircularEventConsumer::getBufferedEvents).sorted(Comparator.comparing(CircuitBreakerEvent::getCreationTime)).map(CircuitBreakerEventDTOFactory::createCircuitBreakerEventDTO).toJavaList());
            d.success(response);
        }).then(r -> ctx.render(Jackson.json(r))));
        chain1.get("stream/events", ctx -> {
            Seq<Flowable<CircuitBreakerEvent>> eventStreams = circuitBreakerRegistry.getAllCircuitBreakers().map(circuitBreaker -> RxJava2Adapter.toFlowable(circuitBreaker.getEventPublisher()));
            Function<CircuitBreakerEvent, String> data = c -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(CircuitBreakerEventDTOFactory.createCircuitBreakerEventDTO(c));
            ServerSentEvents events = ServerSentEvents.serverSentEvents(Flowable.merge(eventStreams), e -> e.id(CircuitBreakerEvent::getCircuitBreakerName).event(c -> c.getEventType().name()).data(data));
            ctx.render(events);
        });
        chain1.get("events/:name", ctx -> {
            String circuitBreakerName = ctx.getPathTokens().get("name");
            Promise.<CircuitBreakerEventsEndpointResponse>async(d -> {
                CircuitBreakerEventsEndpointResponse response = new CircuitBreakerEventsEndpointResponse(eventConsumerRegistry.getEventConsumer(circuitBreakerName).getBufferedEvents().map(CircuitBreakerEventDTOFactory::createCircuitBreakerEventDTO).toJavaList());
                d.success(response);
            }).then(r -> ctx.render(Jackson.json(r)));
        });
        chain1.get("stream/events/:name", ctx -> {
            String circuitBreakerName = ctx.getPathTokens().get("name");
            CircuitBreaker circuitBreaker = circuitBreakerRegistry.getAllCircuitBreakers().find(cb -> cb.getName().equals(circuitBreakerName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("circuit breaker with name %s not found", circuitBreakerName)));
            Function<CircuitBreakerEvent, String> data = c -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(CircuitBreakerEventDTOFactory.createCircuitBreakerEventDTO(c));
            ServerSentEvents events = ServerSentEvents.serverSentEvents(RxJava2Adapter.toFlowable(circuitBreaker.getEventPublisher()), e -> e.id(CircuitBreakerEvent::getCircuitBreakerName).event(c -> c.getEventType().name()).data(data));
            ctx.render(events);
        });
        chain1.get("events/:name/:type", ctx -> {
            String circuitBreakerName = ctx.getPathTokens().get("name");
            String eventType = ctx.getPathTokens().get("type");
            Promise.<CircuitBreakerEventsEndpointResponse>async(d -> {
                CircuitBreakerEventsEndpointResponse response = new CircuitBreakerEventsEndpointResponse(eventConsumerRegistry.getEventConsumer(circuitBreakerName).getBufferedEvents().filter(event -> event.getEventType() == CircuitBreakerEvent.Type.valueOf(eventType.toUpperCase())).map(CircuitBreakerEventDTOFactory::createCircuitBreakerEventDTO).toJavaList());
                d.success(response);
            }).then(r -> ctx.render(Jackson.json(r)));
        });
        chain1.get("stream/events/:name/:type", ctx -> {
            String circuitBreakerName = ctx.getPathTokens().get("name");
            String eventType = ctx.getPathTokens().get("type");
            CircuitBreaker circuitBreaker = circuitBreakerRegistry.getAllCircuitBreakers().find(cb -> cb.getName().equals(circuitBreakerName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("circuit breaker with name %s not found", circuitBreakerName)));
            Flowable<CircuitBreakerEvent> eventStream = RxJava2Adapter.toFlowable(circuitBreaker.getEventPublisher()).filter(event -> event.getEventType() == CircuitBreakerEvent.Type.valueOf(eventType.toUpperCase()));
            Function<CircuitBreakerEvent, String> data = c -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(CircuitBreakerEventDTOFactory.createCircuitBreakerEventDTO(c));
            ServerSentEvents events = ServerSentEvents.serverSentEvents(eventStream, e -> e.id(CircuitBreakerEvent::getCircuitBreakerName).event(c -> c.getEventType().name()).data(data));
            ctx.render(events);
        });
    });
}
Also used : Function(ratpack.func.Function) RxJava2Adapter(io.github.resilience4j.adapter.RxJava2Adapter) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Promise(ratpack.exec.Promise) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) Resilience4jConfig(io.github.resilience4j.ratpack.Resilience4jConfig) Jackson(ratpack.jackson.Jackson) ServerSentEvents(ratpack.sse.ServerSentEvents) Chain(ratpack.handling.Chain) Inject(javax.inject.Inject) Flowable(io.reactivex.Flowable) CircularEventConsumer(io.github.resilience4j.consumer.CircularEventConsumer) Action(ratpack.func.Action) Seq(io.vavr.collection.Seq) Comparator(java.util.Comparator) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) EventConsumerRegistry(io.github.resilience4j.consumer.EventConsumerRegistry) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Resilience4jConfig(io.github.resilience4j.ratpack.Resilience4jConfig) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) CircularEventConsumer(io.github.resilience4j.consumer.CircularEventConsumer) ServerSentEvents(ratpack.sse.ServerSentEvents) Flowable(io.reactivex.Flowable)

Aggregations

CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)26 Test (org.junit.Test)19 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)9 CircuitBreakerConfig (io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)6 CircuitBreakerEvent (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent)6 CircuitBreakerOpenException (io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException)3 InMemoryCircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry)3 Type (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type)2 CircularEventConsumer (io.github.resilience4j.consumer.CircularEventConsumer)2 EventConsumerRegistry (io.github.resilience4j.consumer.EventConsumerRegistry)2 CollectorRegistry (io.prometheus.client.CollectorRegistry)2 CheckedRunnable (io.vavr.CheckedRunnable)2 Tuple (io.vavr.Tuple)2 HashMap (io.vavr.collection.HashMap)2 HashSet (io.vavr.collection.HashSet)2 Map (io.vavr.collection.Map)2 Seq (io.vavr.collection.Seq)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2