Skip to content

Commit 9983930

Browse files
committed
remove reflection
1 parent 23c5c12 commit 9983930

File tree

2 files changed

+26
-19
lines changed
  • instrumentation/runtime-telemetry/runtime-telemetry-java8/library/src

2 files changed

+26
-19
lines changed

instrumentation/runtime-telemetry/runtime-telemetry-java8/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java8/Threads.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.lang.management.ManagementFactory;
2020
import java.lang.management.ThreadInfo;
2121
import java.lang.management.ThreadMXBean;
22-
import java.lang.reflect.Method;
2322
import java.util.ArrayList;
2423
import java.util.HashMap;
2524
import java.util.List;
@@ -58,7 +57,7 @@ public final class Threads {
5857

5958
/** Register observers for java runtime class metrics. */
6059
public static List<AutoCloseable> registerObservers(OpenTelemetry openTelemetry) {
61-
return INSTANCE.registerObservers(openTelemetry, !isJava9OrNewer() && GET_THREADS != null);
60+
return INSTANCE.registerObservers(openTelemetry, !isJava9OrNewer());
6261
}
6362

6463
private List<AutoCloseable> registerObservers(OpenTelemetry openTelemetry, boolean useThread) {
@@ -100,7 +99,6 @@ private static <T> List<AutoCloseable> registerObservers(
10099
}
101100

102101
@Nullable private static final MethodHandle THREAD_INFO_IS_DAEMON;
103-
@Nullable private static final Method GET_THREADS;
104102

105103
static {
106104
MethodHandle isDaemon;
@@ -112,17 +110,6 @@ private static <T> List<AutoCloseable> registerObservers(
112110
isDaemon = null;
113111
}
114112
THREAD_INFO_IS_DAEMON = isDaemon;
115-
Method getThreads = null;
116-
// only on jdk8
117-
if (THREAD_INFO_IS_DAEMON == null) {
118-
try {
119-
getThreads = Thread.class.getDeclaredMethod("getThreads");
120-
getThreads.setAccessible(true);
121-
} catch (Exception e) {
122-
getThreads = null;
123-
}
124-
}
125-
GET_THREADS = getThreads;
126113
}
127114

128115
private static boolean isJava9OrNewer() {
@@ -153,12 +140,22 @@ private static Consumer<ObservableLongMeasurement> java8ThreadCallback(
153140
};
154141
}
155142

156-
private static Thread[] getThreads() {
157-
try {
158-
return requireNonNull((Thread[]) GET_THREADS.invoke(null));
159-
} catch (Exception e) {
160-
throw new IllegalStateException("Unexpected error happened during Thread#getThreads()", e);
143+
// Visible for testing
144+
static Thread[] getThreads() {
145+
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
146+
while (threadGroup.getParent() != null) {
147+
threadGroup = threadGroup.getParent();
148+
}
149+
// use a slightly larger array in case new threads are created
150+
int count = threadGroup.activeCount() + 10;
151+
Thread[] threads = new Thread[count];
152+
int resultSize = threadGroup.enumerate(threads);
153+
if (resultSize == threads.length) {
154+
return threads;
161155
}
156+
Thread[] result = new Thread[resultSize];
157+
System.arraycopy(threads, 0, result, 0, resultSize);
158+
return result;
162159
}
163160

164161
private static Consumer<ObservableLongMeasurement> java9AndNewerCallback(

instrumentation/runtime-telemetry/runtime-telemetry-java8/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/java8/ThreadsStableSemconvTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
1919
import java.lang.management.ThreadInfo;
2020
import java.lang.management.ThreadMXBean;
21+
import java.util.Arrays;
22+
import java.util.HashSet;
23+
import java.util.Set;
2124
import org.junit.jupiter.api.Test;
2225
import org.junit.jupiter.api.condition.EnabledForJreRange;
2326
import org.junit.jupiter.api.condition.JRE;
@@ -158,6 +161,13 @@ void registerObservers_Java9AndNewer() {
158161
equalTo(JVM_THREAD_STATE, "waiting"))))));
159162
}
160163

164+
@Test
165+
void getThreads() {
166+
Thread[] threads = Threads.getThreads();
167+
Set<Thread> set = new HashSet<>(Arrays.asList(threads));
168+
assertThat(set).contains(Thread.currentThread());
169+
}
170+
161171
static final class ThreadInfoAnswer implements Answer<Object> {
162172

163173
private final boolean isDaemon;

0 commit comments

Comments
 (0)