Skip to content

Commit

Permalink
Add a more type safe way of registering gauges
Browse files Browse the repository at this point in the history
  • Loading branch information
arteam committed Jun 2, 2022
1 parent 3c89a98 commit dc5b6ac
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 165 deletions.
Expand Up @@ -58,31 +58,31 @@ public InstrumentedExecutorService(ExecutorService delegate, MetricRegistry regi

if (delegate instanceof ThreadPoolExecutor) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) delegate;
registry.register(MetricRegistry.name(name, "pool.size"),
(Gauge<Integer>) executor::getPoolSize);
registry.register(MetricRegistry.name(name, "pool.core"),
(Gauge<Integer>) executor::getCorePoolSize);
registry.register(MetricRegistry.name(name, "pool.max"),
(Gauge<Integer>) executor::getMaximumPoolSize);
registry.registerGauge(MetricRegistry.name(name, "pool.size"),
executor::getPoolSize);
registry.registerGauge(MetricRegistry.name(name, "pool.core"),
executor::getCorePoolSize);
registry.registerGauge(MetricRegistry.name(name, "pool.max"),
executor::getMaximumPoolSize);
final BlockingQueue<Runnable> queue = executor.getQueue();
registry.register(MetricRegistry.name(name, "tasks.active"),
(Gauge<Integer>) executor::getActiveCount);
registry.register(MetricRegistry.name(name, "tasks.completed"),
(Gauge<Long>) executor::getCompletedTaskCount);
registry.register(MetricRegistry.name(name, "tasks.queued"),
(Gauge<Integer>) queue::size);
registry.register(MetricRegistry.name(name, "tasks.capacity"),
(Gauge<Integer>) queue::remainingCapacity);
registry.registerGauge(MetricRegistry.name(name, "tasks.active"),
executor::getActiveCount);
registry.registerGauge(MetricRegistry.name(name, "tasks.completed"),
executor::getCompletedTaskCount);
registry.registerGauge(MetricRegistry.name(name, "tasks.queued"),
queue::size);
registry.registerGauge(MetricRegistry.name(name, "tasks.capacity"),
queue::remainingCapacity);
} else if (delegate instanceof ForkJoinPool) {
ForkJoinPool forkJoinPool = (ForkJoinPool) delegate;
registry.register(MetricRegistry.name(name, "tasks.stolen"),
(Gauge<Long>) forkJoinPool::getStealCount);
registry.register(MetricRegistry.name(name, "tasks.queued"),
(Gauge<Long>) forkJoinPool::getQueuedTaskCount);
registry.register(MetricRegistry.name(name, "threads.active"),
(Gauge<Integer>) forkJoinPool::getActiveThreadCount);
registry.register(MetricRegistry.name(name, "threads.running"),
(Gauge<Integer>) forkJoinPool::getRunningThreadCount);
registry.registerGauge(MetricRegistry.name(name, "tasks.stolen"),
forkJoinPool::getStealCount);
registry.registerGauge(MetricRegistry.name(name, "tasks.queued"),
forkJoinPool::getQueuedTaskCount);
registry.registerGauge(MetricRegistry.name(name, "threads.active"),
forkJoinPool::getActiveThreadCount);
registry.registerGauge(MetricRegistry.name(name, "threads.running"),
forkJoinPool::getRunningThreadCount);
}
}

Expand Down
Expand Up @@ -76,6 +76,10 @@ protected ConcurrentMap<String, Metric> buildMap() {
return new ConcurrentHashMap<>();
}

public <T> Gauge<T> registerGauge(String name, Gauge<T> metric) throws IllegalArgumentException {
return register(name, metric);
}

