-
Notifications
You must be signed in to change notification settings - Fork 822
#576 add SafepointExports #577
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
Open
mr-git
wants to merge
3
commits into
prometheus:main
Choose a base branch
from
mr-git:576
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/SafepointExports.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
mr-git marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "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; | ||
| } | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/SafepointExportsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.