forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue ReactiveX#71: Implementation of Prometheus exporters for Circui…
…tBreaker and RateLimiter (ReactiveX#81)
- Loading branch information
Showing
11 changed files
with
710 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
= resilience4j-prometheus | ||
|
||
Direct integration of circuit breaker and rate limiter metrics with | ||
https://github.com/prometheus/client_java[Prometheus simple client] | ||
|
||
For the circuit breaker library exports 2 metrics: | ||
|
||
1. By state with default metric name `circuit_breaker_states` and label `state`: | ||
- `closed` | ||
- `open` | ||
- `half_open` | ||
2. By call result with default metric name `circuit_breaker_calls` and label `call_result`: | ||
- `successful` | ||
- `failed` | ||
- `not_permitted` | ||
- `buffered` | ||
- `buffered_max` | ||
For the rate limiter following metric with default name `rate_limiter` and label `param` exported: | ||
|
||
- `available_permissions` | ||
- `waiting_threads` | ||
The names of the rate limiters and circuit breakers are exposed using label `name`. | ||
|
||
== Usage | ||
|
||
=== CircuitBreaker | ||
|
||
[source,java] | ||
-- | ||
final CircuitBreakerRegistry circuitBreakerRegistry = new InMemoryCircuitBreakerRegistry(); | ||
|
||
final CircuitBreaker foo = circuitBreakerRegistry.circuitBreaker("foo"); | ||
final CircuitBreaker boo = circuitBreakerRegistry.circuitBreaker("boo"); | ||
|
||
// Registering metrics in default prometeus CollectorRegistry | ||
new CircuitBreakerExports(circuitBreakerRegistry).register(); | ||
-- | ||
|
||
=== RateLimiter | ||
|
||
[source,java] | ||
-- | ||
final RateLimiterRegistry rateLimiterRegistry = new InMemoryRateLimiterRegistry(); | ||
|
||
final RateLimiter foo = rateLimiterRegistry.rateLimiter("foo"); | ||
final RateLimiter boo = rateLimiterRegistry.rateLimiter("boo"); | ||
|
||
// Registering metrics in default prometeus CollectorRegistry | ||
new RateLimiterExports(rateLimiterRegistry).register(); | ||
-- | ||
|
||
For both it is possible to use just a collection of breakers and limiters instead of registry. | ||
|
||
== License | ||
|
||
Copyright 2017 Oleksandr Goldobin | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the | ||
License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
dependencies { | ||
compile (libraries.prometheus_simpleclient) | ||
compile project(':resilience4j-circuitbreaker') | ||
compile project(':resilience4j-ratelimiter') | ||
} |
170 changes: 170 additions & 0 deletions
170
...e4j-prometheus/src/main/java/io/github/resilience4j/prometheus/CircuitBreakerExports.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* | ||
* Copyright 2017 Oleksandr Goldobin | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* | ||
*/ | ||
package io.github.resilience4j.prometheus; | ||
|
||
import io.github.resilience4j.circuitbreaker.CircuitBreaker; | ||
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; | ||
import io.prometheus.client.Collector; | ||
import io.prometheus.client.GaugeMetricFamily; | ||
import javaslang.Tuple; | ||
import javaslang.Tuple2; | ||
import javaslang.collection.Array; | ||
|
||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import static java.util.Arrays.asList; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* An adapter from builtin {@link CircuitBreaker.Metrics} to prometheus | ||
* {@link io.prometheus.client.CollectorRegistry}. | ||
* | ||
* Also exports {@link CircuitBreaker} state as a labeled metric | ||
*/ | ||
public class CircuitBreakerExports extends Collector { | ||
|
||
private static final String DEFAULT_NAME = "circuit_breaker"; | ||
private static Array<Tuple2<CircuitBreaker.State, String>> STATE_NAME_MAP = | ||
Array.ofAll(asList(CircuitBreaker.State.values())) | ||
.map(state -> Tuple.of(state, state.name().toLowerCase())); | ||
|
||
private final String prefix; | ||
private final Supplier<Iterable<CircuitBreaker>> circuitBreakersSupplier; | ||
|
||
/** | ||
* Creates a new instance of {@link CircuitBreakerExports} with default metrics names prefix and | ||
* {@link CircuitBreakerRegistry} as a source of circuit breakers. | ||
* @param circuitBreakerRegistry the registry of circuit breakers | ||
*/ | ||
public CircuitBreakerExports(CircuitBreakerRegistry circuitBreakerRegistry) { | ||
this(circuitBreakerRegistry::getAllCircuitBreakers); | ||
} | ||
|
||
/** | ||
* Creates a new instance of {@link CircuitBreakerExports} with default metrics names prefix and | ||
* {@link Iterable} of circuit breakers. | ||
* | ||
* @param circuitBreakers the circuit breakers | ||
*/ | ||
public CircuitBreakerExports(Iterable<CircuitBreaker> circuitBreakers) { | ||
this(() -> circuitBreakers); | ||
} | ||
|
||
|
||
/** | ||
* Creates a new instance of {@link CircuitBreakerExports} with default metrics names prefix and | ||
* {@link Supplier} of circuit breakers | ||
* | ||
* @param circuitBreakersSupplier the supplier of circuit breakers | ||
*/ | ||
public CircuitBreakerExports(Supplier<Iterable<CircuitBreaker>> circuitBreakersSupplier) { | ||
this(DEFAULT_NAME, circuitBreakersSupplier); | ||
} | ||
|
||
/** | ||
* Creates a new instance of {@link CircuitBreakerExports} with specified metrics names prefix and | ||
* {@link CircuitBreakerRegistry} as a source of circuit breakers. | ||
* | ||
* @param prefix the prefix of metrics names | ||
* @param circuitBreakerRegistry the registry of circuit breakers | ||
*/ | ||
public CircuitBreakerExports(String prefix, CircuitBreakerRegistry circuitBreakerRegistry) { | ||
this(prefix, circuitBreakerRegistry::getAllCircuitBreakers); | ||
} | ||
|
||
/** | ||
* Creates a new instance of {@link CircuitBreakerExports} with specified metrics names prefix and | ||
* {@link Iterable} of circuit breakers. | ||
* | ||
* @param prefix the prefix of metrics names | ||
* @param circuitBreakers the circuit breakers | ||
*/ | ||
public CircuitBreakerExports(String prefix, Iterable<CircuitBreaker> circuitBreakers) { | ||
this(prefix, () -> circuitBreakers); | ||
} | ||
|
||
/** | ||
* Creates a new instance of {@link CircuitBreakerExports} with specified metrics names prefix and | ||
* {@link Supplier} of circuit breakers | ||
* | ||
* @param prefix the prefix of metrics names | ||
* @param circuitBreakersSupplier the supplier of circuit breakers | ||
*/ | ||
public CircuitBreakerExports(String prefix, Supplier<Iterable<CircuitBreaker>> circuitBreakersSupplier) { | ||
requireNonNull(prefix); | ||
requireNonNull(circuitBreakersSupplier); | ||
|
||
this.prefix = prefix; | ||
this.circuitBreakersSupplier = circuitBreakersSupplier; | ||
} | ||
|
||
/** | ||
* {@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); | ||
} | ||
} |
Oops, something went wrong.