Question regarding Resilience4J, and how to log a circuit breaker state change please.
Currently, Resilience4j is working great. Having the ability to invoke a fallback when downstream systems are down, giving them time to breath, etc...
Unfortunately, I am facing an issue with my Spring Webflux 2.4.4+ app with
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-reactor</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
</dependency>
Suppose our circuit breaker exists closed mode at the time T
, due to the downstream system being unavailable for instance.
On subsequent calls, since the circuit is opened, goes into a fallback, and I have a log when the fallback method is being invoked.
Hence, I only know at T+1
when the circuit breaker has been opened. But not the exact moment the circuit breaker changed state.
I was wondering, how to log the event of the circuit breaker changing the state, at the moment it really changes state at info
level, please?
You can consume the emitted events. This can be found in the CircuitBreaker module documentation, section Consume emitted CircuitBreakerEvents.
A CircuitBreakerEvent can be a state transition, a circuit breaker reset, a successful call, a recorded error or an ignored error. All events contains additional information like event creation time and processing duration of the call. If you want to consume events, you have to register an event consumer.
You are interested in the onStateTransition
method (a full list of event consumers is here):
circuitBreaker.getEventPublisher()
.onStateTransition(e -> logger.info(e));
Feel free to log the whole event directly as it properly overrides the toString
method and a sample log record looks like this:
2021-06-01T01:23:45.678901+00:00: CircuitBreaker 'myService' changed state from CLOSED to OPEN