Skip to content

Instantly share code, notes, and snippets.

@michael-pratt
Created July 25, 2018 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-pratt/47a9d796729bb482a0965352a21e0370 to your computer and use it in GitHub Desktop.
Save michael-pratt/47a9d796729bb482a0965352a21e0370 to your computer and use it in GitHub Desktop.
Spring Boot System and JVM HealthIndicator
@Configuration
public class SystemHealthIndicator
{
@Bean
public SystemHealthIndicator systemHealthIndicator()
{
return new SystemHealthIndicator();
}
/**
* Spring Boot {@link HealthIndicator} that captures System and JVM
* metrics.
*/
private class SystemHealthIndicator implements HealthIndicator
{
@Override
public Health health()
{
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
OperatingSystemMXBean operatingSystemBean = ManagementFactory.getOperatingSystemMXBean();
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
List<MemoryPoolMXBean> memoryPoolBeans = ManagementFactory.getMemoryPoolMXBeans();
List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
// Status is always UP for this check; we just want the details that
// come with each metric
Health.Builder builder = Health.up();
// Add JVM details
builder.withDetail("jvm", runtimeBean.getVmVendor() + " " +
runtimeBean.getVmName() + " " +
runtimeBean.getSpecVersion() + " " +
runtimeBean.getVmVersion());
// Add system load details
HashMap<String, String> cpuInfo = new HashMap<>();
cpuInfo.put("processors", Integer.toString(operatingSystemBean.getAvailableProcessors()));
cpuInfo.put("load", Double.toString(operatingSystemBean.getSystemLoadAverage()));
builder.withDetail("cpu", cpuInfo);
// Add thread details
Map<String, String> threadInfo = new HashMap<>();
threadInfo.put("current", Integer.toString(threadBean.getThreadCount()));
threadInfo.put("peak", Integer.toString(threadBean.getPeakThreadCount()));
builder.withDetail("threads", threadInfo);
// Add memory details
Map<String, HashMap<String, Long>> memoryInfo = new HashMap<>();
memoryPoolBeans.forEach(bean -> memoryInfo.put(bean.getName(), new HashMap<String, Long>()));
memoryPoolBeans.forEach(bean -> {
memoryInfo.get(bean.getName()).put("init", bean.getUsage().getInit());
memoryInfo.get(bean.getName()).put("committed", bean.getUsage().getCommitted());
memoryInfo.get(bean.getName()).put("used", bean.getUsage().getUsed());
memoryInfo.get(bean.getName()).put("max", bean.getUsage().getMax());
});
builder.withDetail("memory", memoryInfo);
// Add GC details
Map<String, HashMap<String, Long>> gcInfo = new HashMap<>();
gcBeans.forEach(bean -> gcInfo.put(bean.getName(), new HashMap<String, Long>()));
gcBeans.forEach(bean -> {
gcInfo.get(bean.getName()).put("count", bean.getCollectionCount());
gcInfo.get(bean.getName()).put("time", bean.getCollectionTime());
});
builder.withDetail("gc", gcInfo);
return builder.build();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment