Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose ForkJoinPool parallelism and pool size metrics #5236

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class ExecutorServiceMetrics implements MeterBinder {

private static final String DEFAULT_EXECUTOR_METRIC_PREFIX = "";

public static final String DESCRIPTION_POOL_SIZE = "The current number of threads in the pool";
jonatan-ivanov marked this conversation as resolved.
Show resolved Hide resolved

@Nullable
private final ExecutorService executorService;

Expand Down Expand Up @@ -362,7 +364,7 @@ private void monitor(MeterRegistry registry, @Nullable ThreadPoolExecutor tp) {

Gauge.builder(metricPrefix + "executor.pool.size", tp, ThreadPoolExecutor::getPoolSize)
.tags(tags)
.description("The current number of threads in the pool")
.description(DESCRIPTION_POOL_SIZE)
.baseUnit(BaseUnits.THREADS)
.register(registry);

Expand All @@ -385,22 +387,38 @@ private void monitor(MeterRegistry registry, ForkJoinPool fj) {
.description("Estimate of the total number of tasks stolen from "
+ "one thread's work queue by another. The reported value "
+ "underestimates the actual total number of steals when the pool " + "is not quiescent")
.baseUnit(BaseUnits.TASKS)
.register(registry);

Gauge.builder(metricPrefix + "executor.queued", fj, ForkJoinPool::getQueuedTaskCount)
.tags(tags)
.description("An estimate of the total number of tasks currently held in queues by worker threads")
.baseUnit(BaseUnits.TASKS)
.register(registry);

Gauge.builder(metricPrefix + "executor.active", fj, ForkJoinPool::getActiveThreadCount)
.tags(tags)
.description("An estimate of the number of threads that are currently stealing or executing tasks")
.baseUnit(BaseUnits.THREADS)
jonatan-ivanov marked this conversation as resolved.
Show resolved Hide resolved
.register(registry);

Gauge.builder(metricPrefix + "executor.running", fj, ForkJoinPool::getRunningThreadCount)
.tags(tags)
.description(
"An estimate of the number of worker threads that are not blocked waiting to join tasks or for other managed synchronization threads")
.baseUnit(BaseUnits.THREADS)
.register(registry);

Gauge.builder(metricPrefix + "executor.parallelism", fj, ForkJoinPool::getParallelism)
.tags(tags)
.description("The targeted parallelism level of this pool")
.baseUnit(BaseUnits.THREADS)
.register(registry);

Gauge.builder(metricPrefix + "executor.pool.size", fj, ForkJoinPool::getPoolSize)
.tags(tags)
.description(DESCRIPTION_POOL_SIZE)
.baseUnit(BaseUnits.THREADS)
.register(registry);
shakuzen marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ void monitorExecutorsExecutorServicePrivateClass() {
.doesNotThrowAnyException();
}

@DisplayName("ForkJoinPool is assigned with its own set of metrics")
@ParameterizedTest
@CsvSource({ "custom,custom.", "custom.,custom.", ",''", "' ',''" })
void forkJoinPool(String metricPrefix, String expectedMetricPrefix) {
var fjp = new ForkJoinPool(1);
monitorExecutorService("fjp", metricPrefix, fjp);
registry.get(expectedMetricPrefix + "executor.steals").tags(userTags).tag("name", "fjp").functionCounter();
registry.get(expectedMetricPrefix + "executor.queued").tags(userTags).tag("name", "fjp").gauge();
registry.get(expectedMetricPrefix + "executor.running").tags(userTags).tag("name", "fjp").gauge();
registry.get(expectedMetricPrefix + "executor.parallelism").tags(userTags).tag("name", "fjp").gauge();
registry.get(expectedMetricPrefix + "executor.pool.size").tags(userTags).tag("name", "fjp").gauge();
}

@DisplayName("ScheduledExecutorService can be monitored with a default set of metrics")
@ParameterizedTest
@CsvSource({ "custom,custom.", "custom.,custom.", ",''", "' ',''" })
Expand Down