Skip to content

Commit

Permalink
Issue ReactiveX#71: Implementation of Prometheus exporters for Circui…
Browse files Browse the repository at this point in the history
…tBreaker and RateLimiter (ReactiveX#81)
  • Loading branch information
goldobin authored and RobWin committed Mar 31, 2017
1 parent e84a012 commit 56501b3
Show file tree
Hide file tree
Showing 11 changed files with 710 additions and 1 deletion.
6 changes: 5 additions & 1 deletion libraries.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ext {
vertxVersion = '3.4.1'
springBootVersion = '1.4.3.RELEASE'
retrofitVersion = '2.1.0'
prometheusSimpleClientVersion = '0.0.21'

libraries = [
// compile
Expand Down Expand Up @@ -52,6 +53,9 @@ ext {
metrics: "io.dropwizard.metrics:metrics-core:${metricsVersion}",

// CircuitBreaker documentation
metrics_healthcheck: "io.dropwizard.metrics:metrics-healthchecks:${metricsVersion}"
metrics_healthcheck: "io.dropwizard.metrics:metrics-healthchecks:${metricsVersion}",

// Prometheus addon
prometheus_simpleclient: "io.prometheus:simpleclient_common:${prometheusSimpleClientVersion}"
]
}
70 changes: 70 additions & 0 deletions resilience4j-prometheus/README.adoc
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.
5 changes: 5 additions & 0 deletions resilience4j-prometheus/build.gradle
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')
}
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);
}
}
Loading

0 comments on commit 56501b3

Please sign in to comment.