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

Adds ability to exclude deadlocked threads from exported metrics #612

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -32,6 +32,7 @@
*/
public class ThreadExports extends Collector {
private final ThreadMXBean threadBean;
private boolean includeDeadlockedThreads = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs locking.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locking in what way? Better to set it in overloaded constructor?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's one option, this can be accessed and mutated concurrently which is the issue. You could also split this out to a completely separate collector.


public ThreadExports() {
this(ManagementFactory.getThreadMXBean());
Expand All @@ -41,6 +42,14 @@ public ThreadExports(ThreadMXBean threadBean) {
this.threadBean = threadBean;
}

public final boolean isIncludeDeadlockedThreads() {
return includeDeadlockedThreads;
}

public final void setIncludeDeadlockedThreads(boolean includeDeadlockedThreads) {
this.includeDeadlockedThreads = includeDeadlockedThreads;
}

void addThreadMetrics(List<MetricFamilySamples> sampleFamilies) {
sampleFamilies.add(
new GaugeMetricFamily(
Expand All @@ -66,18 +75,20 @@ void addThreadMetrics(List<MetricFamilySamples> sampleFamilies) {
"Started thread count of a JVM",
threadBean.getTotalStartedThreadCount()));

sampleFamilies.add(
new GaugeMetricFamily(
"jvm_threads_deadlocked",
"Cycles of JVM-threads that are in deadlock waiting to acquire object monitors or ownable synchronizers",
nullSafeArrayLength(threadBean.findDeadlockedThreads())));

sampleFamilies.add(
new GaugeMetricFamily(
"jvm_threads_deadlocked_monitor",
"Cycles of JVM-threads that are in deadlock waiting to acquire object monitors",
nullSafeArrayLength(threadBean.findMonitorDeadlockedThreads())));

if (includeDeadlockedThreads) {
sampleFamilies.add(
new GaugeMetricFamily(
"jvm_threads_deadlocked",
"Cycles of JVM-threads that are in deadlock waiting to acquire object monitors or ownable synchronizers",
nullSafeArrayLength(threadBean.findDeadlockedThreads())));

sampleFamilies.add(
new GaugeMetricFamily(
"jvm_threads_deadlocked_monitor",
"Cycles of JVM-threads that are in deadlock waiting to acquire object monitors",
nullSafeArrayLength(threadBean.findMonitorDeadlockedThreads())));
}

GaugeMetricFamily threadStateFamily = new GaugeMetricFamily(
"jvm_threads_state",
"Current count of threads by state",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;

public class ThreadExportsTest {
Expand Down Expand Up @@ -97,4 +98,53 @@ public void testThreadPools() {
"jvm_threads_state", STATE_LABEL, STATE_TERMINATED_LABEL),
.0000001);
}

@Test
public void testThreadPoolsNoDeadlocked() {
collectorUnderTest.setIncludeDeadlockedThreads(false);

assertNull(registry.getSampleValue(
"jvm_threads_deadlocked", EMPTY_LABEL, EMPTY_LABEL));
assertNull(registry.getSampleValue(
"jvm_threads_deadlocked_monitor", EMPTY_LABEL, EMPTY_LABEL));

assertEquals(
300L,
registry.getSampleValue(
"jvm_threads_current", EMPTY_LABEL, EMPTY_LABEL),
.0000001);
assertEquals(
200L,
registry.getSampleValue(
"jvm_threads_daemon", EMPTY_LABEL, EMPTY_LABEL),
.0000001);
assertEquals(
301L,
registry.getSampleValue(
"jvm_threads_peak", EMPTY_LABEL, EMPTY_LABEL),
.0000001);
assertEquals(
503L,
registry.getSampleValue(
"jvm_threads_started_total", 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);
}
}