Skip to content

Commit

Permalink
Support AutoShutdownDelegatedExecutorService in ExecutorServiceMetrics (
Browse files Browse the repository at this point in the history
#5811)

* Add support for `AutoShutdownDelegatedExecutorService`

This executor is returned by `Executors#newSingleThreadExecutor` under Java 21.

Signed-off-by: nscuro <nscuro@protonmail.com>
Signed-off-by: Johnny Lim <izeye@naver.com>

* Add test for AutoShutdownDelegatedExecutorService support in ExecutorServiceMetrics

Signed-off-by: Johnny Lim <izeye@naver.com>

---------

Signed-off-by: nscuro <nscuro@protonmail.com>
Signed-off-by: Johnny Lim <izeye@naver.com>
Co-authored-by: nscuro <nscuro@protonmail.com>
  • Loading branch information
izeye and nscuro authored Jan 20, 2025
1 parent 2ba3d54 commit 14d0cd8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ else if (allowIllegalReflectiveAccess) {
if (className.equals("java.util.concurrent.Executors$DelegatedScheduledExecutorService")) {
monitor(registry, unwrapThreadPoolExecutor(executorService, executorService.getClass()));
}
else if (className.equals("java.util.concurrent.Executors$FinalizableDelegatedExecutorService")) {
else if (className.equals("java.util.concurrent.Executors$FinalizableDelegatedExecutorService")
|| className.equals("java.util.concurrent.Executors$AutoShutdownDelegatedExecutorService")) {
monitor(registry,
unwrapThreadPoolExecutor(executorService, executorService.getClass().getSuperclass()));
}
Expand Down
9 changes: 7 additions & 2 deletions micrometer-java21/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ task reflectiveTests(type: Test) {
includeTags 'reflective'
}

// This hack is needed since VirtualThreadMetricsReflectiveTests utilizes reflection against java.lang, see its javadoc
jvmArgs += ['--add-opens', 'java.base/java.lang=ALL-UNNAMED']
// This hack is needed for the following tests:
// - VirtualThreadMetricsReflectiveTests utilizes reflection against java.lang.
// - ExecutorServiceMetricsReflectiveTests utilizes reflection against java.util.concurrent.
jvmArgs += [
'--add-opens', 'java.base/java.lang=ALL-UNNAMED',
'--add-opens', 'java.base/java.util.concurrent=ALL-UNNAMED'
]
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2025 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.core.instrument.binder.jvm;

import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.util.concurrent.*;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link ExecutorServiceMetrics} with reflection enabled.
*
* @author Tommy Ludwig
*/
@Tag("reflective")
class ExecutorServiceMetricsReflectiveTests {

SimpleMeterRegistry registry = new SimpleMeterRegistry();

@Test
void threadPoolMetricsWith_AutoShutdownDelegatedExecutorService() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
ExecutorService unmonitored = Executors.newSingleThreadExecutor();
assertThat(unmonitored.getClass().getName())
.isEqualTo("java.util.concurrent.Executors$AutoShutdownDelegatedExecutorService");
ExecutorService monitored = ExecutorServiceMetrics.monitor(registry, unmonitored, "test");
monitored.execute(latch::countDown);
assertThat(latch.await(100, TimeUnit.MILLISECONDS)).isTrue();
assertThat(registry.get("executor.completed").tag("name", "test").functionCounter().count()).isEqualTo(1L);
}

}

0 comments on commit 14d0cd8

Please sign in to comment.