|
| 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 | +} |
0 commit comments