/* * Copyright 2016 LINE Corporation * * LINE Corporation licenses this file to you 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 com.linecorp.armeria.client.circuitbreaker.metrics; import static java.util.Objects.requireNonNull; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import com.codahale.metrics.MetricRegistry; import com.linecorp.armeria.client.circuitbreaker.CircuitBreaker; import com.linecorp.armeria.client.circuitbreaker.CircuitBreakerListener; import com.linecorp.armeria.client.circuitbreaker.CircuitState; import com.linecorp.armeria.client.circuitbreaker.EventCount; /** * A {@link CircuitBreakerListener} which monitors the status of {@link CircuitBreaker}s using * <a href="https://dropwizard.github.io/metrics/">dropwizard metrics</a>. * * <p>This class will generate the following metrics. * * <table summary="metrics that will generated by this class"> * <tr><th>metric name</th><th>description</th></tr> * * <tr><td>{@code {prefix}.{circuit-breaker-name}.transitions.closed}</td> * <td>The number of times that the circuit state has changed to {@code CLOSED}.</td></tr> * * <tr><td>{@code {prefix}.{circuit-breaker-name}.transitions.open}</td> * <td>The number of times that the circuit state has changed to {@code OPEN}.</td></tr> * * <tr><td>{@code {prefix}.{circuit-breaker-name}.transitions.halfopen}</td> * <td>The number of times that the circuit state has changed to {@code HALF_OPEN}.</td></tr> * * <tr><td>{@code {prefix}.{circuit-breaker-name}.requests.rejected}</td> * <td>The number of requests that is rejected by the circuit breaker.</td></tr> * * <tr><td>{@code {prefix}.{circuit-breaker-name}.counter.rate.success}</td> * <td>The rate of success requests in the counter time window.</td></tr> * * <tr><td>{@code {prefix}.{circuit-breaker-name}.counter.rate.failure}</td> * <td>The rate of failed requests in the counter time window.</td></tr> * * <tr><td>{@code {prefix}.{circuit-breaker-name}.counter.count.success}</td> * <td>The count of success requests in the counter time window.</td></tr> * * <tr><td>{@code {prefix}.{circuit-breaker-name}.counter.count.failure}</td> * <td>The count of failed requests in the counter time window.</td></tr> * * </table> */ public final class DropwizardMetricsCircuitBreakerListener implements CircuitBreakerListener { private final Map<String, DropwizardCircuitBreakerMetrics> metricsMap = new ConcurrentHashMap<>(); private final String prefix; private final MetricRegistry registry; /** * Creates a new instance. */ public DropwizardMetricsCircuitBreakerListener(MetricRegistry registry, String prefix) { this.registry = requireNonNull(registry, "registry"); this.prefix = requireNonNull(prefix, "prefix"); } @Override public void onStateChanged(CircuitBreaker circuitBreaker, CircuitState state) { metricsOf(circuitBreaker).onStateChanged(state); } @Override public void onEventCountUpdated(CircuitBreaker circuitBreaker, EventCount eventCount) { metricsOf(circuitBreaker).onCountUpdated(eventCount); } @Override public void onRequestRejected(CircuitBreaker circuitBreaker) { metricsOf(circuitBreaker).onRequestRejected(); } private DropwizardCircuitBreakerMetrics metricsOf(CircuitBreaker circuitBreaker) { return metricsMap.computeIfAbsent(circuitBreaker.name(), name -> new DropwizardCircuitBreakerMetrics(prefix, name, registry)); } }