ThreadStatesGaugeSet implements MetricSet { @Override public Map<String, Metric> getMetrics() { final Map<String, Metric> gauges = new HashMap<>(); for (final Thread.State state : Thread.State.values()) { gauges.put(name(state.toString().toLowerCase(), "count"), (Gauge<Object>) () -> getThreadCount(state)); } gauges.put("count", (Gauge<Integer>) threads::getThreadCount); gauges.put("daemon.count", (Gauge<Integer>) threads::getDaemonThreadCount); gauges.put("peak.count", (Gauge<Integer>) threads::getPeakThreadCount); gauges.put("total_started.count", (Gauge<Long>) threads::getTotalStartedThreadCount); gauges.put("deadlock.count", (Gauge<Integer>) () -> deadlockDetector.getDeadlockedThreads().size()); gauges.put("deadlocks", (Gauge<Set<String>>) deadlockDetector::getDeadlockedThreads); return Collections.unmodifiableMap(gauges); } ThreadStatesGaugeSet();  ThreadStatesGaugeSet(ThreadMXBean threads,
                                ThreadDeadlockDetector deadlockDetector); @Override Map<String, Metric> getMetrics();  }
@Test public void hasASetOfGauges() { assertThat(gauges.getMetrics().keySet()) .containsOnly("terminated.count", "new.count", "count", "timed_waiting.count", "deadlocks", "blocked.count", "waiting.count", "daemon.count", "runnable.count", "deadlock.count", "total_started.count", "peak.count"); }
@Test public void hasAGaugeForEachThreadState() { assertThat(((Gauge<?>) gauges.getMetrics().get("new.count")).getValue()) .isEqualTo(1); assertThat(((Gauge<?>) gauges.getMetrics().get("runnable.count")).getValue()) .isEqualTo(1); assertThat(((Gauge<?>) gauges.getMetrics().get("blocked.count")).getValue()) .isEqualTo(1); assertThat(((Gauge<?>) gauges.getMetrics().get("waiting.count")).getValue()) .isEqualTo(1); assertThat(((Gauge<?>) gauges.getMetrics().get("timed_waiting.count")).getValue()) .isEqualTo(1); assertThat(((Gauge<?>) gauges.getMetrics().get("terminated.count")).getValue()) .isEqualTo(1); }
@Test public void hasAGaugeForTheNumberOfThreads() { assertThat(((Gauge<?>) gauges.getMetrics().get("count")).getValue()) .isEqualTo(12); }
@Test public void hasAGaugeForTheNumberOfDaemonThreads() { assertThat(((Gauge<?>) gauges.getMetrics().get("daemon.count")).getValue()) .isEqualTo(10); }
@Test public void hasAGaugeForAnyDeadlocks() { assertThat(((Gauge<?>) gauges.getMetrics().get("deadlocks")).getValue()) .isEqualTo(deadlocks); }
@Test public void hasAGaugeForAnyDeadlockCount() { assertThat(((Gauge<?>) gauges.getMetrics().get("deadlock.count")).getValue()) .isEqualTo(1); }
@Test public void hasAGaugeForPeakThreadCount() { assertThat(((Gauge<?>) gauges.getMetrics().get("peak.count")).getValue()) .isEqualTo(30); }
@Test public void hasAGaugeForTotalStartedThreadsCount() { assertThat(((Gauge<?>) gauges.getMetrics().get("total_started.count")).getValue()) .isEqualTo(42L); }
@Test public void autoDiscoversTheMXBeans() { final ThreadStatesGaugeSet set = new ThreadStatesGaugeSet(); assertThat(((Gauge<?>) set.getMetrics().get("count")).getValue()) .isNotNull(); assertThat(((Gauge<?>) set.getMetrics().get("deadlocks")).getValue()) .isNotNull(); }