Skip to content

Commit

Permalink
Issue ReactiveX#354: Use tags instead of gauge values to indicate cir…
Browse files Browse the repository at this point in the history
…cuit breaker state (ReactiveX#416)
  • Loading branch information
leonard84 authored and RobWin committed May 5, 2019
1 parent 16ba681 commit a6da58b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,15 @@ public static CircuitBreakerMetrics ofIterable(String prefix, Iterable<CircuitBr

@Override
public void bindTo(MeterRegistry registry) {
final CircuitBreaker.State[] states = CircuitBreaker.State.values();
for (CircuitBreaker circuitBreaker : circuitBreakers) {
final String name = circuitBreaker.getName();
Gauge.builder(getName(prefix, name, STATE), circuitBreaker, (cb) -> cb.getState().getOrder())
.register(registry);
for (CircuitBreaker.State state : states) {
Gauge.builder(getName(prefix, name, STATE), circuitBreaker, (cb) -> cb.getState() == state ? 1 : 0)
.tag("state", state.name().toLowerCase())
.register(registry);
}

Gauge.builder(getName(prefix, name, BUFFERED_MAX), circuitBreaker, (cb) -> cb.getMetrics().getMaxNumberOfBufferedCalls())
.register(registry);
Gauge.builder(getName(prefix, name, BUFFERED), circuitBreaker, (cb) -> cb.getMetrics().getNumberOfBufferedCalls())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private CircuitBreakerMetricsCollector(MetricNames names, Supplier<? extends Ite
public List<MetricFamilySamples> collect() {
GaugeMetricFamily stateFamily = new GaugeMetricFamily(
names.getStateMetricName(),
"The state of the circuit breaker: 0 - CLOSED, 1 - OPEN, 2 - HALF_OPEN",
LabelNames.NAME
"The state of the circuit breaker:",
LabelNames.NAME_AND_STATE
);
GaugeMetricFamily callsFamily = new GaugeMetricFamily(
names.getCallsMetricName(),
Expand All @@ -109,17 +109,22 @@ public List<MetricFamilySamples> collect() {
"The maximum number of buffered calls",
LabelNames.NAME
);

GaugeMetricFamily failureRateFamily = new GaugeMetricFamily(
GaugeMetricFamily failureRateFamily = new GaugeMetricFamily(
names.getFailureRateMetricName(),
"The failure rate",
LabelNames.NAME
);

final CircuitBreaker.State[] states = CircuitBreaker.State.values();

for (CircuitBreaker circuitBreaker : supplier.get()) {
List<String> nameLabel = singletonList(circuitBreaker.getName());

stateFamily.addMetric(nameLabel, circuitBreaker.getState().getOrder());
for (CircuitBreaker.State state : states) {
stateFamily.addMetric(asList(circuitBreaker.getName(), state.name().toLowerCase()),
circuitBreaker.getState() == state ? 1 : 0);
}

Metrics metrics = circuitBreaker.getMetrics();
callsFamily.addMetric(asList(circuitBreaker.getName(), "successful"), metrics.getNumberOfSuccessfulCalls());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ private LabelNames() {}

public static final List<String> NAME = Collections.singletonList("name");
public static final List<String> NAME_AND_KIND = Arrays.asList("name", "kind");
public static final List<String> NAME_AND_STATE = Arrays.asList("name", "state");
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
*/
package io.github.resilience4j.prometheus.collectors;

import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.prometheus.client.CollectorRegistry;
import org.junit.Before;
import org.junit.Test;


import static io.github.resilience4j.prometheus.collectors.CircuitBreakerMetricsCollector.MetricNames.DEFAULT_CIRCUIT_BREAKER_BUFFERED_CALLS;
import static io.github.resilience4j.prometheus.collectors.CircuitBreakerMetricsCollector.MetricNames.DEFAULT_CIRCUIT_BREAKER_CALLS_METRIC_NAME;
import static io.github.resilience4j.prometheus.collectors.CircuitBreakerMetricsCollector.MetricNames.DEFAULT_CIRCUIT_BREAKER_FAILURE_RATE;
Expand All @@ -29,6 +23,12 @@
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Before;
import org.junit.Test;

import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.prometheus.client.CollectorRegistry;

public class CircuitBreakerMetricsCollectorTest {

CollectorRegistry registry;
Expand All @@ -50,8 +50,8 @@ public void setup() {
public void stateReportsCorrespondingValue() {
double state = registry.getSampleValue(
DEFAULT_CIRCUIT_BREAKER_STATE_METRIC_NAME,
new String[]{"name"},
new String[]{circuitBreaker.getName()}
new String[]{"name", "state"},
new String[]{circuitBreaker.getName(), circuitBreaker.getState().name().toLowerCase()}
);

assertThat(state).isEqualTo(circuitBreaker.getState().getOrder());
Expand Down Expand Up @@ -155,8 +155,8 @@ public void customMetricNamesOverrideDefaultOnes() {
)).isNotNull();
assertThat(registry.getSampleValue(
"custom_state",
new String[]{"name"},
new String[]{"backendA"}
new String[]{"name", "state"},
new String[]{"backendA", "closed"}
)).isNotNull();
assertThat(registry.getSampleValue(
"custom_max_buffered_calls",
Expand Down

0 comments on commit a6da58b

Please sign in to comment.