Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#576 add SafepointExports #577

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* Example usage:
* <pre>
* {@code
* new SafepointExports().register();
* }
* </pre>
* Example metrics being exported:
* <pre>
* jvm_safepoint_count{} 200
* jvm_safepoint_total_time_seconds{} 6.7
* jvm_safepoint_sync_time_seconds{} 6.7
* </pre>
*/
public class SafepointExports extends Collector {
private final HotspotRuntimeMBean hotspotRuntimeMBean;

public SafepointExports() {
this(ManagementFactoryHelper.getHotspotRuntimeMBean());
}

SafepointExports(HotspotRuntimeMBean hotspotRuntimeMBean) {
this.hotspotRuntimeMBean = hotspotRuntimeMBean;
}

public List<MetricFamilySamples> 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<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
mfs.add(safepointCount);
mfs.add(safepointTime);
mfs.add(safepointSyncTime);

return mfs;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}