Skip to content

Commit 4c50c9e

Browse files
committed
Allow basic SystemPublicMetrics on GAE
Update SystemPublicMetrics to silently ignore ManagementFactory NoClassDefFoundErrors which can occur when deploying to Google App Engine. Fixes gh-2701
1 parent 6a6ae64 commit 4c50c9e

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/SystemPublicMetrics.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ public int getOrder() {
5555
public Collection<Metric<?>> metrics() {
5656
Collection<Metric<?>> result = new LinkedHashSet<Metric<?>>();
5757
addBasicMetrics(result);
58-
addHeapMetrics(result);
59-
addThreadMetrics(result);
60-
addClassLoadingMetrics(result);
61-
addGarbageCollectionMetrics(result);
58+
addManagementMetrics(result);
6259
return result;
6360
}
6461

@@ -67,17 +64,34 @@ public Collection<Metric<?>> metrics() {
6764
* @param result the result
6865
*/
6966
protected void addBasicMetrics(Collection<Metric<?>> result) {
67+
// NOTE: ManagementFactory must not be used here since it fails on GAE
7068
result.add(new Metric<Long>("mem", Runtime.getRuntime().totalMemory() / 1024));
7169
result.add(new Metric<Long>("mem.free", Runtime.getRuntime().freeMemory() / 1024));
7270
result.add(new Metric<Integer>("processors", Runtime.getRuntime()
7371
.availableProcessors()));
74-
// Add JVM up time in ms
75-
result.add(new Metric<Long>("uptime", ManagementFactory.getRuntimeMXBean()
76-
.getUptime()));
7772
result.add(new Metric<Long>("instance.uptime", System.currentTimeMillis()
7873
- this.timestamp));
79-
result.add(new Metric<Double>("systemload.average", ManagementFactory
80-
.getOperatingSystemMXBean().getSystemLoadAverage()));
74+
}
75+
76+
/**
77+
* Add metrics from ManagementFactory if possible. Note that ManagementFactory is not
78+
* available on Google App Engine.
79+
*/
80+
private void addManagementMetrics(Collection<Metric<?>> result) {
81+
try {
82+
// Add JVM up time in ms
83+
result.add(new Metric<Long>("uptime", ManagementFactory.getRuntimeMXBean()
84+
.getUptime()));
85+
result.add(new Metric<Double>("systemload.average", ManagementFactory
86+
.getOperatingSystemMXBean().getSystemLoadAverage()));
87+
addHeapMetrics(result);
88+
addThreadMetrics(result);
89+
addClassLoadingMetrics(result);
90+
addGarbageCollectionMetrics(result);
91+
}
92+
catch (NoClassDefFoundError ex) {
93+
// Expected on Google App Engine
94+
}
8195
}
8296

8397
/**

0 commit comments

Comments
 (0)