diff --git a/pom.xml b/pom.xml index 15b614823..3b1ab826d 100644 --- a/pom.xml +++ b/pom.xml @@ -203,6 +203,12 @@ 1.19.3 test + + org.hamcrest + hamcrest-library + 1.3 + test + scm:git:git@github.com:Datadog/jmxfetch.git diff --git a/src/test/java/org/datadog/jmxfetch/TestGCMetrics.java b/src/test/java/org/datadog/jmxfetch/TestGCMetrics.java index 8b27a38a3..a8945fbcd 100644 --- a/src/test/java/org/datadog/jmxfetch/TestGCMetrics.java +++ b/src/test/java/org/datadog/jmxfetch/TestGCMetrics.java @@ -1,19 +1,27 @@ 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 { @@ -21,7 +29,6 @@ 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; @@ -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(); @@ -84,8 +85,67 @@ public void testJMXFetchBasic() throws IOException { " domain: Bohnanza" ); this.app.doIteration(); - List> metrics = ((ConsoleReporter) this.appConfig.getReporter()).getMetrics(); - assertEquals(1, metrics.size()); + final List> 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> actualMetrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + List 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> actualMetrics, + final String expectedMetric, + final List gcGenerations) { + final List> filteredMetrics = new ArrayList<>(); + for (Map 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 filteredMetric : filteredMetrics) { + final Set 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)); } } } diff --git a/src/test/java/org/datadog/jmxfetch/TestReconnectContainer.java b/src/test/java/org/datadog/jmxfetch/TestReconnectContainer.java index 518bb4b8c..ace89ccdf 100644 --- a/src/test/java/org/datadog/jmxfetch/TestReconnectContainer.java +++ b/src/test/java/org/datadog/jmxfetch/TestReconnectContainer.java @@ -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; diff --git a/src/test/java/org/datadog/jmxfetch/util/MisbehavingJMXServer.java b/src/test/java/org/datadog/jmxfetch/util/MisbehavingJMXServer.java index 4c0331752..06896deb6 100644 --- a/src/test/java/org/datadog/jmxfetch/util/MisbehavingJMXServer.java +++ b/src/test/java/org/datadog/jmxfetch/util/MisbehavingJMXServer.java @@ -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; @@ -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; @@ -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));