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

com.codahale.metrics.httpclient.InstrumentedClientConnManager Maven / Gradle / Ivy

package com.codahale.metrics.httpclient;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.impl.conn.SchemeRegistryFactory;
import org.apache.http.impl.conn.SystemDefaultDnsResolver;

import java.util.concurrent.TimeUnit;

import static com.codahale.metrics.MetricRegistry.name;

/**
 * A {@link ClientConnectionManager} which monitors the number of open connections.
 */
public class InstrumentedClientConnManager extends PoolingClientConnectionManager {
    public InstrumentedClientConnManager(MetricRegistry metricRegistry) {
        this(metricRegistry, SchemeRegistryFactory.createDefault());
    }

    public InstrumentedClientConnManager(MetricRegistry metricsRegistry,
                                         SchemeRegistry registry) {
        this(metricsRegistry, registry, -1, TimeUnit.MILLISECONDS);
    }

    public InstrumentedClientConnManager(MetricRegistry metricsRegistry,
                                         SchemeRegistry registry,
                                         long connTTL,
                                         TimeUnit connTTLTimeUnit) {
        this(metricsRegistry, registry, connTTL, connTTLTimeUnit, new SystemDefaultDnsResolver(), null);
    }

    public InstrumentedClientConnManager(MetricRegistry metricsRegistry,
                                         SchemeRegistry schemeRegistry,
                                         long connTTL,
                                         TimeUnit connTTLTimeUnit,
                                         DnsResolver dnsResolver,
                                         String name) {
        super(schemeRegistry, connTTL, connTTLTimeUnit, dnsResolver);
        metricsRegistry.register(name(ClientConnectionManager.class, name, "available-connections"),
                                 new Gauge() {
                                     @Override
                                     public Integer getValue() {
                                         // this acquires a lock on the connection pool; remove if contention sucks
                                         return getTotalStats().getAvailable();
                                     }
                                 });
        metricsRegistry.register(name(ClientConnectionManager.class, name, "leased-connections"),
                                 new Gauge() {
                                     @Override
                                     public Integer getValue() {
                                         // this acquires a lock on the connection pool; remove if contention sucks
                                         return getTotalStats().getLeased();
                                     }
                                 });
        metricsRegistry.register(name(ClientConnectionManager.class, name, "max-connections"),
                                 new Gauge() {
                                     @Override
                                     public Integer getValue() {
                                         // this acquires a lock on the connection pool; remove if contention sucks
                                         return getTotalStats().getMax();
                                     }
                                 });
        metricsRegistry.register(name(ClientConnectionManager.class, name, "pending-connections"),
                                 new Gauge() {
                                     @Override
                                     public Integer getValue() {
                                         // this acquires a lock on the connection pool; remove if contention sucks
                                         return getTotalStats().getPending();
                                     }
                                 });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy