Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.codahale.metrics.InstrumentedScheduledExecutorService Maven / Gradle / Ivy
package com.codahale.metrics;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
/**
* An {@link ScheduledExecutorService} that monitors the number of tasks submitted, running,
* completed and also keeps a {@link Timer} for the task duration.
*
* It will register the metrics using the given (or auto-generated) name as classifier, e.g:
* "your-executor-service.submitted", "your-executor-service.running", etc.
*/
public class InstrumentedScheduledExecutorService implements ScheduledExecutorService {
private static final AtomicLong nameCounter = new AtomicLong();
private final ScheduledExecutorService delegate;
private final Meter submitted;
private final Counter running;
private final Meter completed;
private final Timer duration;
private final Meter scheduledOnce;
private final Meter scheduledRepetitively;
private final Counter scheduledOverrun;
private final Histogram percentOfPeriod;
/**
* Wraps an {@link ScheduledExecutorService} uses an auto-generated default name.
*
* @param delegate {@link ScheduledExecutorService} to wrap.
* @param registry {@link MetricRegistry} that will contain the metrics.
*/
public InstrumentedScheduledExecutorService(ScheduledExecutorService delegate, MetricRegistry registry) {
this(delegate, registry, "instrumented-scheduled-executor-service-" + nameCounter.incrementAndGet());
}
/**
* Wraps an {@link ScheduledExecutorService} with an explicit name.
*
* @param delegate {@link ScheduledExecutorService} to wrap.
* @param registry {@link MetricRegistry} that will contain the metrics.
* @param name name for this executor service.
*/
public InstrumentedScheduledExecutorService(ScheduledExecutorService delegate, MetricRegistry registry, String name) {
this.delegate = delegate;
this.submitted = registry.meter(MetricRegistry.name(name, "submitted"));
this.running = registry.counter(MetricRegistry.name(name, "running"));
this.completed = registry.meter(MetricRegistry.name(name, "completed"));
this.duration = registry.timer(MetricRegistry.name(name, "duration"));
this.scheduledOnce = registry.meter(MetricRegistry.name(name, "scheduled.once"));
this.scheduledRepetitively = registry.meter(MetricRegistry.name(name, "scheduled.repetitively"));
this.scheduledOverrun = registry.counter(MetricRegistry.name(name, "scheduled.overrun"));
this.percentOfPeriod = registry.histogram(MetricRegistry.name(name, "scheduled.percent-of-period"));
}
/**
* {@inheritDoc}
*/
@Override
public ScheduledFuture> schedule(Runnable command, long delay, TimeUnit unit) {
scheduledOnce.mark();
return delegate.schedule(new InstrumentedRunnable(command), delay, unit);
}
/**
* {@inheritDoc}
*/
@Override
public ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit) {
scheduledOnce.mark();
return delegate.schedule(new InstrumentedCallable(callable), delay, unit);
}
/**
* {@inheritDoc}
*/
@Override
public ScheduledFuture> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
scheduledRepetitively.mark();
return delegate.scheduleAtFixedRate(new InstrumentedPeriodicRunnable(command, period, unit), initialDelay, period, unit);
}
/**
* {@inheritDoc}
*/
@Override
public ScheduledFuture> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
scheduledRepetitively.mark();
return delegate.scheduleAtFixedRate(new InstrumentedRunnable(command), initialDelay, delay, unit);
}
/**
* {@inheritDoc}
*/
@Override
public void shutdown() {
delegate.shutdown();
}
/**
* {@inheritDoc}
*/
@Override
public List shutdownNow() {
return delegate.shutdownNow();
}
/**
* {@inheritDoc}
*/
@Override
public boolean isShutdown() {
return delegate.isShutdown();
}
/**
* {@inheritDoc}
*/
@Override
public boolean isTerminated() {
return delegate.isTerminated();
}
/**
* {@inheritDoc}
*/
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return delegate.awaitTermination(timeout, unit);
}
/**
* {@inheritDoc}
*/
@Override
public Future submit(Callable task) {
submitted.mark();
return delegate.submit(new InstrumentedCallable(task));
}
/**
* {@inheritDoc}
*/
@Override
public Future submit(Runnable task, T result) {
submitted.mark();
return delegate.submit(new InstrumentedRunnable(task), result);
}
/**
* {@inheritDoc}
*/
@Override
public Future> submit(Runnable task) {
submitted.mark();
return delegate.submit(new InstrumentedRunnable(task));
}
/**
* {@inheritDoc}
*/
@Override
public List> invokeAll(Collection extends Callable> tasks) throws InterruptedException {
submitted.mark(tasks.size());
Collection extends Callable> instrumented = instrument(tasks);
return delegate.invokeAll(instrumented);
}
/**
* {@inheritDoc}
*/
@Override
public List> invokeAll(Collection extends Callable> tasks, long timeout, TimeUnit unit) throws InterruptedException {
submitted.mark(tasks.size());
Collection extends Callable> instrumented = instrument(tasks);
return delegate.invokeAll(instrumented, timeout, unit);
}
/**
* {@inheritDoc}
*/
@Override
public T invokeAny(Collection extends Callable> tasks) throws InterruptedException, ExecutionException {
submitted.mark(tasks.size());
Collection extends Callable> instrumented = instrument(tasks);
return delegate.invokeAny(instrumented);
}
/**
* {@inheritDoc}
*/
@Override
public T invokeAny(Collection extends Callable> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
submitted.mark(tasks.size());
Collection extends Callable> instrumented = instrument(tasks);
return delegate.invokeAny(instrumented, timeout, unit);
}
private Collection extends Callable> instrument(Collection extends Callable> tasks) {
final List> instrumented = new ArrayList>(tasks.size());
for (Callable task : tasks) {
instrumented.add(new InstrumentedCallable(task));
}
return instrumented;
}
/**
* {@inheritDoc}
*/
@Override
public void execute(Runnable command) {
submitted.mark();
delegate.execute(new InstrumentedRunnable(command));
}
private class InstrumentedRunnable implements Runnable {
private final Runnable command;
InstrumentedRunnable(Runnable command) {
this.command = command;
}
@Override
public void run() {
running.inc();
final Timer.Context context = duration.time();
try {
command.run();
} finally {
context.stop();
running.dec();
completed.mark();
}
}
}
private class InstrumentedPeriodicRunnable implements Runnable {
private final Runnable command;
private final long periodInNanos;
InstrumentedPeriodicRunnable(Runnable command, long period, TimeUnit unit) {
this.command = command;
this.periodInNanos = unit.toNanos(period);
}
@Override
public void run() {
running.inc();
final Timer.Context context = duration.time();
try {
command.run();
} finally {
final long elapsed = context.stop();
running.dec();
completed.mark();
if (elapsed > periodInNanos) {
scheduledOverrun.inc();
}
percentOfPeriod.update((100L * elapsed) / periodInNanos);
}
}
}
private class InstrumentedCallable implements Callable {
private final Callable task;
InstrumentedCallable(Callable task) {
this.task = task;
}
@Override
public T call() throws Exception {
running.inc();
final Timer.Context context = duration.time();
try {
return task.call();
} finally {
context.stop();
running.dec();
completed.mark();
}
}
}
}