Skip to content

Commit 17b552c

Browse files
rainerjungfstab
authored andcommitted
Add "jvm_memory_pool_collection_bytes_xxx" providing access to MemoryPoolMXBean.getCollectionUsage()
Signed-off-by: Rainer Jung <[email protected]>
1 parent 5452fae commit 17b552c

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/MemoryPoolsExports.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,26 @@ void addMemoryPoolMetrics(List<MetricFamilySamples> sampleFamilies) {
102102
"Initial bytes of a given JVM memory pool.",
103103
Collections.singletonList("pool"));
104104
sampleFamilies.add(init);
105+
GaugeMetricFamily collectionUsed = new GaugeMetricFamily(
106+
"jvm_memory_pool_collection_bytes_used",
107+
"Used bytes after last collection of a given JVM memory pool.",
108+
Collections.singletonList("pool"));
109+
sampleFamilies.add(collectionUsed);
110+
GaugeMetricFamily collectionCommitted = new GaugeMetricFamily(
111+
"jvm_memory_pool_collection_bytes_committed",
112+
"Committed after last collection bytes of a given JVM memory pool.",
113+
Collections.singletonList("pool"));
114+
sampleFamilies.add(collectionCommitted);
115+
GaugeMetricFamily collectionMax = new GaugeMetricFamily(
116+
"jvm_memory_pool_collection_bytes_max",
117+
"Max bytes after last collection of a given JVM memory pool.",
118+
Collections.singletonList("pool"));
119+
sampleFamilies.add(collectionMax);
120+
GaugeMetricFamily collectionInit = new GaugeMetricFamily(
121+
"jvm_memory_pool_collection_bytes_init",
122+
"Initial after last collection bytes of a given JVM memory pool.",
123+
Collections.singletonList("pool"));
124+
sampleFamilies.add(collectionInit);
105125
for (final MemoryPoolMXBean pool : poolBeans) {
106126
MemoryUsage poolUsage = pool.getUsage();
107127
used.addMetric(
@@ -116,6 +136,21 @@ void addMemoryPoolMetrics(List<MetricFamilySamples> sampleFamilies) {
116136
init.addMetric(
117137
Collections.singletonList(pool.getName()),
118138
poolUsage.getInit());
139+
MemoryUsage collectionPoolUsage = pool.getCollectionUsage();
140+
if (collectionPoolUsage != null) {
141+
collectionUsed.addMetric(
142+
Collections.singletonList(pool.getName()),
143+
collectionPoolUsage.getUsed());
144+
collectionCommitted.addMetric(
145+
Collections.singletonList(pool.getName()),
146+
collectionPoolUsage.getCommitted());
147+
collectionMax.addMetric(
148+
Collections.singletonList(pool.getName()),
149+
collectionPoolUsage.getMax());
150+
collectionInit.addMetric(
151+
Collections.singletonList(pool.getName()),
152+
collectionPoolUsage.getInit());
153+
}
119154
}
120155
}
121156

simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/MemoryPoolsExportsTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public class MemoryPoolsExportsTest {
2020
private MemoryPoolMXBean mockPoolsBean2 = Mockito.mock(MemoryPoolMXBean.class);
2121
private MemoryMXBean mockMemoryBean = Mockito.mock(MemoryMXBean.class);
2222
private MemoryUsage mockUsage1 = Mockito.mock(MemoryUsage.class);
23+
private MemoryUsage mockCollectionUsage1 = Mockito.mock(MemoryUsage.class);
2324
private MemoryUsage mockUsage2 = Mockito.mock(MemoryUsage.class);
25+
private MemoryUsage mockCollectionUsage2 = Mockito.mock(MemoryUsage.class);
2426
private List<MemoryPoolMXBean> mockList = Arrays.asList(mockPoolsBean1, mockPoolsBean2);
2527
private CollectorRegistry registry = new CollectorRegistry();
2628
private MemoryPoolsExports collectorUnderTest;
@@ -29,18 +31,28 @@ public class MemoryPoolsExportsTest {
2931
public void setUp() {
3032
when(mockPoolsBean1.getName()).thenReturn("PS Eden Space");
3133
when(mockPoolsBean1.getUsage()).thenReturn(mockUsage1);
34+
when(mockPoolsBean1.getCollectionUsage()).thenReturn(mockCollectionUsage1);
3235
when(mockPoolsBean2.getName()).thenReturn("PS Old Gen");
3336
when(mockPoolsBean2.getUsage()).thenReturn(mockUsage2);
37+
when(mockPoolsBean2.getCollectionUsage()).thenReturn(mockCollectionUsage2);
3438
when(mockMemoryBean.getHeapMemoryUsage()).thenReturn(mockUsage1);
3539
when(mockMemoryBean.getNonHeapMemoryUsage()).thenReturn(mockUsage2);
3640
when(mockUsage1.getUsed()).thenReturn(500000L);
3741
when(mockUsage1.getCommitted()).thenReturn(1000000L);
3842
when(mockUsage1.getMax()).thenReturn(2000000L);
3943
when(mockUsage1.getInit()).thenReturn(1000L);
44+
when(mockCollectionUsage1.getUsed()).thenReturn(400000L);
45+
when(mockCollectionUsage1.getCommitted()).thenReturn(800000L);
46+
when(mockCollectionUsage1.getMax()).thenReturn(1600000L);
47+
when(mockCollectionUsage1.getInit()).thenReturn(2000L);
4048
when(mockUsage2.getUsed()).thenReturn(10000L);
4149
when(mockUsage2.getCommitted()).thenReturn(20000L);
4250
when(mockUsage2.getMax()).thenReturn(3000000L);
4351
when(mockUsage2.getInit()).thenReturn(2000L);
52+
when(mockCollectionUsage2.getUsed()).thenReturn(20000L);
53+
when(mockCollectionUsage2.getCommitted()).thenReturn(40000L);
54+
when(mockCollectionUsage2.getMax()).thenReturn(6000000L);
55+
when(mockCollectionUsage2.getInit()).thenReturn(4000L);
4456
collectorUnderTest = new MemoryPoolsExports(mockMemoryBean, mockList).register(registry);
4557
}
4658

@@ -74,6 +86,34 @@ public void testMemoryPools() {
7486
new String[]{"pool"},
7587
new String[]{"PS Eden Space"}),
7688
.0000001);
89+
assertEquals(
90+
400000L,
91+
registry.getSampleValue(
92+
"jvm_memory_pool_collection_bytes_used",
93+
new String[]{"pool"},
94+
new String[]{"PS Eden Space"}),
95+
.0000001);
96+
assertEquals(
97+
800000L,
98+
registry.getSampleValue(
99+
"jvm_memory_pool_collection_bytes_committed",
100+
new String[]{"pool"},
101+
new String[]{"PS Eden Space"}),
102+
.0000001);
103+
assertEquals(
104+
1600000L,
105+
registry.getSampleValue(
106+
"jvm_memory_pool_collection_bytes_max",
107+
new String[]{"pool"},
108+
new String[]{"PS Eden Space"}),
109+
.0000001);
110+
assertEquals(
111+
2000L,
112+
registry.getSampleValue(
113+
"jvm_memory_pool_collection_bytes_init",
114+
new String[]{"pool"},
115+
new String[]{"PS Eden Space"}),
116+
.0000001);
77117
assertEquals(
78118
10000L,
79119
registry.getSampleValue(
@@ -102,6 +142,34 @@ public void testMemoryPools() {
102142
new String[]{"pool"},
103143
new String[]{"PS Old Gen"}),
104144
.0000001);
145+
assertEquals(
146+
20000L,
147+
registry.getSampleValue(
148+
"jvm_memory_pool_collection_bytes_used",
149+
new String[]{"pool"},
150+
new String[]{"PS Old Gen"}),
151+
.0000001);
152+
assertEquals(
153+
40000L,
154+
registry.getSampleValue(
155+
"jvm_memory_pool_collection_bytes_committed",
156+
new String[]{"pool"},
157+
new String[]{"PS Old Gen"}),
158+
.0000001);
159+
assertEquals(
160+
6000000L,
161+
registry.getSampleValue(
162+
"jvm_memory_pool_collection_bytes_max",
163+
new String[]{"pool"},
164+
new String[]{"PS Old Gen"}),
165+
.0000001);
166+
assertEquals(
167+
4000L,
168+
registry.getSampleValue(
169+
"jvm_memory_pool_collection_bytes_init",
170+
new String[]{"pool"},
171+
new String[]{"PS Old Gen"}),
172+
.0000001);
105173
}
106174

107175
@Test

0 commit comments

Comments
 (0)