|
3 | 3 | import io.prometheus.client.Collector; |
4 | 4 | import io.prometheus.client.CounterMetricFamily; |
5 | 5 | import io.prometheus.client.GaugeMetricFamily; |
6 | | -import io.prometheus.client.SampleNameFilter; |
7 | 6 | import io.prometheus.client.Predicate; |
8 | 7 |
|
9 | 8 | import java.lang.management.ManagementFactory; |
10 | 9 | import java.lang.management.ThreadInfo; |
11 | 10 | import java.lang.management.ThreadMXBean; |
12 | 11 | import java.util.ArrayList; |
| 12 | +import java.util.Arrays; |
13 | 13 | import java.util.Collections; |
14 | 14 | import java.util.HashMap; |
15 | 15 | import java.util.List; |
|
36 | 36 | */ |
37 | 37 | public class ThreadExports extends Collector { |
38 | 38 |
|
| 39 | + public static final String UNKNOWN = "UNKNOWN"; |
| 40 | + |
| 41 | + public static final String JVM_THREADS_STATE = "jvm_threads_state"; |
| 42 | + |
39 | 43 | private static final String JVM_THREADS_CURRENT = "jvm_threads_current"; |
40 | 44 | private static final String JVM_THREADS_DAEMON = "jvm_threads_daemon"; |
41 | 45 | private static final String JVM_THREADS_PEAK = "jvm_threads_peak"; |
42 | 46 | private static final String JVM_THREADS_STARTED_TOTAL = "jvm_threads_started_total"; |
43 | 47 | private static final String JVM_THREADS_DEADLOCKED = "jvm_threads_deadlocked"; |
44 | 48 | private static final String JVM_THREADS_DEADLOCKED_MONITOR = "jvm_threads_deadlocked_monitor"; |
45 | | - private static final String JVM_THREADS_STATE = "jvm_threads_state"; |
46 | 49 |
|
47 | 50 | private final ThreadMXBean threadBean; |
48 | 51 |
|
@@ -109,35 +112,51 @@ void addThreadMetrics(List<MetricFamilySamples> sampleFamilies, Predicate<String |
109 | 112 | "Current count of threads by state", |
110 | 113 | Collections.singletonList("state")); |
111 | 114 |
|
112 | | - Map<Thread.State, Integer> threadStateCounts = getThreadStateCountMap(); |
113 | | - for (Map.Entry<Thread.State, Integer> entry : threadStateCounts.entrySet()) { |
| 115 | + Map<String, Integer> threadStateCounts = getThreadStateCountMap(); |
| 116 | + for (Map.Entry<String, Integer> entry : threadStateCounts.entrySet()) { |
114 | 117 | threadStateFamily.addMetric( |
115 | | - Collections.singletonList(entry.getKey().toString()), |
| 118 | + Collections.singletonList(entry.getKey()), |
116 | 119 | entry.getValue() |
117 | 120 | ); |
118 | 121 | } |
119 | 122 | sampleFamilies.add(threadStateFamily); |
120 | 123 | } |
121 | 124 | } |
122 | 125 |
|
123 | | - private Map<Thread.State, Integer> getThreadStateCountMap() { |
| 126 | + private Map<String, Integer> getThreadStateCountMap() { |
| 127 | + long[] threadIds = threadBean.getAllThreadIds(); |
| 128 | + |
| 129 | + // Code to remove any thread id values <= 0 |
| 130 | + int writePos = 0; |
| 131 | + for (int i = 0; i < threadIds.length; i++) { |
| 132 | + if (threadIds[i] > 0) { |
| 133 | + threadIds[writePos++] = threadIds[i]; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + int numberOfInvalidThreadIds = threadIds.length - writePos; |
| 138 | + threadIds = Arrays.copyOf(threadIds, writePos); |
| 139 | + |
124 | 140 | // Get thread information without computing any stack traces |
125 | | - ThreadInfo[] allThreads = threadBean.getThreadInfo(threadBean.getAllThreadIds(), 0); |
| 141 | + ThreadInfo[] allThreads = threadBean.getThreadInfo(threadIds, 0); |
126 | 142 |
|
127 | 143 | // Initialize the map with all thread states |
128 | | - HashMap<Thread.State, Integer> threadCounts = new HashMap<Thread.State, Integer>(); |
| 144 | + HashMap<String, Integer> threadCounts = new HashMap<String, Integer>(); |
129 | 145 | for (Thread.State state : Thread.State.values()) { |
130 | | - threadCounts.put(state, 0); |
| 146 | + threadCounts.put(state.name(), 0); |
131 | 147 | } |
132 | 148 |
|
133 | 149 | // Collect the actual thread counts |
134 | 150 | for (ThreadInfo curThread : allThreads) { |
135 | 151 | if (curThread != null) { |
136 | 152 | Thread.State threadState = curThread.getThreadState(); |
137 | | - threadCounts.put(threadState, threadCounts.get(threadState) + 1); |
| 153 | + threadCounts.put(threadState.name(), threadCounts.get(threadState.name()) + 1); |
138 | 154 | } |
139 | 155 | } |
140 | 156 |
|
| 157 | + // Add the thread count for invalid thread ids |
| 158 | + threadCounts.put(UNKNOWN, numberOfInvalidThreadIds); |
| 159 | + |
141 | 160 | return threadCounts; |
142 | 161 | } |
143 | 162 |
|
|
0 commit comments