Skip to content

Instantly share code, notes, and snippets.

@sha1n
Created February 5, 2024 08:46
Show Gist options
  • Save sha1n/b4287bfa806c59b8c596a4d0e1900890 to your computer and use it in GitHub Desktop.
Save sha1n/b4287bfa806c59b8c596a4d0e1900890 to your computer and use it in GitHub Desktop.
Micrometer executor metrics
package io.sha1n.infra.monitoring;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import lombok.val;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import static io.micrometer.core.instrument.Metrics.gauge;
public class ExecutorsMetrics {
public static void bind(ForkJoinPool pool, String name) {
val tags = Tags.of(Tag.of("name", name));
gauge("forkjoinpool.parallelism", tags, pool, ForkJoinPool::getParallelism);
gauge("forkjoinpool.queued.tasks", tags, pool, ForkJoinPool::getQueuedTaskCount);
gauge("forkjoinpool.queued.submission.tasks", tags, pool, ForkJoinPool::getQueuedSubmissionCount);
gauge("forkjoinpool.active.threads", tags, pool, ForkJoinPool::getActiveThreadCount);
gauge("forkjoinpool.running.threads", tags, pool, ForkJoinPool::getRunningThreadCount);
gauge("forkjoinpool.pool.size.threads", tags, pool, ForkJoinPool::getPoolSize);
gauge("forkjoinpool.steal.total", tags, pool, ForkJoinPool::getStealCount);
}
public static void bind(ThreadPoolExecutor pool, String name) {
bind(pool, name, "threadpoolexecutor");
}
public static void bind(ScheduledThreadPoolExecutor pool, String name) {
bind(pool, name, "scheduledthreadpoolexecutor");
}
private static void bind(ThreadPoolExecutor pool, String name, String prefix) {
val tags = Tags.of(Tag.of("name", name));
gauge(prefix + ".queued.tasks", tags, pool, p -> p.getQueue().size());
gauge(prefix + ".queue.remaining.tasks", tags, pool, p -> p.getQueue().remainingCapacity());
gauge(prefix + ".active.threads", tags, pool, ThreadPoolExecutor::getActiveCount);
gauge(prefix + ".completed.tasks.total", tags, pool, ThreadPoolExecutor::getCompletedTaskCount);
gauge(prefix + ".pool.size.threads", tags, pool, ThreadPoolExecutor::getPoolSize);
gauge(prefix + ".pool.core.threads", tags, pool, ThreadPoolExecutor::getCorePoolSize);
gauge(prefix + ".pool.max.threads", tags, pool, ThreadPoolExecutor::getMaximumPoolSize);
gauge(prefix + ".largest.pool.size", tags, pool, ThreadPoolExecutor::getLargestPoolSize);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment