All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.resilience4j.metrics.ThreadPoolBulkheadMetrics Maven / Gradle / Ivy

Go to download

Resilience4j is a lightweight, easy-to-use fault tolerance library designed for Java8 and functional programming

There is a newer version: 2.2.0
Show newest version
/*
 * Copyright 2017 Jan Sykora
 *
 * 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.metrics;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.MetricSet;
import io.github.resilience4j.bulkhead.Bulkhead;
import io.github.resilience4j.bulkhead.BulkheadRegistry;
import io.github.resilience4j.bulkhead.ThreadPoolBulkhead;
import io.github.resilience4j.bulkhead.ThreadPoolBulkheadRegistry;
import io.vavr.collection.Array;

import java.util.Map;

import static com.codahale.metrics.MetricRegistry.name;
import static io.github.resilience4j.bulkhead.utils.MetricNames.AVAILABLE_QUEUE_CAPACITY;
import static io.github.resilience4j.bulkhead.utils.MetricNames.CURRENT_THREAD_POOL_SIZE;
import static io.github.resilience4j.bulkhead.utils.MetricNames.DEFAULT_PREFIX_THREAD_POOL;
import static java.util.Objects.requireNonNull;

/**
 * An adapter which exports {@link Bulkhead.Metrics} as Dropwizard Metrics Gauges.
 */
public class ThreadPoolBulkheadMetrics implements MetricSet {
    private final MetricRegistry metricRegistry = new MetricRegistry();

    private ThreadPoolBulkheadMetrics(Iterable bulkheads) {
        this(DEFAULT_PREFIX_THREAD_POOL, bulkheads);
    }

    private ThreadPoolBulkheadMetrics(String prefix, Iterable bulkheads) {
        requireNonNull(prefix);
        requireNonNull(bulkheads);
        bulkheads.forEach(bulkhead -> {
                    String name = bulkhead.getName();
            //number of available concurrent calls as an integer
                    metricRegistry.register(name(prefix, name, CURRENT_THREAD_POOL_SIZE),
                            (Gauge) () -> bulkhead.getMetrics().getThreadPoolSize());
                    metricRegistry.register(name(prefix, name, AVAILABLE_QUEUE_CAPACITY),
                            (Gauge) () -> bulkhead.getMetrics().getRemainingQueueCapacity());
                }
        );
    }

    /**
     * Creates a new instance BulkheadMetrics {@link ThreadPoolBulkheadMetrics} with specified metrics names prefix and
     * a {@link BulkheadRegistry} as a source.
     *
     * @param prefix           the prefix of metrics names
     * @param bulkheadRegistry the registry of bulkheads
     */
    public static ThreadPoolBulkheadMetrics ofBulkheadRegistry(String prefix, ThreadPoolBulkheadRegistry bulkheadRegistry) {
        return new ThreadPoolBulkheadMetrics(prefix, bulkheadRegistry.getAllBulkheads());
    }

    /**
     * Creates a new instance BulkheadMetrics {@link ThreadPoolBulkheadMetrics} with
     * a {@link BulkheadRegistry} as a source.
     *
     * @param bulkheadRegistry the registry of bulkheads
     */
    public static ThreadPoolBulkheadMetrics ofBulkheadRegistry(ThreadPoolBulkheadRegistry bulkheadRegistry) {
        return new ThreadPoolBulkheadMetrics(bulkheadRegistry.getAllBulkheads());
    }

    /**
     * Creates a new instance BulkheadMetrics {@link ThreadPoolBulkheadMetrics} with
     * an {@link Iterable} of bulkheads as a source.
     *
     * @param bulkheads the bulkheads
     */
    public static ThreadPoolBulkheadMetrics ofIterable(Iterable bulkheads) {
        return new ThreadPoolBulkheadMetrics(bulkheads);
    }

    /**
     * Creates a new instance BulkheadMetrics {@link ThreadPoolBulkheadMetrics} with
     * an {@link Iterable} of bulkheads as a source.
     *
     * @param bulkheads the bulkheads
     */
    public static ThreadPoolBulkheadMetrics ofIterable(String prefix, Iterable bulkheads) {
        return new ThreadPoolBulkheadMetrics(prefix, bulkheads);
    }


    /**
     * Creates a new instance of BulkheadMetrics {@link ThreadPoolBulkheadMetrics} with a bulkhead as a source.
     *
     * @param bulkhead the circuit breaker
     */
    public static ThreadPoolBulkheadMetrics ofBulkhead(ThreadPoolBulkhead bulkhead) {
        return new ThreadPoolBulkheadMetrics(Array.of(bulkhead));
    }

    @Override
    public Map getMetrics() {
        return metricRegistry.getMetrics();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy