From 1c4b5646f2926ea431fe26a8ad3b00795bd04ec6 Mon Sep 17 00:00:00 2001 From: mr-git <8796159+mr-git@users.noreply.github.com> Date: Sun, 11 Oct 2020 21:10:09 +0300 Subject: [PATCH 1/3] #576 add SafepointExports Signed-off-by: mr-git <8796159+mr-git@users.noreply.github.com> --- .../client/hotspot/DefaultExports.java | 1 + .../client/hotspot/SafepointExports.java | 61 +++++++++++++++++++ .../client/hotspot/SafepointExportsTest.java | 47 ++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java create mode 100644 simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java diff --git a/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/DefaultExports.java b/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/DefaultExports.java index 79cd6ed5e..ee041cbd1 100644 --- a/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/DefaultExports.java +++ b/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/DefaultExports.java @@ -42,6 +42,7 @@ public static void register(CollectorRegistry registry) { new ThreadExports().register(registry); new ClassLoadingExports().register(registry); new VersionInfoExports().register(registry); + new SafepointExports().register(registry); } } diff --git a/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java b/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java new file mode 100644 index 000000000..631a8aa0d --- /dev/null +++ b/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java @@ -0,0 +1,61 @@ +package io.prometheus.client.hotspot; + +import io.prometheus.client.Collector; +import io.prometheus.client.CounterMetricFamily; +import sun.management.HotspotRuntimeMBean; +import sun.management.ManagementFactoryHelper; + +import java.util.ArrayList; +import java.util.List; + +/** + * Exports metrics about JVM Safepoints. + *

+ * Example usage: + *

+ * {@code
+ *   new SafepointExports().register();
+ * }
+ * 
+ * Example metrics being exported: + *
+ *   jvm_safepoint_count{} 200
+ *   jvm_safepoint_total_time_seconds{} 6.7
+ *   jvm_safepoint_sync_time_seconds{} 6.7
+ * 
+ */ +public class SafepointExports extends Collector { + private final HotspotRuntimeMBean hotspotRuntimeMBean; + + public SafepointExports() { + this(ManagementFactoryHelper.getHotspotRuntimeMBean()); + } + + SafepointExports(HotspotRuntimeMBean hotspotRuntimeMBean) { + this.hotspotRuntimeMBean = hotspotRuntimeMBean; + } + + public List collect() { + CounterMetricFamily safepointCount = new CounterMetricFamily( + "jvm_safepoint_count", + "The number of safepoints taken place since the JVM started.", + hotspotRuntimeMBean.getSafepointCount()); + + CounterMetricFamily safepointTime = new CounterMetricFamily( + "jvm_safepoint_total_time_seconds", + "The accumulated time spent at safepoints in seconds. This is the accumulated elapsed time that the application has been stopped for safepoint operations.", + hotspotRuntimeMBean.getTotalSafepointTime() / MILLISECONDS_PER_SECOND); + + CounterMetricFamily safepointSyncTime = new CounterMetricFamily( + "jvm_safepoint_sync_time_seconds", + "The accumulated time spent getting to safepoints in seconds.", + hotspotRuntimeMBean.getSafepointSyncTime() / MILLISECONDS_PER_SECOND); + + List mfs = new ArrayList(); + mfs.add(safepointCount); + mfs.add(safepointTime); + mfs.add(safepointSyncTime); + + return mfs; + } +} diff --git a/simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java b/simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java new file mode 100644 index 000000000..fc929a5e4 --- /dev/null +++ b/simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java @@ -0,0 +1,47 @@ +package io.prometheus.client.hotspot; + +import io.prometheus.client.CollectorRegistry; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import sun.management.HotspotRuntimeMBean; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +public class SafepointExportsTest { + + private HotspotRuntimeMBean mockHotspotRuntimeBean = Mockito.mock(HotspotRuntimeMBean.class); + private CollectorRegistry registry = new CollectorRegistry(); + private SafepointExports collectorUnderTest; + + private static final String[] EMPTY_LABEL = new String[0]; + + + @Before + public void setUp() { + when(mockHotspotRuntimeBean.getSafepointCount()).thenReturn(300L); + when(mockHotspotRuntimeBean.getTotalSafepointTime()).thenReturn(13L); + when(mockHotspotRuntimeBean.getSafepointSyncTime()).thenReturn(31L); + collectorUnderTest = new SafepointExports(mockHotspotRuntimeBean).register(registry); + } + + @Test + public void testSafepoints() { + assertEquals( + 300L, + registry.getSampleValue( + "jvm_safepoint_count", EMPTY_LABEL, EMPTY_LABEL), + .0000001); + assertEquals( + 0.013, + registry.getSampleValue( + "jvm_safepoint_total_time_seconds", EMPTY_LABEL, EMPTY_LABEL), + .0000001); + assertEquals( + 0.031, + registry.getSampleValue( + "jvm_safepoint_sync_time_seconds", EMPTY_LABEL, EMPTY_LABEL), + .0000001); + } +} From f05e847edadddd377692de5ec388dfbe69bd3b77 Mon Sep 17 00:00:00 2001 From: mr-git <8796159+mr-git@users.noreply.github.com> Date: Tue, 20 Oct 2020 21:08:42 +0300 Subject: [PATCH 2/3] #576 add SafepointExports (Counters should end in _total) Signed-off-by: mr-git <8796159+mr-git@users.noreply.github.com> --- .../java/io/prometheus/client/hotspot/SafepointExports.java | 4 ++-- .../io/prometheus/client/hotspot/SafepointExportsTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java b/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java index 631a8aa0d..cabf32e44 100644 --- a/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java +++ b/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java @@ -19,7 +19,7 @@ * * Example metrics being exported: *
- *   jvm_safepoint_count{} 200
+ *   jvm_safepoint_count_total{} 200
  *   jvm_safepoint_total_time_seconds{} 6.7
  *   jvm_safepoint_sync_time_seconds{} 6.7
  * 
