Search in sources :

Example 1 with CircuitBreakerEvent

use of io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent in project resilience4j by resilience4j.

the class CircularEventConsumerTest method shouldNotBufferEvents.

@Test
public void shouldNotBufferEvents() {
    // Given
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
    CircularEventConsumer<CircuitBreakerEvent> ringBuffer = new CircularEventConsumer<>(2);
    assertThat(ringBuffer.getBufferedEvents()).isEmpty();
    circuitBreaker.onError(0, new RuntimeException("Bla"));
    circuitBreaker.onError(0, new RuntimeException("Bla"));
    circuitBreaker.onError(0, new RuntimeException("Bla"));
    // Subscription is too late
    circuitBreaker.getEventPublisher().onEvent(ringBuffer);
    // Then
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(3);
    // Should store 0 events, because Subscription was too late
    assertThat(ringBuffer.getBufferedEvents()).hasSize(0);
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) Test(org.junit.Test)

Example 2 with CircuitBreakerEvent

use of io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent in project resilience4j by resilience4j.

the class CircularEventConsumerTest method shouldBufferAllEvents.

@Test
public void shouldBufferAllEvents() {
    // Given
    CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom().ringBufferSizeInClosedState(3).recordFailure(throwable -> API.Match(throwable).of(Case($(instanceOf(WebServiceException.class)), true), Case($(), false))).build();
    CircuitBreaker circuitBreaker = CircuitBreaker.of("testName", circuitBreakerConfig);
    CircularEventConsumer<CircuitBreakerEvent> ringBuffer = new CircularEventConsumer<>(10);
    circuitBreaker.getEventPublisher().onEvent(ringBuffer);
    assertThat(ringBuffer.getBufferedEvents()).isEmpty();
    // When
    circuitBreaker.onSuccess(0);
    circuitBreaker.onError(0, new WebServiceException("Bla"));
    circuitBreaker.onError(0, new IOException("Bla"));
    circuitBreaker.onError(0, new WebServiceException("Bla"));
    // Then
    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);
    // Should store 3 events, because circuit emits 2 error events and one state transition event
    assertThat(ringBuffer.getBufferedEvents()).hasSize(7);
    assertThat(ringBuffer.getBufferedEvents()).extracting("eventType").containsExactly(Type.SUCCESS, Type.ERROR, Type.IGNORED_ERROR, Type.ERROR, Type.STATE_TRANSITION, Type.STATE_TRANSITION, Type.RESET);
// ringBuffer.getBufferedEvents().forEach(event -> LOG.info(event.toString()));
}
Also used : CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) WebServiceException(javax.xml.ws.WebServiceException) Predicates.instanceOf(io.vavr.Predicates.instanceOf) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Type(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type) Test(org.junit.Test) IOException(java.io.IOException) Case(io.vavr.API.Case) API(io.vavr.API) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) API.$(io.vavr.API.$) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) WebServiceException(javax.xml.ws.WebServiceException) IOException(java.io.IOException) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Test(org.junit.Test)

Example 3 with CircuitBreakerEvent

use of io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent 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 4 with CircuitBreakerEvent

use of io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent 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)

Example 5 with CircuitBreakerEvent

use of io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent in project resilience4j by resilience4j.

the class CircuitBreakerEventsEndpoint method getEventsStreamFilteredByCircuitBreakerNameAndEventType.

@RequestMapping(value = "stream/events/{circuitBreakerName}/{eventType}", produces = MEDIA_TYPE_TEXT_EVENT_STREAM)
public SseEmitter getEventsStreamFilteredByCircuitBreakerNameAndEventType(@PathVariable("circuitBreakerName") String circuitBreakerName, @PathVariable("eventType") String eventType) {
    CircuitBreaker circuitBreaker = circuitBreakerRegistry.getAllCircuitBreakers().find(cb -> cb.getName().equals(circuitBreakerName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("circuit breaker with name %s not found", circuitBreakerName)));
    Flux<CircuitBreakerEvent> eventStream = toFlux(circuitBreaker.getEventPublisher()).filter(event -> event.getEventType() == CircuitBreakerEvent.Type.valueOf(eventType.toUpperCase()));
    return CircuitBreakerEventEmitter.createSseEmitter(eventStream);
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) MediaType(org.springframework.http.MediaType) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Controller(org.springframework.stereotype.Controller) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) ReactorAdapter.toFlux(io.github.resilience4j.adapter.ReactorAdapter.toFlux) Flux(reactor.core.publisher.Flux) CircularEventConsumer(io.github.resilience4j.consumer.CircularEventConsumer) Seq(io.vavr.collection.Seq) Comparator(java.util.Comparator) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) EventConsumerRegistry(io.github.resilience4j.consumer.EventConsumerRegistry) SseEmitter(org.springframework.web.servlet.mvc.method.annotation.SseEmitter) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)5 CircuitBreakerEvent (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent)5 Test (org.junit.Test)3 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)2 CircularEventConsumer (io.github.resilience4j.consumer.CircularEventConsumer)2 EventConsumerRegistry (io.github.resilience4j.consumer.EventConsumerRegistry)2 Seq (io.vavr.collection.Seq)2 Comparator (java.util.Comparator)2 ReactorAdapter.toFlux (io.github.resilience4j.adapter.ReactorAdapter.toFlux)1 RxJava2Adapter (io.github.resilience4j.adapter.RxJava2Adapter)1 CircuitBreakerConfig (io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)1 Type (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type)1 Resilience4jConfig (io.github.resilience4j.ratpack.Resilience4jConfig)1 Flowable (io.reactivex.Flowable)1 API (io.vavr.API)1 API.$ (io.vavr.API.$)1 Case (io.vavr.API.Case)1 Predicates.instanceOf (io.vavr.Predicates.instanceOf)1 IOException (java.io.IOException)1 Inject (javax.inject.Inject)1