Skip to content

Commit

Permalink
Various cleanups in instrumentation-jvm (#1016)
Browse files Browse the repository at this point in the history
* Various cleanups in instrumentation-jvm

Signed-off-by: Mickael Maison <[email protected]>

* Add comment in ProcessMetricsTest.testGoodCase

Signed-off-by: Mickael Maison <[email protected]>

---------

Signed-off-by: Mickael Maison <[email protected]>
Co-authored-by: Gregor Zeitlinger <[email protected]>
  • Loading branch information
mimaison and zeitlinger authored Oct 2, 2024
1 parent fd816bd commit 12fbc0d
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ public class JvmMemoryPoolAllocationMetrics {
private static final String JVM_MEMORY_POOL_ALLOCATED_BYTES_TOTAL =
"jvm_memory_pool_allocated_bytes_total";

private final PrometheusProperties config;
private final List<GarbageCollectorMXBean> garbageCollectorBeans;

private JvmMemoryPoolAllocationMetrics(
List<GarbageCollectorMXBean> garbageCollectorBeans, PrometheusProperties config) {
this.garbageCollectorBeans = garbageCollectorBeans;
this.config = config;
}

private void register(PrometheusRegistry registry) {
Expand All @@ -80,7 +78,7 @@ private void register(PrometheusRegistry registry) {

static class AllocationCountingNotificationListener implements NotificationListener {

private final Map<String, Long> lastMemoryUsage = new HashMap<String, Long>();
private final Map<String, Long> lastMemoryUsage = new HashMap<>();
private final Counter counter;

AllocationCountingNotificationListener(Counter counter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,8 @@ private Consumer<GaugeWithCallback.Callback> makeCallback(Boolean reserved) {
Matcher matcher = pattern.matcher(summary);
while (matcher.find()) {
String category = matcher.group(1);
long value;
if (reserved) {
value = Long.parseLong(matcher.group(2));
} else {
value = Long.parseLong(matcher.group(3));
}
long value =
reserved ? Long.parseLong(matcher.group(2)) : Long.parseLong(matcher.group(3));
callback.call(value, category);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private Map<String, Integer> getThreadStateCountMap(ThreadMXBean threadBean) {
ThreadInfo[] allThreads = threadBean.getThreadInfo(threadIds, 0);

// Initialize the map with all thread states
HashMap<String, Integer> threadCounts = new HashMap<String, Integer>();
Map<String, Integer> threadCounts = new HashMap<>();
for (Thread.State state : Thread.State.values()) {
threadCounts.put(state.name(), 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class ProcessMetrics {
private static final String PROCESS_VIRTUAL_MEMORY_BYTES = "process_virtual_memory_bytes";
private static final String PROCESS_RESIDENT_MEMORY_BYTES = "process_resident_memory_bytes";

private static final File PROC_SELF_STATUS = new File("/proc/self/status");
static final File PROC_SELF_STATUS = new File("/proc/self/status");

private final PrometheusProperties config;
private final OperatingSystemMXBean osBean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.prometheus.metrics.instrumentation.jvm;

import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -11,7 +12,6 @@
import java.io.IOException;
import java.lang.management.BufferPoolMXBean;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void testGoodCase() throws IOException {
+ "jvm_buffer_pool_used_bytes{pool=\"mapped\"} 2345.0\n"
+ "# EOF\n";

Assert.assertEquals(expected, convertToOpenMetricsFormat(snapshots));
assertEquals(expected, convertToOpenMetricsFormat(snapshots));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.prometheus.metrics.instrumentation.jvm;

import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -10,14 +11,13 @@
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.io.IOException;
import java.lang.management.ClassLoadingMXBean;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public class JvmClassLoadingMetricsTest {

private ClassLoadingMXBean mockClassLoadingBean = Mockito.mock(ClassLoadingMXBean.class);
private final ClassLoadingMXBean mockClassLoadingBean = Mockito.mock(ClassLoadingMXBean.class);

@Before
public void setUp() {
Expand Down Expand Up @@ -45,7 +45,7 @@ public void testGoodCase() throws IOException {
+ "jvm_classes_unloaded_total 500.0\n"
+ "# EOF\n";

Assert.assertEquals(expected, convertToOpenMetricsFormat(snapshots));
assertEquals(expected, convertToOpenMetricsFormat(snapshots));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.prometheus.metrics.instrumentation.jvm;

import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.internal.verification.VerificationModeFactory.times;
Expand All @@ -10,18 +11,17 @@
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.io.IOException;
import java.lang.management.CompilationMXBean;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public class JvmCompilationMetricsTest {

private CompilationMXBean mockCompilationBean = Mockito.mock(CompilationMXBean.class);
private final CompilationMXBean mockCompilationBean = Mockito.mock(CompilationMXBean.class);

@Before
public void setUp() {
when(mockCompilationBean.getTotalCompilationTime()).thenReturn(10000l);
when(mockCompilationBean.getTotalCompilationTime()).thenReturn(10000L);
when(mockCompilationBean.isCompilationTimeMonitoringSupported()).thenReturn(true);
}

Expand All @@ -39,7 +39,7 @@ public void testGoodCase() throws IOException {
+ "jvm_compilation_time_seconds_total 10.0\n"
+ "# EOF\n";

Assert.assertEquals(expected, convertToOpenMetricsFormat(snapshots));
assertEquals(expected, convertToOpenMetricsFormat(snapshots));
}

@Test
Expand All @@ -54,6 +54,6 @@ public void testIgnoredMetricNotScraped() {
MetricSnapshots snapshots = registry.scrape(filter);

verify(mockCompilationBean, times(0)).getTotalCompilationTime();
Assert.assertEquals(0, snapshots.size());
assertEquals(0, snapshots.size());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.prometheus.metrics.instrumentation.jvm;

import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -12,15 +13,14 @@
import java.lang.management.GarbageCollectorMXBean;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public class JvmGarbageCollectorMetricsTest {

private GarbageCollectorMXBean mockGcBean1 = Mockito.mock(GarbageCollectorMXBean.class);
private GarbageCollectorMXBean mockGcBean2 = Mockito.mock(GarbageCollectorMXBean.class);
private final GarbageCollectorMXBean mockGcBean1 = Mockito.mock(GarbageCollectorMXBean.class);
private final GarbageCollectorMXBean mockGcBean2 = Mockito.mock(GarbageCollectorMXBean.class);

@Before
public void setUp() {
Expand Down Expand Up @@ -51,7 +51,7 @@ public void testGoodCase() throws IOException {
+ "jvm_gc_collection_seconds_sum{gc=\"MyGC2\"} 20.0\n"
+ "# EOF\n";

Assert.assertEquals(expected, convertToOpenMetricsFormat(snapshots));
assertEquals(expected, convertToOpenMetricsFormat(snapshots));
}

@Test
Expand All @@ -67,6 +67,6 @@ public void testIgnoredMetricNotScraped() {

verify(mockGcBean1, times(0)).getCollectionTime();
verify(mockGcBean1, times(0)).getCollectionCount();
Assert.assertEquals(0, snapshots.size());
assertEquals(0, snapshots.size());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.prometheus.metrics.instrumentation.jvm;

import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -13,22 +14,21 @@
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public class JvmMemoryMetricsTest {

private MemoryMXBean mockMemoryBean = Mockito.mock(MemoryMXBean.class);
private MemoryPoolMXBean mockPoolsBeanEdenSpace = Mockito.mock(MemoryPoolMXBean.class);
private MemoryPoolMXBean mockPoolsBeanOldGen = Mockito.mock(MemoryPoolMXBean.class);
private MemoryUsage memoryUsageHeap = Mockito.mock(MemoryUsage.class);
private MemoryUsage memoryUsageNonHeap = Mockito.mock(MemoryUsage.class);
private MemoryUsage memoryUsagePoolEdenSpace = Mockito.mock(MemoryUsage.class);
private MemoryUsage memoryUsagePoolOldGen = Mockito.mock(MemoryUsage.class);
private MemoryUsage memoryUsagePoolCollectionEdenSpace = Mockito.mock(MemoryUsage.class);
private MemoryUsage memoryUsagePoolCollectionOldGen = Mockito.mock(MemoryUsage.class);
private final MemoryMXBean mockMemoryBean = Mockito.mock(MemoryMXBean.class);
private final MemoryPoolMXBean mockPoolsBeanEdenSpace = Mockito.mock(MemoryPoolMXBean.class);
private final MemoryPoolMXBean mockPoolsBeanOldGen = Mockito.mock(MemoryPoolMXBean.class);
private final MemoryUsage memoryUsageHeap = Mockito.mock(MemoryUsage.class);
private final MemoryUsage memoryUsageNonHeap = Mockito.mock(MemoryUsage.class);
private final MemoryUsage memoryUsagePoolEdenSpace = Mockito.mock(MemoryUsage.class);
private final MemoryUsage memoryUsagePoolOldGen = Mockito.mock(MemoryUsage.class);
private final MemoryUsage memoryUsagePoolCollectionEdenSpace = Mockito.mock(MemoryUsage.class);
private final MemoryUsage memoryUsagePoolCollectionOldGen = Mockito.mock(MemoryUsage.class);

@Before
public void setUp() {
Expand Down Expand Up @@ -154,7 +154,7 @@ public void testGoodCase() throws IOException {
+ "jvm_memory_used_bytes{area=\"nonheap\"} 6.0\n"
+ "# EOF\n";

Assert.assertEquals(expected, convertToOpenMetricsFormat(snapshots));
assertEquals(expected, convertToOpenMetricsFormat(snapshots));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.prometheus.metrics.instrumentation.jvm;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import io.prometheus.metrics.core.metrics.Counter;
import io.prometheus.metrics.instrumentation.jvm.JvmMemoryPoolAllocationMetrics.AllocationCountingNotificationListener;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import org.junit.Assert;
import org.junit.Test;

public class JvmMemoryPoolAllocationMetricsTest {
Expand Down Expand Up @@ -60,7 +60,7 @@ private double getCountByPool(String metricName, String poolName, MetricSnapshot
}
}
}
Assert.fail("pool " + poolName + " not found.");
fail("pool " + poolName + " not found.");
return 0.0;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package io.prometheus.metrics.instrumentation.jvm;

import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.io.IOException;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

public class JvmNativeMemoryMetricsTest extends TestCase {
public class JvmNativeMemoryMetricsTest {

@Test
public void testNativeMemoryTrackingFail() throws IOException {
Expand All @@ -28,7 +27,7 @@ public void testNativeMemoryTrackingFail() throws IOException {

String expected = "# EOF\n";

Assert.assertEquals(expected, convertToOpenMetricsFormat(snapshots));
assertEquals(expected, convertToOpenMetricsFormat(snapshots));
}

@Test
Expand All @@ -45,7 +44,7 @@ public void testNativeMemoryTrackingEmpty() throws IOException {

String expected = "# EOF\n";

Assert.assertEquals(expected, convertToOpenMetricsFormat(snapshots));
assertEquals(expected, convertToOpenMetricsFormat(snapshots));
}

@Test
Expand All @@ -63,7 +62,7 @@ public void testNativeMemoryTrackingDisabled() throws IOException {

String expected = "# EOF\n";

Assert.assertEquals(expected, convertToOpenMetricsFormat(snapshots));
assertEquals(expected, convertToOpenMetricsFormat(snapshots));
}

@Test
Expand Down Expand Up @@ -226,6 +225,6 @@ public void testNativeMemoryTrackingEnabled() throws IOException {
+ "jvm_native_memory_reserved_bytes{pool=\"Tracing\"} 33097.0\n"
+ "# EOF\n";

Assert.assertEquals(expected, convertToOpenMetricsFormat(snapshots));
assertEquals(expected, convertToOpenMetricsFormat(snapshots));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.prometheus.metrics.instrumentation.jvm;

import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.junit.Assert.assertEquals;

import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;

public class JvmRuntimeInfoMetricTest {
Expand All @@ -27,6 +27,6 @@ public void testGoodCase() throws IOException {
+ "jvm_runtime_info{runtime=\"OpenJDK Runtime Environment\",vendor=\"Oracle Corporation\",version=\"1.8.0_382-b05\"} 1\n"
+ "# EOF\n";

Assert.assertEquals(expected, convertToOpenMetricsFormat(snapshots));
assertEquals(expected, convertToOpenMetricsFormat(snapshots));
}
}
Loading

0 comments on commit 12fbc0d

Please sign in to comment.