Skip to content

Commit

Permalink
Small refactor to remove some magic strings
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosroman committed Dec 21, 2023
1 parent 2484b72 commit 5ca1719
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 63 deletions.
114 changes: 70 additions & 44 deletions src/test/java/org/datadog/jmxfetch/TestGCMetrics.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
package org.datadog.jmxfetch;

import static org.datadog.jmxfetch.util.MetricsAssert.assertDomainPresent;
import static org.datadog.jmxfetch.util.server.app.org.datadog.jmxfetch.util.server.JDKImage.JDK_11;
import static org.datadog.jmxfetch.util.server.app.org.datadog.jmxfetch.util.server.JDKImage.JDK_17;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import java.io.IOException;
import java.util.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
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.junit.Test;

import org.datadog.jmxfetch.reporter.ConsoleReporter;
import org.datadog.jmxfetch.util.MetricsAssert;
import org.datadog.jmxfetch.util.server.MisbehavingJMXServer;
import org.datadog.jmxfetch.util.server.SimpleAppContainer;
import org.datadog.jmxfetch.util.MetricsAssert;

@Slf4j
public class TestGCMetrics extends TestCommon {

@Test
public void testJMXDirectBasic() throws Exception {
try (final SimpleAppContainer container = new SimpleAppContainer()){
try (final SimpleAppContainer container = new SimpleAppContainer()) {
container.start();
final String ipAddress = container.getIp();
final String remoteJmxServiceUrl = String.format(
Expand All @@ -40,9 +51,8 @@ public void testJMXDirectBasic() throws Exception {

@Test
public void testDefaultOldGC() throws IOException {
try (final MisbehavingJMXServer server = new MisbehavingJMXServer(MisbehavingJMXServer.DEFAULT_RMI_PORT, MisbehavingJMXServer.DEFAULT_CONTROL_PORT,
MisbehavingJMXServer.DEFAULT_SUPERVISOR_PORT)) {
final List<Map<String, Object>> actualMetrics = startAngGetMetrics(server, false);
try (final MisbehavingJMXServer server = new MisbehavingJMXServer.Builder().build()) {
final List<Map<String, Object>> actualMetrics = startAndGetMetrics(server, false);
List<String> gcGenerations = Arrays.asList(
"G1 Old Generation",
"G1 Young Generation");
Expand All @@ -53,61 +63,74 @@ public void testDefaultOldGC() throws IOException {

@Test
public void testDefaultNewGCMetricsUseParallelGC() throws IOException {
try (final MisbehavingJMXServer server = new MisbehavingJMXServer(
MisbehavingJMXServer.JDK_11,
"-XX:+UseParallelGC -Xmx128M -Xms128M")) {
final List<Map<String, Object>> actualMetrics = startAngGetMetrics(server, true);
try (final MisbehavingJMXServer server = new MisbehavingJMXServer.Builder().withJDKImage(
JDK_11).appendJavaOpts("-XX:+UseParallelGC").build()) {
final List<Map<String, Object>> actualMetrics = startAndGetMetrics(server, true);
assertThat(actualMetrics, hasSize(13));
assertGCMetric(actualMetrics, "jvm.gc.minor_collection_count", "PS Scavenge", "counter");
assertGCMetric(actualMetrics, "jvm.gc.minor_collection_time", "PS Scavenge", "counter");
assertGCMetric(actualMetrics, "jvm.gc.major_collection_count", "PS MarkSweep", "counter");
assertGCMetric(actualMetrics, "jvm.gc.major_collection_time", "PS MarkSweep", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.minor_collection_count", "PS Scavenge", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.minor_collection_time", "PS Scavenge", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.major_collection_count", "PS MarkSweep", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.major_collection_time", "PS MarkSweep", "counter");
}
}

@Test
public void testDefaultNewGCMetricsUseConcMarkSweepGC() throws IOException {
try (final MisbehavingJMXServer server = new MisbehavingJMXServer(
MisbehavingJMXServer.JDK_11,
"-XX:+UseConcMarkSweepGC -Xmx128M -Xms128M")) {
final List<Map<String, Object>> actualMetrics = startAngGetMetrics(server, true);
try (final MisbehavingJMXServer server = new MisbehavingJMXServer.Builder().withJDKImage(
JDK_11).appendJavaOpts("-XX:+UseConcMarkSweepGC").build()) {
final List<Map<String, Object>> actualMetrics = startAndGetMetrics(server, true);
assertThat(actualMetrics, hasSize(13));
assertGCMetric(actualMetrics, "jvm.gc.minor_collection_count", "ParNew", "counter");
assertGCMetric(actualMetrics, "jvm.gc.minor_collection_time", "ParNew", "counter");
assertGCMetric(actualMetrics, "jvm.gc.major_collection_count", "ConcurrentMarkSweep", "counter");
assertGCMetric(actualMetrics, "jvm.gc.major_collection_time", "ConcurrentMarkSweep", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.minor_collection_count", "ParNew", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.minor_collection_time", "ParNew", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.major_collection_count", "ConcurrentMarkSweep", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.major_collection_time", "ConcurrentMarkSweep", "counter");
}
}

@Test
public void testDefaultNewGCMetricsUseG1GC() throws IOException {
try (final MisbehavingJMXServer server = new MisbehavingJMXServer(
MisbehavingJMXServer.JDK_17,
"-XX:+UseG1GC -Xmx128M -Xms128M")) {
final List<Map<String, Object>> actualMetrics = startAngGetMetrics(server, true);
try (final MisbehavingJMXServer server = new MisbehavingJMXServer.Builder().withJDKImage(
JDK_17).appendJavaOpts("-XX:+UseG1GC").build()) {
final List<Map<String, Object>> actualMetrics = startAndGetMetrics(server, true);
assertThat(actualMetrics, hasSize(13));
assertGCMetric(actualMetrics, "jvm.gc.minor_collection_count", "G1 Young Generation", "counter");
assertGCMetric(actualMetrics, "jvm.gc.minor_collection_time", "G1 Young Generation", "counter");
assertGCMetric(actualMetrics, "jvm.gc.major_collection_count", "G1 Old Generation", "counter");
assertGCMetric(actualMetrics, "jvm.gc.major_collection_time", "G1 Old Generation", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.minor_collection_count", "G1 Young Generation", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.minor_collection_time", "G1 Young Generation", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.major_collection_count", "G1 Old Generation", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.major_collection_time", "G1 Old Generation", "counter");
}
}

@Test
public void testDefaultNewGCMetricsUseZGC() throws IOException {
try (final MisbehavingJMXServer server = new MisbehavingJMXServer(
MisbehavingJMXServer.JDK_17,
"-XX:+UseZGC -Xmx128M -Xms128M")) {
final List<Map<String, Object>> actualMetrics = startAngGetMetrics(server, true);
try (final MisbehavingJMXServer server = new MisbehavingJMXServer.Builder().withJDKImage(
JDK_17).appendJavaOpts("-XX:+UseZGC").build()) {
final List<Map<String, Object>> actualMetrics = startAndGetMetrics(server, true);
assertThat(actualMetrics, hasSize(13));
assertGCMetric(actualMetrics, "jvm.gc.zgc_pauses_collection_count", "ZGC Pauses", "counter");
assertGCMetric(actualMetrics, "jvm.gc.zgc_pauses_collection_time", "ZGC Pauses", "counter");
assertGCMetric(actualMetrics, "jvm.gc.zgc_cycles_collection_count", "ZGC Cycles", "counter");
assertGCMetric(actualMetrics, "jvm.gc.zgc_cycles_collection_time", "ZGC Cycles", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.zgc_pauses_collection_count", "ZGC Pauses", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.zgc_pauses_collection_time", "ZGC Pauses", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.zgc_cycles_collection_count", "ZGC Cycles", "counter");
assertGCMetric(actualMetrics,
"jvm.gc.zgc_cycles_collection_time", "ZGC Cycles", "counter");
}
}

private List<Map<String, Object>> startAngGetMetrics(final MisbehavingJMXServer server, final boolean newGCMetrics) throws IOException {
private List<Map<String, Object>> startAndGetMetrics(final MisbehavingJMXServer server,
final boolean newGCMetrics) throws IOException {
server.start();
this.initApplicationWithYamlLines(
"init_config:",
Expand Down Expand Up @@ -140,7 +163,10 @@ private static void assertGCMetric(final List<Map<String, Object>> actualMetrics
-1,
10.0,
Collections.singletonList(String.format("name:%s", gcGeneration)),
Arrays.asList("instance:jmxint_container", "jmx_domain:java.lang", "type:GarbageCollector"),
Arrays.asList(
"instance:jmxint_container",
"jmx_domain:java.lang",
"type:GarbageCollector"),
5,
metricType,
actualMetrics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

import java.io.IOException;
import java.nio.file.Paths;
import lombok.extern.slf4j.Slf4j;
import org.datadog.jmxfetch.JMXServerControlClient;
import org.datadog.jmxfetch.JMXServerSupervisorClient;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.images.builder.ImageFromDockerfile;
import org.testcontainers.lifecycle.Startable;

import lombok.extern.slf4j.Slf4j;

import org.datadog.jmxfetch.JMXServerControlClient;
import org.datadog.jmxfetch.JMXServerSupervisorClient;
import org.datadog.jmxfetch.util.server.app.org.datadog.jmxfetch.util.server.JDKImage;

@Slf4j
public class MisbehavingJMXServer implements Startable {


public static final int DEFAULT_RMI_PORT = 9090;
public static final int DEFAULT_CONTROL_PORT = 9091;
public static final int DEFAULT_SUPERVISOR_PORT = 9092;
private static final String DEFAULT_JDK_IMAGE = "base";
public static final String JDK_11 = "eclipse-temurin:11";
public static final String JDK_17 = "eclipse-temurin:17";
public static final String JDK_21 = "eclipse-temurin:21";
private static final String DEFAULT_MISBEHAVING_OPTS = "-Xmx128M -Xms128M";
private static final String RMI_PORT = "RMI_PORT";
private static final String CONTROL_PORT = "CONTROL_PORT";
private static final String SUPERVISOR_PORT = "SUPERVISOR_PORT";
Expand All @@ -35,18 +36,6 @@ public class MisbehavingJMXServer implements Startable {
private JMXServerControlClient controlClient;
private JMXServerSupervisorClient supervisorClient;

public MisbehavingJMXServer(
final String jdkImage,
final String javaOpts) {
this(jdkImage, javaOpts, DEFAULT_RMI_PORT, DEFAULT_CONTROL_PORT, DEFAULT_SUPERVISOR_PORT);
}
public MisbehavingJMXServer(
final int rmiPort,
final int controlPort,
final int supervisorPort) {
this(DEFAULT_JDK_IMAGE, "", rmiPort, controlPort, supervisorPort);
}

public MisbehavingJMXServer(
final String jdkImage,
final String javaOpts,
Expand Down Expand Up @@ -112,4 +101,66 @@ public void restoreNetwork() throws IOException {
public int getRMIPort() {
return this.rmiPort;
}

public static class Builder {

private String jdkImage;
private String javaOpts;
private int rmiPort;
private int controlPort;
private int supervisorPort;

public Builder() {
this.jdkImage = MisbehavingJMXServer.DEFAULT_JDK_IMAGE;
this.javaOpts = MisbehavingJMXServer.DEFAULT_MISBEHAVING_OPTS;
this.rmiPort = MisbehavingJMXServer.DEFAULT_RMI_PORT;
this.controlPort = MisbehavingJMXServer.DEFAULT_CONTROL_PORT;
this.supervisorPort = MisbehavingJMXServer.DEFAULT_SUPERVISOR_PORT;
}

public Builder withJDKImage(final String jdkImage) {
this.jdkImage = jdkImage;
return this;
}

public Builder withJDKImage(final JDKImage jdkImage) {
this.jdkImage = jdkImage.toString();
return this;
}

public Builder withJavaOpts(String javaOpts) {
this.javaOpts = javaOpts;
return this;
}

public Builder appendJavaOpts(String javaOpts) {
this.javaOpts = String.format("%s %s", javaOpts, this.javaOpts);
return this;
}

public Builder withRmiPort(int rmiPort) {
this.rmiPort = rmiPort;
return this;
}

public Builder withControlPort(int controlPort) {
this.controlPort = controlPort;
return this;
}

public Builder withSupervisorPort(int supervisorPort) {
this.supervisorPort = supervisorPort;
return this;
}

public MisbehavingJMXServer build() {
return new MisbehavingJMXServer(
this.jdkImage,
this.javaOpts,
this.rmiPort,
this.controlPort,
this.supervisorPort
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.datadog.jmxfetch.util.server.app.org.datadog.jmxfetch.util.server;

public enum JDKImage {
BASE("base"),
JDK_11("eclipse-temurin:11"),
JDK_17("eclipse-temurin:17"),
JDK_21("eclipse-temurin:21");

private final String image;

private JDKImage(final String image) {
this.image = image;
}

@Override
public String toString() {
return this.image;
}
}

0 comments on commit 5ca1719

Please sign in to comment.