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

Fix thread state metrics not being exposed #419

Merged
merged 2 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -90,6 +90,7 @@ void addThreadMetrics(List<MetricFamilySamples> sampleFamilies) {
entry.getValue()
);
}
sampleFamilies.add(threadStateFamily);
}

private Map<Thread.State, Integer> getThreadStateCountMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
Expand All @@ -16,8 +17,16 @@ public class ThreadExportsTest {
private ThreadMXBean mockThreadsBean = Mockito.mock(ThreadMXBean.class);
private CollectorRegistry registry = new CollectorRegistry();
private ThreadExports collectorUnderTest;
private ThreadInfo mockThreadInfoBlocked = Mockito.mock(ThreadInfo.class);
private ThreadInfo mockThreadInfoRunnable1 = Mockito.mock(ThreadInfo.class);
private ThreadInfo mockThreadInfoRunnable2 = Mockito.mock(ThreadInfo.class);

private static final String[] EMPTY_LABEL = new String[0];
private static final String[] STATE_LABEL = {"state"};
private static final String[] STATE_BLOCKED_LABEL = {Thread.State.BLOCKED.toString()};
private static final String[] STATE_RUNNABLE_LABEL = {Thread.State.RUNNABLE.toString()};
private static final String[] STATE_TERMINATED_LABEL = {Thread.State.TERMINATED.toString()};


@Before
public void setUp() {
Expand All @@ -28,7 +37,12 @@ public void setUp() {
when(mockThreadsBean.findDeadlockedThreads()).thenReturn(new long[]{1L,2L,3L});
when(mockThreadsBean.findMonitorDeadlockedThreads()).thenReturn(new long[]{2L,3L,4L});
when(mockThreadsBean.getAllThreadIds()).thenReturn(new long[]{3L,4L,5L});
when(mockThreadsBean.getThreadInfo(new long[]{3L,4L,5L}, 0)).thenReturn(new ThreadInfo[] {});
when(mockThreadInfoBlocked.getThreadState()).thenReturn(Thread.State.BLOCKED);
when(mockThreadInfoRunnable1.getThreadState()).thenReturn(Thread.State.RUNNABLE);
when(mockThreadInfoRunnable2.getThreadState()).thenReturn(Thread.State.RUNNABLE);
when(mockThreadsBean.getThreadInfo(new long[]{3L, 4L, 5L}, 0)).thenReturn(new ThreadInfo[]{
mockThreadInfoBlocked, mockThreadInfoRunnable1, mockThreadInfoRunnable2
});
collectorUnderTest = new ThreadExports(mockThreadsBean).register(registry);
}

Expand Down Expand Up @@ -64,5 +78,23 @@ public void testThreadPools() {
registry.getSampleValue(
"jvm_threads_deadlocked_monitor", EMPTY_LABEL, EMPTY_LABEL),
.0000001);

assertEquals(
1L,
registry.getSampleValue(
"jvm_threads_state", STATE_LABEL, STATE_BLOCKED_LABEL),
.0000001);

assertEquals(
2L,
registry.getSampleValue(
"jvm_threads_state", STATE_LABEL, STATE_RUNNABLE_LABEL),
.0000001);

assertEquals(
0L,
registry.getSampleValue(
"jvm_threads_state", STATE_LABEL, STATE_TERMINATED_LABEL),
.0000001);
}
}