-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AMLII-1148 - Adding Unit test to check GC metrics emitted by each type of Java garbage collector #500
AMLII-1148 - Adding Unit test to check GC metrics emitted by each type of Java garbage collector #500
Changes from 19 commits
ba48c6d
e352022
96a45bc
dc55bbb
f9a8680
a7fdff8
22a91de
ed430f9
d006fd0
50c6041
a1585b0
5ee4f54
57b4d53
bcea1a3
fc3a762
4224dc7
156f305
90835f9
2484b72
5ca1719
e8a8007
229b018
a902ae1
87a8750
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,182 @@ | ||||||
package org.datadog.jmxfetch; | ||||||
|
||||||
import static org.datadog.jmxfetch.util.MetricsAssert.assertDomainPresent; | ||||||
import static org.hamcrest.MatcherAssert.assertThat; | ||||||
import static org.hamcrest.Matchers.*; | ||||||
|
||||||
import java.io.IOException; | ||||||
import java.util.*; | ||||||
|
||||||
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.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()){ | ||||||
container.start(); | ||||||
final String ipAddress = container.getIp(); | ||||||
final String remoteJmxServiceUrl = String.format( | ||||||
"service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", ipAddress, container.getRMIPort()); | ||||||
final JMXServiceURL jmxUrl = new JMXServiceURL(remoteJmxServiceUrl); | ||||||
final JMXConnector conn = JMXConnectorFactory.connect(jmxUrl); | ||||||
final MBeanServerConnection mBeanServerConnection = conn.getMBeanServerConnection(); | ||||||
assertDomainPresent("java.lang", mBeanServerConnection); | ||||||
} | ||||||
} | ||||||
|
||||||
@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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will fix 🤦🏽♂️ |
||||||
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); | ||||||
} | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testDefaultNewGCMetricsUseParallelGC() throws IOException { | ||||||
try (final MisbehavingJMXServer server = new MisbehavingJMXServer( | ||||||
MisbehavingJMXServer.JDK_11, | ||||||
"-XX:+UseParallelGC -Xmx128M -Xms128M")) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If all of these need increased heap mem, maybe that should be the default? |
||||||
final List<Map<String, Object>> actualMetrics = startAngGetMetrics(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"); | ||||||
} | ||||||
} | ||||||
|
||||||
@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); | ||||||
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"); | ||||||
} | ||||||
} | ||||||
|
||||||
@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); | ||||||
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"); | ||||||
} | ||||||
} | ||||||
|
||||||
@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); | ||||||
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"); | ||||||
} | ||||||
} | ||||||
|
||||||
private List<Map<String, Object>> startAngGetMetrics(final MisbehavingJMXServer server, final boolean newGCMetrics) throws IOException { | ||||||
server.start(); | ||||||
this.initApplicationWithYamlLines( | ||||||
"init_config:", | ||||||
" is_jmx: true", | ||||||
" new_gc_metrics: " + newGCMetrics, | ||||||
"", | ||||||
"instances:", | ||||||
" - name: jmxint_container", | ||||||
" host: " + server.getIp(), | ||||||
" collect_default_jvm_metrics: true", | ||||||
" max_returned_metrics: 300000", | ||||||
" port: " + server.getRMIPort()); | ||||||
// Run one iteration first | ||||||
carlosroman marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
this.app.doIteration(); | ||||||
// And then pull get the metrics or else reporter does not have correct number of metrics | ||||||
((ConsoleReporter) appConfig.getReporter()).getMetrics(); | ||||||
|
||||||
// Actual iteration we care about | ||||||
this.app.doIteration(); | ||||||
return ((ConsoleReporter) appConfig.getReporter()).getMetrics(); | ||||||
} | ||||||
|
||||||
private static void assertGCMetric(final List<Map<String, Object>> actualMetrics, | ||||||
final String expectedMetric, | ||||||
final String gcGeneration, | ||||||
final String metricType) { | ||||||
MetricsAssert.assertMetric( | ||||||
expectedMetric, | ||||||
-1, | ||||||
-1, | ||||||
10.0, | ||||||
Collections.singletonList(String.format("name:%s", gcGeneration)), | ||||||
Arrays.asList("instance:jmxint_container", "jmx_domain:java.lang", "type:GarbageCollector"), | ||||||
5, | ||||||
metricType, | ||||||
actualMetrics); | ||||||
} | ||||||
|
||||||
private static void assertGCMetric(final List<Map<String, Object>> actualMetrics, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be implemented in terms of the above |
||||||
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(gcGenerations.size())); | ||||||
for (final String name : gcGenerations) { | ||||||
log.debug("Asserting for metric '{}'", name); | ||||||
scottopell marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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)); | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there any specific reason why you decided to leave in
SimpleApp
in addition to misbehaving-jmx-server?