/**
* Given a {@link Metric}, registers it under the given name.
*
Expand All @@ -85,7 +89,6 @@ protected ConcurrentMap<String, Metric> buildMap() {
* @return {@code metric}
* @throws IllegalArgumentException if the name is already registered or metric variable is null
*/
@SuppressWarnings("unchecked")
public <T extends Metric> T register(String name, T metric) throws IllegalArgumentException {

if (metric == null) {
Expand Down
@@ -1,6 +1,5 @@
package com.codahale.metrics.ehcache;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import net.sf.ehcache.CacheException;
Expand Down Expand Up @@ -118,56 +117,56 @@ public class InstrumentedEhcache extends EhcacheDecoratorAdapter {
public static Ehcache instrument(MetricRegistry registry, final Ehcache cache) {

final String prefix = name(cache.getClass(), cache.getName());
registry.register(name(prefix, "hits"),
(Gauge<Long>) () -> cache.getStatistics().cacheHitCount());
registry.registerGauge(name(prefix, "hits"),
() -> cache.getStatistics().cacheHitCount());

registry.register(name(prefix, "in-memory-hits"),
(Gauge<Long>) () -> cache.getStatistics().localHeapHitCount());
registry.registerGauge(name(prefix, "in-memory-hits"),
() -> cache.getStatistics().localHeapHitCount());

registry.register(name(prefix, "off-heap-hits"),
(Gauge<Long>) () -> cache.getStatistics().localOffHeapHitCount());
registry.registerGauge(name(prefix, "off-heap-hits"),
() -> cache.getStatistics().localOffHeapHitCount());

registry.register(name(prefix, "on-disk-hits"),
(Gauge<Long>) () -> cache.getStatistics().localDiskHitCount());
registry.registerGauge(name(prefix, "on-disk-hits"),
() -> cache.getStatistics().localDiskHitCount());

registry.register(name(prefix, "misses"),
(Gauge<Long>) () -> cache.getStatistics().cacheMissCount());
registry.registerGauge(name(prefix, "misses"),
() -> cache.getStatistics().cacheMissCount());

registry.register(name(prefix, "in-memory-misses"),
(Gauge<Long>) () -> cache.getStatistics().localHeapMissCount());
registry.registerGauge(name(prefix, "in-memory-misses"),
() -> cache.getStatistics().localHeapMissCount());

registry.register(name(prefix, "off-heap-misses"),
(Gauge<Long>) () -> cache.getStatistics().localOffHeapMissCount());
registry.registerGauge(name(prefix, "off-heap-misses"),
() -> cache.getStatistics().localOffHeapMissCount());

registry.register(name(prefix, "on-disk-misses"),
(Gauge<Long>) () -> cache.getStatistics().localDiskMissCount());
registry.registerGauge(name(prefix, "on-disk-misses"),
() -> cache.getStatistics().localDiskMissCount());

registry.register(name(prefix, "objects"),
(Gauge<Long>) () -> cache.getStatistics().getSize());
registry.registerGauge(name(prefix, "objects"),
() -> cache.getStatistics().getSize());

registry.register(name(prefix, "in-memory-objects"),
(Gauge<Long>) () -> cache.getStatistics().getLocalHeapSize());
registry.registerGauge(name(prefix, "in-memory-objects"),
() -> cache.getStatistics().getLocalHeapSize());

registry.register(name(prefix, "off-heap-objects"),
(Gauge<Long>) () -> cache.getStatistics().getLocalOffHeapSize());
registry.registerGauge(name(prefix, "off-heap-objects"),
() -> cache.getStatistics().getLocalOffHeapSize());

registry.register(name(prefix, "on-disk-objects"),
(Gauge<Long>) () -> cache.getStatistics().getLocalDiskSize());
registry.registerGauge(name(prefix, "on-disk-objects"),
() -> cache.getStatistics().getLocalDiskSize());

registry.register(name(prefix, "mean-get-time"),
(Gauge<Double>) () -> cache.getStatistics().cacheGetOperation().latency().average().value());
registry.registerGauge(name(prefix, "mean-get-time"),
() -> cache.getStatistics().cacheGetOperation().latency().average().value());

registry.register(name(prefix, "mean-search-time"),
(Gauge<Double>) () -> cache.getStatistics().cacheSearchOperation().latency().average().value());
registry.registerGauge(name(prefix, "mean-search-time"),
() -> cache.getStatistics().cacheSearchOperation().latency().average().value());

registry.register(name(prefix, "eviction-count"),
(Gauge<Long>) () -> cache.getStatistics().cacheEvictionOperation().count().value());
registry.registerGauge(name(prefix, "eviction-count"),
() -> cache.getStatistics().cacheEvictionOperation().count().value());

registry.register(name(prefix, "searches-per-second"),
(Gauge<Double>) () -> cache.getStatistics().cacheSearchOperation().rate().value());
registry.registerGauge(name(prefix, "searches-per-second"),
() -> cache.getStatistics().cacheSearchOperation().rate().value());

registry.register(name(prefix, "writer-queue-size"),
(Gauge<Long>) () -> cache.getStatistics().getWriterQueueLength());
registry.registerGauge(name(prefix, "writer-queue-size"),
() -> cache.getStatistics().getWriterQueueLength());

return new InstrumentedEhcache(registry, cache);
}
Expand Down
@@ -1,6 +1,5 @@
package com.codahale.metrics.httpasyncclient;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import org.apache.http.config.Registry;
import org.apache.http.conn.DnsResolver;
Expand All @@ -20,26 +19,18 @@ public class InstrumentedNClientConnManager extends PoolingNHttpClientConnection

public InstrumentedNClientConnManager(final ConnectingIOReactor ioreactor, final NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, final SchemePortResolver schemePortResolver, final MetricRegistry metricRegistry, final Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry, final long timeToLive, final TimeUnit tunit, final DnsResolver dnsResolver, final String name) {
super(ioreactor, connFactory, iosessionFactoryRegistry, schemePortResolver, dnsResolver, timeToLive, tunit);
metricRegistry.register(name(NHttpClientConnectionManager.class, name, "available-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getAvailable();
});
metricRegistry.register(name(NHttpClientConnectionManager.class, name, "leased-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getLeased();
});
metricRegistry.register(name(NHttpClientConnectionManager.class, name, "max-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getMax();
});
metricRegistry.register(name(NHttpClientConnectionManager.class, name, "pending-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getPending();
});
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(NHttpClientConnectionManager.class, name, "available-connections"),
() -> getTotalStats().getAvailable());
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(NHttpClientConnectionManager.class, name, "leased-connections"),
() -> getTotalStats().getLeased());
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(NHttpClientConnectionManager.class, name, "max-connections"),
() -> getTotalStats().getMax());
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(NHttpClientConnectionManager.class, name, "pending-connections"),
() -> getTotalStats().getPending());
}

}
@@ -1,6 +1,5 @@
package com.codahale.metrics.httpclient;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
Expand Down Expand Up @@ -104,26 +103,18 @@ public InstrumentedHttpClientConnectionManager(MetricRegistry metricsRegistry,
this.metricsRegistry = metricsRegistry;
this.name = name;

metricsRegistry.register(name(HttpClientConnectionManager.class, name, "available-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getAvailable();
});
metricsRegistry.register(name(HttpClientConnectionManager.class, name, "leased-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getLeased();
});
metricsRegistry.register(name(HttpClientConnectionManager.class, name, "max-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getMax();
});
metricsRegistry.register(name(HttpClientConnectionManager.class, name, "pending-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getPending();
});
// this acquires a lock on the connection pool; remove if contention sucks
metricsRegistry.registerGauge(name(HttpClientConnectionManager.class, name, "available-connections"),
() -> getTotalStats().getAvailable());
// this acquires a lock on the connection pool; remove if contention sucks
metricsRegistry.registerGauge(name(HttpClientConnectionManager.class, name, "leased-connections"),
() -> getTotalStats().getLeased());
// this acquires a lock on the connection pool; remove if contention sucks
metricsRegistry.registerGauge(name(HttpClientConnectionManager.class, name, "max-connections"),
() -> getTotalStats().getMax());
// this acquires a lock on the connection pool; remove if contention sucks
metricsRegistry.registerGauge(name(HttpClientConnectionManager.class, name, "pending-connections"),
() -> getTotalStats().getPending());
}