@@ -37,7 +37,7 @@ public SafepointExports() { public List collect() { CounterMetricFamily safepointCount = new CounterMetricFamily( - "jvm_safepoint_count", + "jvm_safepoint_count_total", "The number of safepoints taken place since the JVM started.", hotspotRuntimeMBean.getSafepointCount()); diff --git a/simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java b/simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java index fc929a5e4..9ec04502e 100644 --- a/simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java +++ b/simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java @@ -31,7 +31,7 @@ public void testSafepoints() { assertEquals( 300L, registry.getSampleValue( - "jvm_safepoint_count", EMPTY_LABEL, EMPTY_LABEL), + "jvm_safepoint_count_total", EMPTY_LABEL, EMPTY_LABEL), .0000001); assertEquals( 0.013, From ebcc395bd92f1d9a3c107510f0e945af2d69360e Mon Sep 17 00:00:00 2001 From: mr-git <8796159+mr-git@users.noreply.github.com> Date: Tue, 20 Oct 2020 21:41:35 +0300 Subject: [PATCH 3/3] #576 add SafepointExports (Merge two `Counter`s in `Summary`) Signed-off-by: mr-git <8796159+mr-git@users.noreply.github.com> --- .../client/hotspot/SafepointExports.java | 26 ++++++++++--------- .../client/hotspot/SafepointExportsTest.java | 4 +-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java b/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java index cabf32e44..6f1387cef 100644 --- a/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java +++ b/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java @@ -2,10 +2,13 @@ import io.prometheus.client.Collector; import io.prometheus.client.CounterMetricFamily; +import io.prometheus.client.SummaryMetricFamily; import sun.management.HotspotRuntimeMBean; import sun.management.ManagementFactoryHelper; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; /** @@ -19,8 +22,8 @@ * * Example metrics being exported: *
- *   jvm_safepoint_count_total{} 200
- *   jvm_safepoint_total_time_seconds{} 6.7
+ *   jvm_safepoint_seconds_count{} 200
+ *   jvm_safepoint_seconds_sum{} 6.7
  *   jvm_safepoint_sync_time_seconds{} 6.7
  * 
*/ @@ -36,15 +39,15 @@ public SafepointExports() { } public List collect() { - CounterMetricFamily safepointCount = new CounterMetricFamily( - "jvm_safepoint_count_total", - "The number of safepoints taken place since the JVM started.", - hotspotRuntimeMBean.getSafepointCount()); + SummaryMetricFamily safepoint = new SummaryMetricFamily( + "jvm_safepoint_seconds", + "The accumulated time spent at safepoints in seconds. This is the accumulated elapsed time that the application has been stopped for safepoint operations. (count: The number of safepoints taken place since the JVM started.", + Collections.EMPTY_LIST); - CounterMetricFamily safepointTime = new CounterMetricFamily( - "jvm_safepoint_total_time_seconds", - "The accumulated time spent at safepoints in seconds. This is the accumulated elapsed time that the application has been stopped for safepoint operations.", - hotspotRuntimeMBean.getTotalSafepointTime() / MILLISECONDS_PER_SECOND); + safepoint.addMetric( + Collections.EMPTY_LIST, + hotspotRuntimeMBean.getSafepointCount(), + hotspotRuntimeMBean.getTotalSafepointTime() / MILLISECONDS_PER_SECOND); CounterMetricFamily safepointSyncTime = new CounterMetricFamily( "jvm_safepoint_sync_time_seconds", @@ -52,8 +55,7 @@ public List collect() { hotspotRuntimeMBean.getSafepointSyncTime() / MILLISECONDS_PER_SECOND); List mfs = new ArrayList(); - mfs.add(safepointCount); - mfs.add(safepointTime); + mfs.add(safepoint); mfs.add(safepointSyncTime); return mfs; diff --git a/simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java b/simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java index 9ec04502e..469683c13 100644 --- a/simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java +++ b/simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java @@ -31,12 +31,12 @@ public void testSafepoints() { assertEquals( 300L, registry.getSampleValue( - "jvm_safepoint_count_total", EMPTY_LABEL, EMPTY_LABEL), + "jvm_safepoint_seconds_count", EMPTY_LABEL, EMPTY_LABEL), .0000001); assertEquals( 0.013, registry.getSampleValue( - "jvm_safepoint_total_time_seconds", EMPTY_LABEL, EMPTY_LABEL), + "jvm_safepoint_seconds_sum", EMPTY_LABEL, EMPTY_LABEL), .0000001); assertEquals( 0.031,