Skip to content

Commit 1c4b564

Browse files
committed
#576 add SafepointExports
Signed-off-by: mr-git <[email protected]>
1 parent 40e6eec commit 1c4b564

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static void register(CollectorRegistry registry) {
4242
new ThreadExports().register(registry);
4343
new ClassLoadingExports().register(registry);
4444
new VersionInfoExports().register(registry);
45+
new SafepointExports().register(registry);
4546
}
4647

4748
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.prometheus.client.hotspot;
2+
3+
import io.prometheus.client.Collector;
4+
import io.prometheus.client.CounterMetricFamily;
5+
import sun.management.HotspotRuntimeMBean;
6+
import sun.management.ManagementFactoryHelper;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* Exports metrics about JVM Safepoints.
13+
* <p>
14+
* Example usage:
15+
* <pre>
16+
* {@code
17+
* new SafepointExports().register();
18+
* }
19+
* </pre>
20+
* Example metrics being exported:
21+
* <pre>
22+
* jvm_safepoint_count{} 200
23+
* jvm_safepoint_total_time_seconds{} 6.7
24+
* jvm_safepoint_sync_time_seconds{} 6.7
25+
* </pre>
26+
*/
27+
public class SafepointExports extends Collector {
28+
private final HotspotRuntimeMBean hotspotRuntimeMBean;
29+
30+
public SafepointExports() {
31+
this(ManagementFactoryHelper.getHotspotRuntimeMBean());
32+
}
33+
34+
SafepointExports(HotspotRuntimeMBean hotspotRuntimeMBean) {
35+
this.hotspotRuntimeMBean = hotspotRuntimeMBean;
36+
}
37+
38+
public List<MetricFamilySamples> collect() {
39+
CounterMetricFamily safepointCount = new CounterMetricFamily(
40+
"jvm_safepoint_count",
41+
"The number of safepoints taken place since the JVM started.",
42+
hotspotRuntimeMBean.getSafepointCount());
43+
44+
CounterMetricFamily safepointTime = new CounterMetricFamily(
45+
"jvm_safepoint_total_time_seconds",
46+
"The accumulated time spent at safepoints in seconds. This is the accumulated elapsed time that the application has been stopped for safepoint operations.",
47+
hotspotRuntimeMBean.getTotalSafepointTime() / MILLISECONDS_PER_SECOND);
48+
49+
CounterMetricFamily safepointSyncTime = new CounterMetricFamily(
50+
"jvm_safepoint_sync_time_seconds",
51+
"The accumulated time spent getting to safepoints in seconds.",
52+
hotspotRuntimeMBean.getSafepointSyncTime() / MILLISECONDS_PER_SECOND);
53+
54+
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
55+
mfs.add(safepointCount);
56+
mfs.add(safepointTime);
57+
mfs.add(safepointSyncTime);
58+
59+
return mfs;
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.prometheus.client.hotspot;
2+
3+
import io.prometheus.client.CollectorRegistry;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.mockito.Mockito;
7+
import sun.management.HotspotRuntimeMBean;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.mockito.Mockito.when;
11+
12+
public class SafepointExportsTest {
13+
14+
private HotspotRuntimeMBean mockHotspotRuntimeBean = Mockito.mock(HotspotRuntimeMBean.class);
15+
private CollectorRegistry registry = new CollectorRegistry();
16+
private SafepointExports collectorUnderTest;
17+
18+
private static final String[] EMPTY_LABEL = new String[0];
19+
20+
21+
@Before
22+
public void setUp() {
23+
when(mockHotspotRuntimeBean.getSafepointCount()).thenReturn(300L);
24+
when(mockHotspotRuntimeBean.getTotalSafepointTime()).thenReturn(13L);
25+
when(mockHotspotRuntimeBean.getSafepointSyncTime()).thenReturn(31L);
26+
collectorUnderTest = new SafepointExports(mockHotspotRuntimeBean).register(registry);
27+
}
28+
29+
@Test
30+
public void testSafepoints() {
31+
assertEquals(
32+
300L,
33+
registry.getSampleValue(
34+
"jvm_safepoint_count", EMPTY_LABEL, EMPTY_LABEL),
35+
.0000001);
36+
assertEquals(
37+
0.013,
38+
registry.getSampleValue(
39+
"jvm_safepoint_total_time_seconds", EMPTY_LABEL, EMPTY_LABEL),
40+
.0000001);
41+
assertEquals(
42+
0.031,
43+
registry.getSampleValue(
44+
"jvm_safepoint_sync_time_seconds", EMPTY_LABEL, EMPTY_LABEL),
45+
.0000001);
46+
}
47+
}

0 commit comments

Comments
 (0)