@Override
Expand Down
@@ -1,6 +1,5 @@
package com.codahale.metrics.httpclient5;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import org.apache.hc.client5.http.DnsResolver;
import org.apache.hc.client5.http.SchemePortResolver;
Expand Down Expand Up @@ -49,26 +48,18 @@ protected static Registry<TlsStrategy> getDefaultTlsStrategy() {
this.metricsRegistry = requireNonNull(metricRegistry, "metricRegistry");
this.name = name;

metricRegistry.register(name(METRICS_PREFIX, name, "available-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getAvailable();
});
metricRegistry.register(name(METRICS_PREFIX, name, "leased-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getLeased();
});
metricRegistry.register(name(METRICS_PREFIX, name, "max-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getMax();
});
metricRegistry.register(name(METRICS_PREFIX, name, "pending-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getPending();
});
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(METRICS_PREFIX, name, "available-connections"),
() -> getTotalStats().getAvailable());
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(METRICS_PREFIX, name, "leased-connections"),
() -> getTotalStats().getLeased());
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(METRICS_PREFIX, name, "max-connections"),
() -> getTotalStats().getMax());
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(METRICS_PREFIX, name, "pending-connections"),
() -> getTotalStats().getPending());
}

/**
Expand Down
@@ -1,6 +1,5 @@
package com.codahale.metrics.httpclient5;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import org.apache.hc.client5.http.DnsResolver;
import org.apache.hc.client5.http.SchemePortResolver;
Expand Down Expand Up @@ -52,26 +51,21 @@ protected static Registry<ConnectionSocketFactory> getDefaultRegistry() {
this.metricsRegistry = requireNonNull(metricRegistry, "metricRegistry");
this.name = name;

metricRegistry.register(name(METRICS_PREFIX, name, "available-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(METRICS_PREFIX, name, "available-connections"),
() -> {
return getTotalStats().getAvailable();
});
metricRegistry.register(name(METRICS_PREFIX, name, "leased-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getLeased();
});
metricRegistry.register(name(METRICS_PREFIX, name, "max-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getMax();
});
metricRegistry.register(name(METRICS_PREFIX, name, "pending-connections"),
(Gauge<Integer>) () -> {
// this acquires a lock on the connection pool; remove if contention sucks
return getTotalStats().getPending();
});
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(METRICS_PREFIX, name, "leased-connections"),
() -> getTotalStats().getLeased());
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(METRICS_PREFIX, name, "max-connections"),
() -> getTotalStats().getMax()
);
// this acquires a lock on the connection pool; remove if contention sucks
metricRegistry.registerGauge(name(METRICS_PREFIX, name, "pending-connections"),
() -> getTotalStats().getPending());
}

/**
Expand Down

0 comments on commit dc5b6ac

Please sign in to comment.