Skip to content

Commit

Permalink
Added defensive code for scenario where thread id <= 0
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Hoard <doug.hoard@gmail.com>
  • Loading branch information
dhoard authored and fstab committed May 20, 2022
1 parent 2f31b96 commit 2be241c
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import io.prometheus.client.Collector;
import io.prometheus.client.CounterMetricFamily;
import io.prometheus.client.GaugeMetricFamily;
import io.prometheus.client.SampleNameFilter;
import io.prometheus.client.Predicate;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -36,13 +36,16 @@
*/
public class ThreadExports extends Collector {

public static final String UNKNOWN = "UNKNOWN";

public static final String JVM_THREADS_STATE = "jvm_threads_state";

private static final String JVM_THREADS_CURRENT = "jvm_threads_current";
private static final String JVM_THREADS_DAEMON = "jvm_threads_daemon";
private static final String JVM_THREADS_PEAK = "jvm_threads_peak";
private static final String JVM_THREADS_STARTED_TOTAL = "jvm_threads_started_total";
private static final String JVM_THREADS_DEADLOCKED = "jvm_threads_deadlocked";
private static final String JVM_THREADS_DEADLOCKED_MONITOR = "jvm_threads_deadlocked_monitor";
private static final String JVM_THREADS_STATE = "jvm_threads_state";

private final ThreadMXBean threadBean;

Expand Down Expand Up @@ -109,35 +112,51 @@ void addThreadMetrics(List<MetricFamilySamples> sampleFamilies, Predicate<String
"Current count of threads by state",
Collections.singletonList("state"));

Map<Thread.State, Integer> threadStateCounts = getThreadStateCountMap();
for (Map.Entry<Thread.State, Integer> entry : threadStateCounts.entrySet()) {
Map<String, Integer> threadStateCounts = getThreadStateCountMap();
for (Map.Entry<String, Integer> entry : threadStateCounts.entrySet()) {
threadStateFamily.addMetric(
Collections.singletonList(entry.getKey().toString()),
Collections.singletonList(entry.getKey()),
entry.getValue()
);
}
sampleFamilies.add(threadStateFamily);
}
}

private Map<Thread.State, Integer> getThreadStateCountMap() {
private Map<String, Integer> getThreadStateCountMap() {
long[] threadIds = threadBean.getAllThreadIds();

// Code to remove any thread id values <= 0
int writePos = 0;
for (int i = 0; i < threadIds.length; i++) {
if (threadIds[i] > 0) {
threadIds[writePos++] = threadIds[i];
}
}

int numberOfInvalidThreadIds = threadIds.length - writePos;
threadIds = Arrays.copyOf(threadIds, writePos);

// Get thread information without computing any stack traces
ThreadInfo[] allThreads = threadBean.getThreadInfo(threadBean.getAllThreadIds(), 0);
ThreadInfo[] allThreads = threadBean.getThreadInfo(threadIds, 0);

// Initialize the map with all thread states
HashMap<Thread.State, Integer> threadCounts = new HashMap<Thread.State, Integer>();
HashMap<String, Integer> threadCounts = new HashMap<String, Integer>();
for (Thread.State state : Thread.State.values()) {
threadCounts.put(state, 0);
threadCounts.put(state.name(), 0);
}

// Collect the actual thread counts
for (ThreadInfo curThread : allThreads) {
if (curThread != null) {
Thread.State threadState = curThread.getThreadState();
threadCounts.put(threadState, threadCounts.get(threadState) + 1);
threadCounts.put(threadState.name(), threadCounts.get(threadState.name()) + 1);
}
}

// Add the thread count for invalid thread ids
threadCounts.put(UNKNOWN, numberOfInvalidThreadIds);

return threadCounts;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.prometheus.client.hotspot;

import io.prometheus.client.Collector.MetricFamilySamples;
import io.prometheus.client.CollectorRegistry;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -97,4 +101,98 @@ public void testThreadPools() {
"jvm_threads_state", STATE_LABEL, STATE_TERMINATED_LABEL),
.0000001);
}

@Test
public void testInvalidThreadIds() {
ThreadExports threadExports = new ThreadExports();

// Number of threads to create with invalid thread ids
int numberOfInvalidThreadIds = 2;

// Get the current thread state counts
Map<String, Double> expectedThreadStateCountMap = new HashMap<String, Double>();
List<MetricFamilySamples> metricFamilySamplesList = threadExports.collect();
for (MetricFamilySamples metricFamilySamples : metricFamilySamplesList) {
if (ThreadExports.JVM_THREADS_STATE.equals(metricFamilySamples.name)) {
for (MetricFamilySamples.Sample sample : metricFamilySamples.samples) {
expectedThreadStateCountMap.put(ThreadExports.JVM_THREADS_STATE + "-" + sample.labelValues.get(0), sample.value);
}
}
}

// Add numberOfInvalidThreadIds to the expected UNKNOWN thread state count
expectedThreadStateCountMap.put(
ThreadExports.JVM_THREADS_STATE + "-" + ThreadExports.UNKNOWN,
expectedThreadStateCountMap.get(
ThreadExports.JVM_THREADS_STATE + "-" + ThreadExports.UNKNOWN) + numberOfInvalidThreadIds);

final CountDownLatch countDownLatch = new CountDownLatch(numberOfInvalidThreadIds);

try {
// Create and start threads with invalid thread ids (id=0, id=-1, etc.)
for (int i = 0; i < numberOfInvalidThreadIds; i++) {
new TestThread(-i, new TestRunnable(countDownLatch)).start();
}

// Get the current thread state counts
Map<String, Double> actualThreadStateCountMap = new HashMap<String, Double>();
metricFamilySamplesList = threadExports.collect();
for (MetricFamilySamples metricFamilySamples : metricFamilySamplesList) {
if (ThreadExports.JVM_THREADS_STATE.equals(metricFamilySamples.name)) {
for (MetricFamilySamples.Sample sample : metricFamilySamples.samples) {
actualThreadStateCountMap.put(ThreadExports.JVM_THREADS_STATE + "-" + sample.labelValues.get(0), sample.value);
}
}
}

// Assert that we have the same number of thread states
assertEquals(expectedThreadStateCountMap.size(), actualThreadStateCountMap.size());

// Check each thread state count
for (String threadState : expectedThreadStateCountMap.keySet()) {
double expectedThreadStateCount = expectedThreadStateCountMap.get(threadState);
double actualThreadStateCount = actualThreadStateCountMap.get(threadState);

// Assert the expected and actual thread count states are equal
assertEquals(expectedThreadStateCount, actualThreadStateCount, 0.0);
}
} finally {
for (int i = 0; i < numberOfInvalidThreadIds; i++) {
countDownLatch.countDown();
}
}
}

private class TestThread extends Thread {

private long id;

public TestThread(long id, Runnable runnable) {
super(runnable);
setDaemon(true);
this.id = id;
}

public long getId() {
return this.id;
}
}

private class TestRunnable implements Runnable {

private CountDownLatch countDownLatch;

public TestRunnable(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}

@Override
public void run() {
try {
countDownLatch.await();
} catch (InterruptedException e) {
// DO NOTHING
}
}
}
}

0 comments on commit 2be241c

Please sign in to comment.