Skip to content

Commit 065c5da

Browse files
davidkarlsenbrian-brazil
authored andcommitted
Invoke processCpuTime with reflection to bridge ibm/sun diffs (#176)
* Invoke processCpuTime with reflection to bridge ibm/sun diffs Fixes #174
1 parent ddd1533 commit 065c5da

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/StandardExports.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package io.prometheus.client.hotspot;
22

3-
import com.sun.management.OperatingSystemMXBean;
43
import com.sun.management.UnixOperatingSystemMXBean;
4+
import io.prometheus.client.Collector;
5+
import io.prometheus.client.CounterMetricFamily;
6+
import io.prometheus.client.GaugeMetricFamily;
7+
58
import java.io.BufferedReader;
9+
import java.io.FileNotFoundException;
610
import java.io.FileReader;
711
import java.io.IOException;
8-
import java.io.FileNotFoundException;
9-
import java.lang.management.RuntimeMXBean;
1012
import java.lang.management.ManagementFactory;
13+
import java.lang.management.OperatingSystemMXBean;
14+
import java.lang.management.RuntimeMXBean;
15+
import java.lang.reflect.Method;
1116
import java.util.ArrayList;
1217
import java.util.List;
18+
import java.util.logging.Level;
1319
import java.util.logging.Logger;
1420

15-
import io.prometheus.client.Collector;
16-
import io.prometheus.client.CounterMetricFamily;
17-
import io.prometheus.client.GaugeMetricFamily;
18-
1921
/**
2022
* Exports the standard exports common across all prometheus clients.
2123
* <p>
@@ -39,7 +41,7 @@ public class StandardExports extends Collector {
3941

4042
public StandardExports() {
4143
this(new StatusReader(),
42-
(OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean(),
44+
ManagementFactory.getOperatingSystemMXBean(),
4345
ManagementFactory.getRuntimeMXBean());
4446
}
4547

@@ -56,10 +58,26 @@ public StandardExports() {
5658
public List<MetricFamilySamples> collect() {
5759
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
5860

59-
mfs.add(new CounterMetricFamily("process_cpu_seconds_total", "Total user and system CPU time spent in seconds.",
60-
osBean.getProcessCpuTime() / NANOSECONDS_PER_SECOND));
61+
try {
62+
/*
63+
There are two interfaces com.ibm.lang.management.OperatingSystemMXBean and com.sun.management.OperatingSystemMXBean,
64+
but no common interface which exposes getProcessCpuTime. Hence call on the implementing class, which is default access,
65+
so use reflection hack to set it accessible.
66+
*/
67+
Method method = osBean.getClass().getMethod("getProcessCpuTime", null);
68+
if (!method.isAccessible()) {
69+
method.setAccessible(true);
70+
}
71+
Long l = (Long) method.invoke(osBean);
72+
mfs.add(new CounterMetricFamily("process_cpu_seconds_total", "Total user and system CPU time spent in seconds.",
73+
l / NANOSECONDS_PER_SECOND));
74+
}
75+
catch (Exception e) {
76+
LOGGER.log(Level.FINE,"Could not access process cpu time", e);
77+
}
78+
6179

62-
mfs.add(new GaugeMetricFamily("process_start_time_seconds","Start time of the process since unix epoch in seconds.",
80+
mfs.add(new GaugeMetricFamily("process_start_time_seconds", "Start time of the process since unix epoch in seconds.",
6381
runtimeBean.getStartTime() / MILLISECONDS_PER_SECOND));
6482

6583
if (unix) {
@@ -74,7 +92,7 @@ public List<MetricFamilySamples> collect() {
7492
// so add support for just Linux for now.
7593
if (linux) {
7694
try {
77-
collectMemoryMetricsLinux(mfs);
95+
collectMemoryMetricsLinux(mfs);
7896
} catch (Exception e) {
7997
// If the format changes, log a warning and return what we can.
8098
LOGGER.warning(e.toString());

0 commit comments

Comments
 (0)