Skip to content

Commit 022f3af

Browse files
authored
jvm.gc.live.data.size and max not updating for optavgpause/optthruput collectors (#2874)
With the OpenJ9 non-generational collectors `optavgpause` and `optthruput`, a NPE was happening in the notification listener which caused some code in it to not be executed. Namely, setting the live and max data size was being effectively skipped for these collectors. Now we check for `null` and only the allocated bytes counter is skipped when no allocation pool name is set.
1 parent 2e18d82 commit 022f3af

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jvm/JvmGcMetrics.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ private boolean isGenerationalGcConfigured() {
195195
}
196196

197197
private void countPoolSizeDelta(Map<String, MemoryUsage> before, Map<String, MemoryUsage> after, Counter counter,
198-
AtomicLong previousPoolSize, String poolName) {
198+
AtomicLong previousPoolSize, @Nullable String poolName) {
199+
if (poolName == null) {
200+
return;
201+
}
199202
final long beforeBytes = before.get(poolName).getUsed();
200203
final long afterBytes = after.get(poolName).getUsed();
201204
final long delta = beforeBytes - previousPoolSize.get();

micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jvm/JvmGcMetricsTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import static org.assertj.core.api.Assertions.assertThat;
3535
import static org.assertj.core.api.Assertions.within;
3636
import static org.awaitility.Awaitility.await;
37-
import static org.junit.jupiter.api.Assumptions.assumeTrue;
3837

3938
/**
4039
* Tests for {@link JvmGcMetrics}.
@@ -65,12 +64,18 @@ void noJvmImplementationSpecificApiSignatures() {
6564
}
6665

6766
@Test
68-
void metersAreBound() {
69-
assertThat(registry.find("jvm.gc.live.data.size").gauge()).isNotNull();
70-
assertThat(registry.find("jvm.gc.memory.allocated").counter()).isNotNull();
71-
assertThat(registry.find("jvm.gc.max.data.size").gauge().value()).isGreaterThan(0);
67+
void gcMetricsAvailableAfterGc() {
68+
System.gc();
69+
await().timeout(200, TimeUnit.MILLISECONDS).alias("NotificationListener takes time after GC")
70+
.untilAsserted(() ->
71+
assertThat(registry.find("jvm.gc.live.data.size").gauge().value()).isPositive());
72+
assertThat(registry.find("jvm.gc.memory.allocated").counter().count()).isPositive();
73+
assertThat(registry.find("jvm.gc.max.data.size").gauge().value()).isPositive();
7274

73-
assumeTrue(binder.isGenerationalGc);
75+
if (!binder.isGenerationalGc) {
76+
return;
77+
}
78+
// cannot guarantee an object was promoted, so cannot check for positive count
7479
assertThat(registry.find("jvm.gc.memory.promoted").counter()).isNotNull();
7580
}
7681

0 commit comments

Comments
 (0)