Skip to content

Commit ed61752

Browse files
Andrew FromRobWin
Andrew From
authored andcommitted
add getting current circuit state ratpack endpoint (ReactiveX#319)
1 parent 1c76944 commit ed61752

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed

resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/circuitbreaker/endpoint/CircuitBreakerChain.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import io.github.resilience4j.consumer.EventConsumerRegistry;
2424
import io.github.resilience4j.ratpack.Resilience4jConfig;
2525
import io.github.resilience4j.ratpack.adapter.ReactorAdapter;
26+
import io.github.resilience4j.ratpack.circuitbreaker.endpoint.metrics.CircuitBreakerMetricsDTO;
27+
import io.github.resilience4j.ratpack.circuitbreaker.endpoint.states.CircuitBreakerStateDTO;
28+
import io.github.resilience4j.ratpack.circuitbreaker.endpoint.states.CircuitBreakerStatesEndpointResponse;
2629
import io.vavr.collection.Seq;
2730
import ratpack.exec.Promise;
2831
import ratpack.func.Action;
@@ -53,6 +56,28 @@ public CircuitBreakerChain(EventConsumerRegistry<CircuitBreakerEvent> eventConsu
5356
public void execute(Chain chain) throws Exception {
5457
String prefix = chain.getRegistry().get(Resilience4jConfig.class).getEndpoints().getCircuitBreakers().getPath();
5558
chain.prefix(prefix, chain1 -> {
59+
chain1.get("states/:name", ctx -> {
60+
String circuitBreakerName = ctx.getPathTokens().get("name");
61+
Promise.<CircuitBreakerStatesEndpointResponse>async(d -> {
62+
CircuitBreakerStatesEndpointResponse response = new CircuitBreakerStatesEndpointResponse(circuitBreakerRegistry
63+
.getAllCircuitBreakers()
64+
.filter(c -> c.getName().equals(circuitBreakerName))
65+
.map(c -> new CircuitBreakerStateDTO(c.getName(), c.getState(), new CircuitBreakerMetricsDTO(c.getMetrics())))
66+
.toJavaList()
67+
);
68+
d.success(response);
69+
}).then(r -> ctx.render(Jackson.json(r)));
70+
});
71+
chain1.get("states", ctx ->
72+
Promise.<CircuitBreakerStatesEndpointResponse>async(d -> {
73+
CircuitBreakerStatesEndpointResponse response = new CircuitBreakerStatesEndpointResponse(circuitBreakerRegistry
74+
.getAllCircuitBreakers()
75+
.map(c -> new CircuitBreakerStateDTO(c.getName(), c.getState(), new CircuitBreakerMetricsDTO(c.getMetrics())))
76+
.toJavaList()
77+
);
78+
d.success(response);
79+
}).then(r -> ctx.render(Jackson.json(r)))
80+
);
5681
chain1.get("events", ctx ->
5782
Promise.<CircuitBreakerEventsEndpointResponse>async(d -> {
5883
CircuitBreakerEventsEndpointResponse response = new CircuitBreakerEventsEndpointResponse(eventConsumerRegistry
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2019 Andrew From
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.resilience4j.ratpack.circuitbreaker.endpoint.metrics;
18+
19+
import com.fasterxml.jackson.annotation.JsonInclude;
20+
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
21+
22+
@JsonInclude(JsonInclude.Include.NON_NULL)
23+
public class CircuitBreakerMetricsDTO {
24+
private float failureRate;
25+
private int numberOfBufferedCalls;
26+
private int numberOfFailedCalls;
27+
private long numberOfNotPermittedCalls;
28+
private int maxNumberOfBufferedCalls;
29+
private int numberOfSuccessfulCalls;
30+
31+
CircuitBreakerMetricsDTO() {
32+
}
33+
34+
public CircuitBreakerMetricsDTO(CircuitBreaker.Metrics metrics) {
35+
this.failureRate = metrics.getFailureRate();
36+
this.numberOfBufferedCalls = metrics.getNumberOfBufferedCalls();
37+
this.numberOfFailedCalls = metrics.getNumberOfFailedCalls();
38+
this.numberOfNotPermittedCalls = metrics.getNumberOfNotPermittedCalls();
39+
this.maxNumberOfBufferedCalls = metrics.getMaxNumberOfBufferedCalls();
40+
this.numberOfSuccessfulCalls = metrics.getNumberOfSuccessfulCalls();
41+
}
42+
43+
public float getFailureRate() {
44+
return failureRate;
45+
}
46+
47+
public void setFailureRate(float failureRate) {
48+
this.failureRate = failureRate;
49+
}
50+
51+
public int getNumberOfBufferedCalls() {
52+
return numberOfBufferedCalls;
53+
}
54+
55+
public void setNumberOfBufferedCalls(int numberOfBufferedCalls) {
56+
this.numberOfBufferedCalls = numberOfBufferedCalls;
57+
}
58+
59+
public int getNumberOfFailedCalls() {
60+
return numberOfFailedCalls;
61+
}
62+
63+
public void setNumberOfFailedCalls(int numberOfFailedCalls) {
64+
this.numberOfFailedCalls = numberOfFailedCalls;
65+
}
66+
67+
public long getNumberOfNotPermittedCalls() {
68+
return numberOfNotPermittedCalls;
69+
}
70+
71+
public void setNumberOfNotPermittedCalls(long numberOfNotPermittedCalls) {
72+
this.numberOfNotPermittedCalls = numberOfNotPermittedCalls;
73+
}
74+
75+
public int getMaxNumberOfBufferedCalls() {
76+
return maxNumberOfBufferedCalls;
77+
}
78+
79+
public void setMaxNumberOfBufferedCalls(int maxNumberOfBufferedCalls) {
80+
this.maxNumberOfBufferedCalls = maxNumberOfBufferedCalls;
81+
}
82+
83+
public int getNumberOfSuccessfulCalls() {
84+
return numberOfSuccessfulCalls;
85+
}
86+
87+
public void setNumberOfSuccessfulCalls(int numberOfSuccessfulCalls) {
88+
this.numberOfSuccessfulCalls = numberOfSuccessfulCalls;
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2019 Andrew From
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.resilience4j.ratpack.circuitbreaker.endpoint.states;
18+
19+
import com.fasterxml.jackson.annotation.JsonInclude;
20+
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
21+
import io.github.resilience4j.ratpack.circuitbreaker.endpoint.metrics.CircuitBreakerMetricsDTO;
22+
23+
24+
@JsonInclude(JsonInclude.Include.NON_NULL)
25+
public class CircuitBreakerStateDTO {
26+
27+
private String circuitBreakerName;
28+
private CircuitBreaker.State currentState;
29+
private CircuitBreakerMetricsDTO metrics;
30+
31+
CircuitBreakerStateDTO() {
32+
}
33+
34+
public CircuitBreakerStateDTO(String circuitBreakerName, CircuitBreaker.State currentState, CircuitBreakerMetricsDTO metrics) {
35+
this.circuitBreakerName = circuitBreakerName;
36+
this.currentState = currentState;
37+
this.metrics = metrics;
38+
}
39+
40+
public String getCircuitBreakerName() {
41+
return circuitBreakerName;
42+
}
43+
44+
public void setCircuitBreakerName(String circuitBreakerName) {
45+
this.circuitBreakerName = circuitBreakerName;
46+
}
47+
48+
public CircuitBreaker.State getCurrentState() {
49+
return currentState;
50+
}
51+
52+
public void setCurrentState(CircuitBreaker.State currentState) {
53+
this.currentState = currentState;
54+
}
55+
56+
public CircuitBreakerMetricsDTO getMetrics() {
57+
return metrics;
58+
}
59+
60+
public void setMetrics(CircuitBreakerMetricsDTO metrics) {
61+
this.metrics = metrics;
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2019 Andrew From
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.resilience4j.ratpack.circuitbreaker.endpoint.states;
18+
19+
import java.util.List;
20+
21+
public class CircuitBreakerStatesEndpointResponse {
22+
private List<CircuitBreakerStateDTO> circuitBreakerStates;
23+
24+
public CircuitBreakerStatesEndpointResponse() {
25+
}
26+
27+
public CircuitBreakerStatesEndpointResponse(List<CircuitBreakerStateDTO> circuitBreakerStates){
28+
this.circuitBreakerStates = circuitBreakerStates;
29+
}
30+
31+
public List<CircuitBreakerStateDTO> getCircuitBreakerStates() {
32+
return circuitBreakerStates;
33+
}
34+
35+
public void setCircuitBreakerStates(List<CircuitBreakerStateDTO> circuitBreakerStates) {
36+
this.circuitBreakerStates = circuitBreakerStates;
37+
}
38+
}

resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/circuitbreaker/endpoint/CircuitBreakerChainSpec.groovy

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package io.github.resilience4j.ratpack.circuitbreaker.endpoint
1919
import com.fasterxml.jackson.databind.ObjectMapper
2020
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry
2121
import io.github.resilience4j.ratpack.Resilience4jModule
22+
import io.github.resilience4j.ratpack.circuitbreaker.endpoint.states.CircuitBreakerStatesEndpointResponse
2223
import ratpack.http.client.HttpClient
2324
import ratpack.test.embed.EmbeddedApp
2425
import ratpack.test.exec.ExecHarness
@@ -40,6 +41,68 @@ class CircuitBreakerChainSpec extends Specification {
4041

4142
def mapper = new ObjectMapper()
4243

44+
def "test states"() {
45+
given: "a ratpack app"
46+
def circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults()
47+
48+
app = ratpack {
49+
serverConfig {
50+
development(false)
51+
}
52+
bindings {
53+
bindInstance(CircuitBreakerRegistry, circuitBreakerRegistry)
54+
module(Resilience4jModule) {
55+
it.circuitBreaker('test1') {
56+
it.failureRateThreshold(75).waitIntervalInMillis(5000)
57+
}.circuitBreaker('test2') {
58+
it.failureRateThreshold(25).waitIntervalInMillis(5000)
59+
}
60+
}
61+
}
62+
handlers {
63+
get {
64+
render 'ok'
65+
}
66+
}
67+
}
68+
client = testHttpClient(app)
69+
app.server.start() // override lazy start
70+
71+
and: "some circuit breaker events"
72+
['test1', 'test2'].each {
73+
def c = circuitBreakerRegistry.circuitBreaker(it)
74+
c.onSuccess(1000)
75+
c.onError(1000, new Exception("meh"))
76+
}
77+
78+
when: "we do a sanity check"
79+
def actual = client.get()
80+
81+
then: "it works"
82+
actual.statusCode == 200
83+
actual.body.text == 'ok'
84+
circuitBreakerRegistry.circuitBreaker('test1').metrics.numberOfBufferedCalls == 2
85+
circuitBreakerRegistry.circuitBreaker('test2').metrics.numberOfBufferedCalls == 2
86+
87+
when: "we get all circuit breaker states"
88+
actual = client.get('circuitbreaker/states')
89+
def dto = mapper.readValue(actual.body.text, CircuitBreakerStatesEndpointResponse)
90+
91+
then: "it works"
92+
dto.circuitBreakerStates.size() == 2
93+
dto.circuitBreakerStates.each {
94+
assert it.metrics.numberOfBufferedCalls == 2
95+
}
96+
97+
when: "we get state for just the test1 circuit"
98+
actual = client.get('circuitbreaker/states/test1')
99+
dto = mapper.readValue(actual.body.text, CircuitBreakerStatesEndpointResponse)
100+
101+
then: "we retrieved the test1 circuit"
102+
dto.circuitBreakerStates.size() == 1
103+
dto.circuitBreakerStates.get(0).metrics.numberOfBufferedCalls == 2
104+
}
105+
43106
def "test events"() {
44107
given: "an app"
45108
def circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults()

0 commit comments

Comments
 (0)