Skip to content

Commit

Permalink
wip: Added working Old GC test
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosroman committed Dec 14, 2023
1 parent 96a45bc commit dc55bbb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 22 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@
<version>1.19.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<scm>
<connection>scm:git:[email protected]:Datadog/jmxfetch.git</connection>
Expand Down
94 changes: 77 additions & 17 deletions src/test/java/org/datadog/jmxfetch/TestGCMetrics.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package org.datadog.jmxfetch;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.junit.Test;

import lombok.extern.slf4j.Slf4j;

import org.datadog.jmxfetch.reporter.ConsoleReporter;
import org.datadog.jmxfetch.util.MisbehavingJMXServer;
import org.junit.Test;
import org.testcontainers.containers.output.Slf4jLogConsumer;

@Slf4j
public class TestGCMetrics extends TestCommon {

private static final int RMI_PORT = 9090;
private static final int CONTROL_PORT = 9091;
private static final int SUPERVISOR_PORT = 9092;
private static final Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);

private static boolean isDomainPresent(final String domain, final MBeanServerConnection mbs) {
boolean found = false;
Expand All @@ -44,28 +51,22 @@ private static boolean isDomainPresent(final String domain, final MBeanServerCon
*/
@Test
public void testJMXDirectBasic() throws Exception {
try (final MisbehavingJMXServer server = new MisbehavingJMXServer(
RMI_PORT,
CONTROL_PORT,
try (final MisbehavingJMXServer server = new MisbehavingJMXServer(RMI_PORT, CONTROL_PORT,
SUPERVISOR_PORT)) {
server.start();
final String ipAddress = server.getIp();
final String remoteJmxServiceUrl = String.format(
"service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi",
ipAddress, RMI_PORT
);
"service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", ipAddress, RMI_PORT);
final JMXServiceURL jmxUrl = new JMXServiceURL(remoteJmxServiceUrl);
final JMXConnector conn = JMXConnectorFactory.connect(jmxUrl);
final MBeanServerConnection mBeanServerConnection = conn.getMBeanServerConnection();
assertTrue(isDomainPresent("Bohnanza", mBeanServerConnection));
assertThat(isDomainPresent("Bohnanza", mBeanServerConnection), is(true));
}
}

@Test
public void testJMXFetchBasic() throws IOException {
try (final MisbehavingJMXServer server = new MisbehavingJMXServer(
RMI_PORT,
CONTROL_PORT,
try (final MisbehavingJMXServer server = new MisbehavingJMXServer(RMI_PORT, CONTROL_PORT,
SUPERVISOR_PORT)) {
server.start();
final String ipAddress = server.getIp();
Expand All @@ -84,8 +85,67 @@ public void testJMXFetchBasic() throws IOException {
" domain: Bohnanza"
);
this.app.doIteration();
List<Map<String, Object>> metrics = ((ConsoleReporter) this.appConfig.getReporter()).getMetrics();
assertEquals(1, metrics.size());
final List<Map<String, Object>> metrics = ((ConsoleReporter) this.appConfig.getReporter()).getMetrics();
assertThat(metrics, hasSize(1));
}
}

@Test
public void testDefaultOldGC() throws IOException {
try (final MisbehavingJMXServer server = new MisbehavingJMXServer(RMI_PORT, CONTROL_PORT,
SUPERVISOR_PORT)) {
server.start();
final String ipAddress = server.getIp();
this.initApplicationWithYamlLines("init_config:",
" is_jmx: true",
"",
"instances:",
" - name: jmxint_container",
" host: " + ipAddress,
" collect_default_jvm_metrics: true",
" max_returned_metrics: 300000",
" port: " + RMI_PORT);
this.app.doIteration();
final List<Map<String, Object>> actualMetrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics();
List<String> gcGenerations = Arrays.asList(
"G1 Old Generation",
"G1 Young Generation");
assertGCMetric(actualMetrics, "jvm.gc.cms.count", gcGenerations);
assertGCMetric(actualMetrics, "jvm.gc.parnew.time", gcGenerations);
}
}

private static void assertGCMetric(final List<Map<String, Object>> actualMetrics,
final String expectedMetric,
final List<String> gcGenerations) {
final List<Map<String, Object>> filteredMetrics = new ArrayList<>();
for (Map<String, Object> actualMetric : actualMetrics) {
final String name = (String) actualMetric.get("name");
if(expectedMetric.equals(name)) {
filteredMetrics.add(actualMetric);
}
}
assertThat(filteredMetrics, hasSize(2));
for (final String name : gcGenerations) {
log.debug("Asserting for metric '{}'", name);
boolean found = false;
for (Map<String, Object> filteredMetric : filteredMetrics) {
final Set<String> mTags = new HashSet<>(
Arrays.asList((String[]) (filteredMetric.get("tags"))));

if(mTags.contains(String.format("name:%s", name))) {
assertThat(mTags, not(empty()));
assertThat(mTags, hasSize(5));
log.debug("mTags '{}' has size: {}\n{}", name, mTags.size(), mTags);
assertThat(mTags, hasItems(
"instance:jmxint_container",
"jmx_domain:java.lang",
"type:GarbageCollector",
String.format("name:%s", name)));
found = true;
}
}
assertThat(String.format("Did not find metric '%s'", name), found, is(true));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collections;
import lombok.extern.slf4j.Slf4j;
import org.datadog.jmxfetch.JMXServerControlClient;
import org.datadog.jmxfetch.JMXServerSupervisorClient;
Expand All @@ -15,6 +14,9 @@
public class MisbehavingJMXServer implements Startable {

private static final String DEFAULT_JDK_IMAGE = "base";
private static final String RMI_PORT = "RMI_PORT";
private static final String CONTROL_PORT = "CONTROL_PORT";
private static final String SUPERVISOR_PORT = "SUPERVISOR_PORT";
private final String jdkImage;
private final int controlPort;
private final int supervisorPort;
Expand All @@ -40,9 +42,9 @@ public MisbehavingJMXServer(
final ImageFromDockerfile img = new ImageFromDockerfile()
.withFileFromPath(".", Paths.get("./tools/misbehaving-jmx-server/"));
this.server = new GenericContainer<>(img)
.withEnv(Collections.singletonMap("RMI_PORT", "" + rmiPort))
.withEnv(Collections.singletonMap("CONTROL_PORT", "" + controlPort))
.withEnv(Collections.singletonMap("SUPERVISOR_PORT", "" + supervisorPort))
.withEnv(RMI_PORT, String.valueOf(rmiPort))
.withEnv(CONTROL_PORT, String.valueOf(controlPort))
.withEnv(SUPERVISOR_PORT, String.valueOf(supervisorPort))
.waitingFor(Wait.forLogMessage(
".*Supervisor HTTP Server Started. Waiting for initialization payload POST to /init.*",
1));
Expand Down

0 comments on commit dc55bbb

Please sign in to comment.