|
| 1 | +package io.prometheus.client.hotspot; |
| 2 | + |
| 3 | +import io.prometheus.client.Collector; |
| 4 | + |
| 5 | +import java.lang.management.ManagementFactory; |
| 6 | +import java.lang.management.MemoryMXBean; |
| 7 | +import java.lang.management.MemoryPoolMXBean; |
| 8 | +import java.lang.management.MemoryUsage; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.regex.Pattern; |
| 14 | + |
| 15 | +/** |
| 16 | + * Exports metrics about JVM memory areas. |
| 17 | + * <p> |
| 18 | + * Example usage: |
| 19 | + * <pre> |
| 20 | + * {@code |
| 21 | + * new MemoryPoolsExports().register(); |
| 22 | + * } |
| 23 | + * </pre> |
| 24 | + * Example metrics being exported: |
| 25 | + * <pre> |
| 26 | + * jvm_memory_used{area="heap} 2000000 |
| 27 | + * jvm_memory_limit{area="nonheap"} 200000 |
| 28 | + * jvm_memory_pool_used{pool="PS-Eden-Space"} 2000 |
| 29 | + * </pre> |
| 30 | + */ |
| 31 | +public class MemoryPoolsExports extends Collector { |
| 32 | + private static final Pattern WHITESPACE = Pattern.compile("[\\s]+"); |
| 33 | + static final String MEMORY_USED_METRIC = "jvm_memory_used"; |
| 34 | + static final String MEMORY_LIMIT_METRIC = "jvm_memory_limit"; |
| 35 | + static final String POOLS_USED_METRIC = "jvm_memory_pool_used"; |
| 36 | + static final String POOLS_LIMIT_METRIC = "jvm_memory_pool_limit"; |
| 37 | + |
| 38 | + private static final List<String> MEMORY_LABEL_NAMES = Arrays.asList("area"); |
| 39 | + private static final List<String> MEMORY_HEAP_LABEL = Arrays.asList("heap"); |
| 40 | + private static final List<String> MEMORY_NONHEAP_LABEL = Arrays.asList("nonheap"); |
| 41 | + |
| 42 | + private static final List<String> POOLS_LABEL_NAMES = Arrays.asList("pool"); |
| 43 | + |
| 44 | + private final HashMap<MemoryPoolMXBean, List<String>> poolLabelValues = new HashMap<MemoryPoolMXBean, List<String>>(); |
| 45 | + private final MemoryMXBean memoryBean; |
| 46 | + private final List<MemoryPoolMXBean> poolBeans; |
| 47 | + |
| 48 | + public MemoryPoolsExports() { |
| 49 | + this( |
| 50 | + ManagementFactory.getMemoryMXBean(), |
| 51 | + ManagementFactory.getMemoryPoolMXBeans()); |
| 52 | + } |
| 53 | + |
| 54 | + public MemoryPoolsExports(MemoryMXBean memoryBean, |
| 55 | + List<MemoryPoolMXBean> poolBeans) { |
| 56 | + this.memoryBean = memoryBean; |
| 57 | + this.poolBeans = poolBeans; |
| 58 | + for (final MemoryPoolMXBean pool : poolBeans) { |
| 59 | + if (!poolLabelValues.containsKey(pool)) { |
| 60 | + String gcName = WHITESPACE.matcher(pool.getName()).replaceAll("-"); |
| 61 | + poolLabelValues.put(pool, Arrays.asList(gcName)); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + void addMemoryAreaMetrics(List<MetricFamilySamples> sampleFamilies) { |
| 67 | + MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage(); |
| 68 | + MemoryUsage nonHeapUsage = memoryBean.getNonHeapMemoryUsage(); |
| 69 | + ArrayList<MetricFamilySamples.Sample> usedSamples = new ArrayList<MetricFamilySamples.Sample>(); |
| 70 | + usedSamples.add( |
| 71 | + new MetricFamilySamples.Sample( |
| 72 | + MEMORY_USED_METRIC, |
| 73 | + MEMORY_LABEL_NAMES, |
| 74 | + MEMORY_HEAP_LABEL, |
| 75 | + heapUsage.getUsed())); |
| 76 | + usedSamples.add( |
| 77 | + new MetricFamilySamples.Sample( |
| 78 | + MEMORY_USED_METRIC, |
| 79 | + MEMORY_LABEL_NAMES, |
| 80 | + MEMORY_NONHEAP_LABEL, |
| 81 | + nonHeapUsage.getUsed())); |
| 82 | + sampleFamilies.add( |
| 83 | + new MetricFamilySamples( |
| 84 | + MEMORY_USED_METRIC, |
| 85 | + Type.GAUGE, |
| 86 | + "Used bytes of a given JVM memory area (heap, nonheap).", |
| 87 | + usedSamples)); |
| 88 | + ArrayList<MetricFamilySamples.Sample> limitSamples = new ArrayList<MetricFamilySamples.Sample>(); |
| 89 | + limitSamples.add( |
| 90 | + new MetricFamilySamples.Sample( |
| 91 | + MEMORY_LIMIT_METRIC, |
| 92 | + MEMORY_LABEL_NAMES, |
| 93 | + MEMORY_HEAP_LABEL, |
| 94 | + heapUsage.getMax() == -1 ? heapUsage.getMax() : heapUsage.getCommitted())); |
| 95 | + limitSamples.add( |
| 96 | + new MetricFamilySamples.Sample( |
| 97 | + MEMORY_LIMIT_METRIC, |
| 98 | + MEMORY_LABEL_NAMES, |
| 99 | + MEMORY_NONHEAP_LABEL, |
| 100 | + nonHeapUsage.getMax() == -1 ? nonHeapUsage.getMax() : nonHeapUsage.getCommitted())); |
| 101 | + sampleFamilies.add( |
| 102 | + new MetricFamilySamples( |
| 103 | + MEMORY_LIMIT_METRIC, |
| 104 | + Type.GAUGE, |
| 105 | + "Limit (bytes) of a given JVM memory area (heap, nonheap).", |
| 106 | + limitSamples)); |
| 107 | + } |
| 108 | + |
| 109 | + void addMemoryPoolMetrics(List<MetricFamilySamples> sampleFamilies) { |
| 110 | + ArrayList<MetricFamilySamples.Sample> usedSamples = new ArrayList<MetricFamilySamples.Sample>(); |
| 111 | + ArrayList<MetricFamilySamples.Sample> limitSamples = new ArrayList<MetricFamilySamples.Sample>(); |
| 112 | + for (final MemoryPoolMXBean pool : poolBeans) { |
| 113 | + MemoryUsage poolUsage = pool.getUsage(); |
| 114 | + usedSamples.add( |
| 115 | + new MetricFamilySamples.Sample( |
| 116 | + POOLS_USED_METRIC, |
| 117 | + POOLS_LABEL_NAMES, |
| 118 | + poolLabelValues.get(pool), |
| 119 | + poolUsage.getUsed())); |
| 120 | + limitSamples.add( |
| 121 | + new MetricFamilySamples.Sample( |
| 122 | + POOLS_LIMIT_METRIC, |
| 123 | + POOLS_LABEL_NAMES, |
| 124 | + poolLabelValues.get(pool), |
| 125 | + poolUsage.getMax() != -1 ? poolUsage.getMax() : poolUsage.getCommitted())); |
| 126 | + } |
| 127 | + sampleFamilies.add( |
| 128 | + new MetricFamilySamples( |
| 129 | + POOLS_USED_METRIC, |
| 130 | + Type.GAUGE, |
| 131 | + "Used bytes of a given JVM memory pool.", |
| 132 | + usedSamples)); |
| 133 | + |
| 134 | + sampleFamilies.add( |
| 135 | + new MetricFamilySamples( |
| 136 | + POOLS_LIMIT_METRIC, |
| 137 | + Type.GAUGE, |
| 138 | + "Limit (bytes) of a given JVM memory pool.", |
| 139 | + limitSamples)); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + public List<MetricFamilySamples> collect() { |
| 144 | + List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>(); |
| 145 | + addMemoryAreaMetrics(mfs); |
| 146 | + addMemoryPoolMetrics(mfs); |
| 147 | + return mfs; |
| 148 | + } |
| 149 | +} |
0 commit comments