From 4c50c9eaa8ea0ad04ca7319842955a3cdbdb46d7 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 24 Mar 2015 13:56:12 -0700 Subject: [PATCH] Allow basic SystemPublicMetrics on GAE Update SystemPublicMetrics to silently ignore ManagementFactory NoClassDefFoundErrors which can occur when deploying to Google App Engine. Fixes gh-2701 --- .../actuate/endpoint/SystemPublicMetrics.java | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/SystemPublicMetrics.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/SystemPublicMetrics.java index 31b7a98cad2..bc0ffad3d37 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/SystemPublicMetrics.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/SystemPublicMetrics.java @@ -55,10 +55,7 @@ public class SystemPublicMetrics implements PublicMetrics, Ordered { public Collection> metrics() { Collection> result = new LinkedHashSet>(); addBasicMetrics(result); - addHeapMetrics(result); - addThreadMetrics(result); - addClassLoadingMetrics(result); - addGarbageCollectionMetrics(result); + addManagementMetrics(result); return result; } @@ -67,17 +64,34 @@ public class SystemPublicMetrics implements PublicMetrics, Ordered { * @param result the result */ protected void addBasicMetrics(Collection> result) { + // NOTE: ManagementFactory must not be used here since it fails on GAE result.add(new Metric("mem", Runtime.getRuntime().totalMemory() / 1024)); result.add(new Metric("mem.free", Runtime.getRuntime().freeMemory() / 1024)); result.add(new Metric("processors", Runtime.getRuntime() .availableProcessors())); - // Add JVM up time in ms - result.add(new Metric("uptime", ManagementFactory.getRuntimeMXBean() - .getUptime())); result.add(new Metric("instance.uptime", System.currentTimeMillis() - this.timestamp)); - result.add(new Metric("systemload.average", ManagementFactory - .getOperatingSystemMXBean().getSystemLoadAverage())); + } + + /** + * Add metrics from ManagementFactory if possible. Note that ManagementFactory is not + * available on Google App Engine. + */ + private void addManagementMetrics(Collection> result) { + try { + // Add JVM up time in ms + result.add(new Metric("uptime", ManagementFactory.getRuntimeMXBean() + .getUptime())); + result.add(new Metric("systemload.average", ManagementFactory + .getOperatingSystemMXBean().getSystemLoadAverage())); + addHeapMetrics(result); + addThreadMetrics(result); + addClassLoadingMetrics(result); + addGarbageCollectionMetrics(result); + } + catch (NoClassDefFoundError ex) { + // Expected on Google App Engine + } } /**