From 6c430dc414d3e90e2f2c753cd810c81bd02fe31f Mon Sep 17 00:00:00 2001 From: Yann Mahe Date: Mon, 21 Sep 2015 18:08:25 -0400 Subject: [PATCH 1/4] Support 'float' and 'java.lang.Float' Support 'float' and 'java.lang.Float' attribute types as simple JMX attrbutes. --- src/main/java/org/datadog/jmxfetch/Instance.java | 2 +- .../org/datadog/jmxfetch/SimpleTestJavaApp.java | 5 ++++- .../datadog/jmxfetch/SimpleTestJavaAppMBean.java | 2 ++ src/test/java/org/datadog/jmxfetch/TestApp.java | 15 +++++++++------ 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 34261c376..0ac509d53 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -21,7 +21,7 @@ public class Instance { private final static Logger LOGGER = Logger.getLogger(Instance.class.getName()); private final static List SIMPLE_TYPES = Arrays.asList("long", - "java.lang.String", "int", "double", "java.lang.Double", "java.lang.Integer", "java.lang.Long", + "java.lang.String", "int", "float", "double", "java.lang.Double","java.lang.Float", "java.lang.Integer", "java.lang.Long", "java.util.concurrent.atomic.AtomicInteger", "java.util.concurrent.atomic.AtomicLong", "java.lang.Object", "java.lang.Boolean", "boolean", "java.lang.Number"); private final static List COMPOSED_TYPES = Arrays.asList("javax.management.openmbean.CompositeData", "java.util.HashMap"); diff --git a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java index ce20079d8..9e0a911a6 100644 --- a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java +++ b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java @@ -20,6 +20,7 @@ public class SimpleTestJavaApp implements SimpleTestJavaAppMBean { private final Long long42424242 = new Long(42424242); private final Integer int424242 = new Integer(424242); private final BigDecimal numberBig = new BigDecimal(123456788901234567890.0); + private final float someFloat = 123.4f; SimpleTestJavaApp() { hashmap.put("thisis0", 0); @@ -93,5 +94,7 @@ public Integer getInt424242() { return int424242; } - + public float getSomeFloat(){ + return someFloat; + } } diff --git a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java index 7e6cc852a..5302129c1 100644 --- a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java +++ b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java @@ -32,4 +32,6 @@ public interface SimpleTestJavaAppMBean { Integer getInt424242(); + float getSomeFloat(); + } diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index 7640a42d2..378edfe6b 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -83,8 +83,8 @@ public void testDomainInclude() throws Exception { app.doIteration(); LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); - // First filter 26 = 13 metrics from java.lang + 13 metrics implicitly defined - assertEquals(26, metrics.size()); + // First filter 27 = 13 metrics from java.lang + 13 metrics implicitly defined + assertEquals(27, metrics.size()); mbs.unregisterMBean(includeObjectName); } @@ -349,7 +349,7 @@ public void testApp() throws Exception { LinkedList> metrics = reporter.getMetrics(); - assertEquals(25, metrics.size()); // 25 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + the 7 gauges that is implicitly collected, see jmx.yaml in the test/resources folder + assertEquals(26, metrics.size()); // 26 = 13 metrics from java.lang + the 6 gauges we are explicitly collecting + the 7 gauges that is implicitly collected, see jmx.yaml in the test/resources folder // We test for the presence and the value of the metrics we want to collect boolean metric100Present = false; @@ -464,7 +464,7 @@ public void testApp() throws Exception { // We run a second collection. The counter should now be present app.doIteration(); metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); - assertEquals(27, metrics.size()); // 27 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 7 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder + assertEquals(28, metrics.size()); // 28 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 8 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder // We test for the same metrics but this time, the counter should be here metric100Present = false; @@ -585,7 +585,7 @@ public void testApp() throws Exception { app.doIteration(); metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); - assertEquals(metrics.size(), 27); // 27 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 7 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder + assertEquals(metrics.size(), 28); // 28 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 8 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder metric100Present = false; metric1000Present = false; @@ -694,7 +694,10 @@ public void testApp() throws Exception { assertEquals(tags.length, 5); assertEquals(value, new Double(13.37)); objectPresent = true; - + } else if (name.equals("jmx.org.datadog.jmxfetch.test.some_float")) { + assertEquals(tags.length, 5); + assertEquals(value, new Double(123.4f)); + objectPresent = true; } else { // Those are jvm metrics From f62214076cf486e62332a7d39ebf40b179c018b2 Mon Sep 17 00:00:00 2001 From: Yann Mahe Date: Wed, 30 Sep 2015 16:18:22 -0400 Subject: [PATCH 2/4] [test] `TestCommon` to refactor JUnit tests Introduce a `TestCommon` parent class to refactor JUnit tests. Features: * One-liner `registerMBean` method * One-liner `initApplication` nethod * Automatically unregister MBeans after execution of every test * More eye candies --- .../org/datadog/jmxfetch/CommonTestSetup.java | 25 -- .../datadog/jmxfetch/SimpleTestJavaApp.java | 24 +- .../jmxfetch/SimpleTestJavaAppMBean.java | 2 +- .../java/org/datadog/jmxfetch/TestApp.java | 228 +++++------------- .../java/org/datadog/jmxfetch/TestCommon.java | 116 +++++++++ .../datadog/jmxfetch/TestServiceChecks.java | 65 ++--- 6 files changed, 222 insertions(+), 238 deletions(-) delete mode 100644 src/test/java/org/datadog/jmxfetch/CommonTestSetup.java create mode 100644 src/test/java/org/datadog/jmxfetch/TestCommon.java diff --git a/src/test/java/org/datadog/jmxfetch/CommonTestSetup.java b/src/test/java/org/datadog/jmxfetch/CommonTestSetup.java deleted file mode 100644 index 3437de6f5..000000000 --- a/src/test/java/org/datadog/jmxfetch/CommonTestSetup.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.datadog.jmxfetch; - -import com.beust.jcommander.JCommander; -import org.apache.log4j.Level; -import org.datadog.jmxfetch.util.CustomLogger; - -public class CommonTestSetup { - public static void setupLogger() { - CustomLogger.setup(Level.toLevel("ALL"), "/tmp/jmxfetch_test.log"); - } - - public static App initApp(String yamlFileName, AppConfig appConfig){ - // We do a first collection - // We initialize the main app that will collect these metrics using JMX - String confdDirectory = Thread.currentThread().getContextClassLoader().getResource(yamlFileName).getPath(); - confdDirectory = new String(confdDirectory.substring(0, confdDirectory.length() - yamlFileName.length())); - String[] params = {"--reporter", "console", "-c", yamlFileName, "--conf_directory", confdDirectory, "collect"}; - new JCommander(appConfig, params); - - App app = new App(appConfig); - app.init(false); - - return app; - } -} diff --git a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java index 9e0a911a6..0b3412f62 100644 --- a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java +++ b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java @@ -7,20 +7,30 @@ public class SimpleTestJavaApp implements SimpleTestJavaAppMBean { + // Integers private final int shouldBe100 = 100; private final int shouldBe1000 = 1000; + private final Integer int424242 = new Integer(424242); + private final AtomicInteger atomic42 = new AtomicInteger(42); + private int shouldBeCounter = 0; + + // Floats & Long + private final float primitiveFloat = 123.4f; + private final Float instanceFloat = 567.8f; + private final AtomicLong atomic4242 = new AtomicLong(4242); + private final Long long42424242 = new Long(42424242); + + // String private final String shouldBeConverted = "ShouldBe5"; private final String shouldBeDefaulted = "DefaultMe"; + + // Others private final boolean shouldBeBoolean = true; private final HashMap hashmap = new HashMap(); - private final AtomicInteger atomic42 = new AtomicInteger(42); - private final AtomicLong atomic4242 = new AtomicLong(4242); private final Object object1337 = new Double(13.37); - private final Long long42424242 = new Long(42424242); - private final Integer int424242 = new Integer(424242); private final BigDecimal numberBig = new BigDecimal(123456788901234567890.0); - private final float someFloat = 123.4f; + SimpleTestJavaApp() { hashmap.put("thisis0", 0); @@ -94,7 +104,7 @@ public Integer getInt424242() { return int424242; } - public float getSomeFloat(){ - return someFloat; + public float getPrimitiveFloat(){ + return primitiveFloat; } } diff --git a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java index 5302129c1..a8355e591 100644 --- a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java +++ b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java @@ -32,6 +32,6 @@ public interface SimpleTestJavaAppMBean { Integer getInt424242(); - float getSomeFloat(); + float getPrimitiveFloat(); } diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index 378edfe6b..bf857787f 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -17,28 +17,17 @@ import static org.junit.Assert.*; -public class TestApp { - - @BeforeClass - public static void init() { - CommonTestSetup.setupLogger(); - } +public class TestApp extends TestCommon { @Test public void testBeanTags() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName objectName = new ObjectName("org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=CoolScope,host=localhost,component="); - SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, objectName); - - // Initializing application - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx_bean_tags.yaml", appConfig); + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=CoolScope,host=localhost,component="); + initApplication("jmx_bean_tags.yaml"); // Collecting metrics - app.doIteration(); - LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + LinkedList> metrics = getMetrics(); // 14 = 13 metrics from java.lang + 1 metric explicitly defined in the yaml config file assertEquals(14, metrics.size()); @@ -64,259 +53,180 @@ public void testBeanTags() throws Exception { // Empty values should also be added as tags, without the colon assertEquals(true, tagsSet.contains("component")); } - mbs.unregisterMBean(objectName); } @Test public void testDomainInclude() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName includeObjectName = new ObjectName("org.datadog.jmxfetch.includeme:type=AType"); - SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, includeObjectName); - - // Initializing application - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx_domain_include.yaml", appConfig); + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.includeme:type=AType"); + initApplication("jmx_domain_include.yaml"); // Collecting metrics - app.doIteration(); - LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + LinkedList> metrics = getMetrics(); // First filter 27 = 13 metrics from java.lang + 13 metrics implicitly defined assertEquals(27, metrics.size()); - - mbs.unregisterMBean(includeObjectName); } @Test public void testDomainExclude() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName includeMe = new ObjectName("org.datadog.jmxfetch.includeme:type=AType"); - ObjectName excludeMe = new ObjectName("org.datadog.jmxfetch.excludeme:type=AnotherType"); SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, includeMe); - mbs.registerMBean(testApp, excludeMe); + registerMBean(testApp, "org.datadog.jmxfetch.includeme:type=AType"); + registerMBean(testApp, "org.datadog.jmxfetch.excludeme:type=AnotherType"); // Initializing application - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx_domain_exclude.yaml", appConfig); + initApplication("jmx_domain_exclude.yaml"); // Collecting metrics - app.doIteration(); - LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + LinkedList> metrics = getMetrics(); // First filter 14 = 13 metrics from java.lang + 2 metrics explicitly define- 1 implicitly defined in the exclude section assertEquals(14, metrics.size()); - - mbs.unregisterMBean(includeMe); - mbs.unregisterMBean(excludeMe); } @Test public void testDomainRegex() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - - ObjectName includeObjectName1 = new ObjectName("org.datadog.jmxfetch.includeme:type=AType"); - ObjectName includeObjectName2 = new ObjectName("org.datadog.jmxfetch.includeme.too:type=AType"); - ObjectName excludeObjectName = new ObjectName("org.datadog.jmxfetch.includeme.not.me:type=AType"); SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, includeObjectName1); - mbs.registerMBean(testApp, includeObjectName2); - mbs.registerMBean(testApp, excludeObjectName); + registerMBean(testApp, "org.datadog.jmxfetch.includeme:type=AType"); + registerMBean(testApp, "org.datadog.jmxfetch.includeme.too:type=AType"); + registerMBean(testApp, "org.datadog.jmxfetch.includeme.not.me:type=AType"); // Initializing application - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx_domain_regex.yaml", appConfig); + initApplication("jmx_domain_regex.yaml"); // Collecting metrics - app.doIteration(); - LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + LinkedList> metrics = getMetrics(); // First filter 15 = 13 metrics from java.lang + 3 metrics explicitly defined - 1 implicitly defined in exclude section assertEquals(15, metrics.size()); - - mbs.unregisterMBean(includeObjectName1); - mbs.unregisterMBean(includeObjectName2); - mbs.unregisterMBean(excludeObjectName); } @Test public void testParameterMatch() throws Exception { // Do not match beans which do not contain types specified in the conf - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName matchParam = new ObjectName("org.datadog.jmxfetch.test:param=AParameter"); - SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, matchParam); + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:param=AParameter"); // Initializing application - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx_list_params_include.yaml", appConfig); + initApplication("jmx_list_params_include.yaml"); // Collecting metrics - app.doIteration(); - LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + LinkedList> metrics = getMetrics(); // 13 default metrics from java.lang assertEquals(13, metrics.size()); - - mbs.unregisterMBean(matchParam); - } @Test public void testListParamsInclude() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName includeObjectName = new ObjectName("org.datadog.jmxfetch.test:type=RightType"); - SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, includeObjectName); + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:type=RightType"); // Initializing application - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx_list_params_include.yaml", appConfig); + initApplication("jmx_list_params_include.yaml"); // Collecting metrics - app.doIteration(); - LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + LinkedList> metrics = getMetrics(); // First filter 14 = 13 metrics from java.lang + 1 metrics explicitly defined assertEquals(14, metrics.size()); - - mbs.unregisterMBean(includeObjectName); } @Test public void testListParamsExclude() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName includeObjectName = new ObjectName("org.datadog.jmxfetch.test:type=RightType"); - SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, includeObjectName); + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:type=RightType"); // Initializing application - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx_list_params_exclude.yaml", appConfig); + initApplication("jmx_list_params_exclude.yaml"); // Collecting metrics - app.doIteration(); - LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + LinkedList> metrics = getMetrics(); // First filter 13 = 13 metrics from java.lang + 2 metrics explicitly defined - 2 explicitly defined assertEquals(13, metrics.size()); - - mbs.unregisterMBean(includeObjectName); } @Test public void testListBeansInclude() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName includeMe = new ObjectName("org.datadog.jmxfetch.test:type=IncludeMe"); - SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, includeMe); + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:type=IncludeMe"); // Initializing application - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx_list_beans_include.yaml", appConfig); + initApplication("jmx_list_beans_include.yaml"); // Collecting metrics - app.doIteration(); - LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + LinkedList> metrics = getMetrics(); // First filter 14 = 13 metrics from java.lang + 1 metrics explicitly defined assertEquals(14, metrics.size()); - - mbs.unregisterMBean(includeMe); } @Test public void testListBeansRegexInclude() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName includeMe = new ObjectName("org.datadog.jmxfetch.test:type=IncludeMe"); - ObjectName includeMeToo = new ObjectName("org.datadog.jmxfetch.test:type=IncludeMeToo"); - ObjectName notIncludeMe = new ObjectName("org.datadog.jmxfetch.test:type=RightType"); SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, includeMe); - mbs.registerMBean(testApp, includeMeToo); - mbs.registerMBean(testApp, notIncludeMe); + registerMBean(testApp, "org.datadog.jmxfetch.test:type=IncludeMe"); + registerMBean(testApp, "org.datadog.jmxfetch.test:type=IncludeMeToo"); + registerMBean(testApp, "org.datadog.jmxfetch.test:type=RightType"); // Initializing application - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx_list_beans_regex_include.yaml", appConfig); + initApplication("jmx_list_beans_regex_include.yaml"); // Collecting metrics - app.doIteration(); - LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + LinkedList> metrics = getMetrics(); // First filter 15 = 13 metrics from java.lang + 2 metrics explicitly defined assertEquals(15, metrics.size()); - - mbs.unregisterMBean(includeMe); - mbs.unregisterMBean(includeMeToo); - mbs.unregisterMBean(notIncludeMe); } @Test public void testListBeansRegexExclude() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName includeMe = new ObjectName("org.datadog.jmxfetch.test:type=IncludeMe"); - ObjectName excludeMe = new ObjectName("org.datadog.jmxfetch.test:type=ExcludeMe,scope=InScope"); - ObjectName excludeMeToo = new ObjectName("org.datadog.jmxfetch.test:scope=OutOfScope"); SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, includeMe); - mbs.registerMBean(testApp, excludeMe); - mbs.registerMBean(testApp, excludeMeToo); + registerMBean(testApp, "org.datadog.jmxfetch.test:type=IncludeMe"); + registerMBean(testApp, "org.datadog.jmxfetch.test:type=ExcludeMe,scope=InScope"); + registerMBean(testApp, "org.datadog.jmxfetch.test:scope=OutOfScope"); // Initializing application - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx_list_beans_regex_exclude.yaml", appConfig); + initApplication("jmx_list_beans_regex_exclude.yaml"); // Collecting metrics - app.doIteration(); - LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + LinkedList> metrics = getMetrics(); // First filter 14 = 13 metrics from java.lang + 1 metrics explicitly defined assertEquals(14, metrics.size()); - - mbs.unregisterMBean(includeMe); - mbs.unregisterMBean(excludeMe); - mbs.unregisterMBean(excludeMeToo); } @Test public void testListBeansExclude() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName includeMe = new ObjectName("org.datadog.jmxfetch.test:type=IncludeMe"); - ObjectName excludeMe = new ObjectName("org.datadog.jmxfetch.test:type=ExcludeMe"); - ObjectName excludeMeToo = new ObjectName("org.datadog.jmxfetch.test:type=ExcludeMeToo"); SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, includeMe); - mbs.registerMBean(testApp, excludeMe); - mbs.registerMBean(testApp, excludeMeToo); + registerMBean(testApp, "org.datadog.jmxfetch.test:type=IncludeMe"); + registerMBean(testApp, "org.datadog.jmxfetch.test:type=ExcludeMe"); + registerMBean(testApp, "org.datadog.jmxfetch.test:type=ExcludeMeToo"); // Initializing application - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx_list_beans_exclude.yaml", appConfig); + initApplication("jmx_list_beans_exclude.yaml"); // Collecting metrics - app.doIteration(); - LinkedList> metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + LinkedList> metrics = getMetrics(); // First filter 14 = 13 metrics from java.lang + 1 metrics explicitly defined assertEquals(14, metrics.size()); - - mbs.unregisterMBean(includeMe); - mbs.unregisterMBean(excludeMe); - mbs.unregisterMBean(excludeMeToo); } - @Test public void testExitWatcher() throws Exception { // Test the ExitWatcher logic @@ -332,24 +242,22 @@ public void testExitWatcher() throws Exception { assertFalse(exitWatcher.shouldExit()); } + /** + * FIXME: Split this test in multiple sub-tests. + */ @Test public void testApp() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName objectName = new ObjectName("org.datadog.jmxfetch.test:type=SimpleTestJavaApp"); SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, objectName); + registerMBean( testApp, "org.datadog.jmxfetch.test:type=SimpleTestJavaApp"); // We do a first collection - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx.yaml", appConfig); - - app.doIteration(); - ConsoleReporter reporter = ((ConsoleReporter) appConfig.getReporter()); + initApplication("jmx.yaml"); - LinkedList> metrics = reporter.getMetrics(); + run(); + LinkedList> metrics = getMetrics(); - assertEquals(26, metrics.size()); // 26 = 13 metrics from java.lang + the 6 gauges we are explicitly collecting + the 7 gauges that is implicitly collected, see jmx.yaml in the test/resources folder + assertEquals(26, metrics.size()); // 26 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + the 8 gauges that is implicitly collected, see jmx.yaml in the test/resources folder // We test for the presence and the value of the metrics we want to collect boolean metric100Present = false; @@ -462,8 +370,8 @@ public void testApp() throws Exception { assertTrue(integerPresent); // We run a second collection. The counter should now be present - app.doIteration(); - metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + metrics = getMetrics(); assertEquals(28, metrics.size()); // 28 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 8 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder // We test for the same metrics but this time, the counter should be here @@ -583,8 +491,8 @@ public void testApp() throws Exception { testApp.incrementCounter(5); testApp.incrementHashMapCounter(5); - app.doIteration(); - metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + run(); + metrics = getMetrics(); assertEquals(metrics.size(), 28); // 28 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 8 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder metric100Present = false; @@ -694,7 +602,7 @@ public void testApp() throws Exception { assertEquals(tags.length, 5); assertEquals(value, new Double(13.37)); objectPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.some_float")) { + } else if (name.equals("jmx.org.datadog.jmxfetch.test.primitive_float")) { assertEquals(tags.length, 5); assertEquals(value, new Double(123.4f)); objectPresent = true; @@ -725,7 +633,5 @@ public void testApp() throws Exception { for (int i : jvm_metrics.values()) { assertEquals(0, i); } - // Unregistering MBean - mbs.unregisterMBean(objectName); } } diff --git a/src/test/java/org/datadog/jmxfetch/TestCommon.java b/src/test/java/org/datadog/jmxfetch/TestCommon.java new file mode 100644 index 000000000..08603b805 --- /dev/null +++ b/src/test/java/org/datadog/jmxfetch/TestCommon.java @@ -0,0 +1,116 @@ +package org.datadog.jmxfetch; + +import org.datadog.jmxfetch.reporter.ConsoleReporter; +import org.datadog.jmxfetch.reporter.Reporter; +import org.datadog.jmxfetch.util.CustomLogger; +import org.junit.After; +import org.junit.BeforeClass; + +import com.beust.jcommander.JCommander; + +import org.apache.log4j.Level; + +import java.lang.management.ManagementFactory; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; + +import javax.management.InstanceAlreadyExistsException; +import javax.management.InstanceNotFoundException; +import javax.management.MalformedObjectNameException; +import javax.management.MBeanRegistrationException; +import javax.management.MBeanServer; +import javax.management.NotCompliantMBeanException; +import javax.management.ObjectName; + + +public class TestCommon { + AppConfig appConfig = new AppConfig(); + App app; + MBeanServer mbs; + ArrayList objectNames = new ArrayList(); + + /** + * Setup logger. + */ + @BeforeClass + public static void init() throws Exception { + CustomLogger.setup(Level.toLevel("ALL"), "/tmp/jmxfetch_test.log"); + } + + /** + * Register a MBean with the given name, and application attributes. + * @throws NotCompliantMBeanException + * @throws MBeanRegistrationException + * @throws InstanceAlreadyExistsException + * @throws MalformedObjectNameException + */ + protected void registerMBean(Object application, String objectStringName) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, MalformedObjectNameException { + mbs = (mbs == null)? ManagementFactory.getPlatformMBeanServer() : mbs; + ObjectName objectName = new ObjectName(objectStringName); + objectNames.add(objectName); + mbs.registerMBean(application, objectName); + } + + /** + * Unregister MBeans. + * Note: executed after execution of every test. + * @throws InstanceNotFoundException + * @throws MBeanRegistrationException + */ + @After + public void unregisterMBean() throws MBeanRegistrationException, InstanceNotFoundException{ + if (mbs != null) { + for (ObjectName objectName: objectNames ) { + mbs.unregisterMBean(objectName); + } + } + } + + /** + * Init JMXFetch with the given YAML configuration file. + */ + protected void initApplication(String yamlFileName){ + // We do a first collection + // We initialize the main app that will collect these metrics using JMX + String confdDirectory = Thread.currentThread().getContextClassLoader().getResource(yamlFileName).getPath(); + confdDirectory = new String(confdDirectory.substring(0, confdDirectory.length() - yamlFileName.length())); + String[] params = {"--reporter", "console", "-c", yamlFileName, "--conf_directory", confdDirectory, "collect"}; + new JCommander(appConfig, params); + + app = new App(appConfig); + app.init(false); + } + + /** + * Run a JMXFetch iteration. + */ + protected void run(){ + if (app != null) { + app.doIteration(); + } + } + + /** + * Return JMXFetch reporter. + */ + protected Reporter getReporter(){ + return appConfig.getReporter(); + } + + /** + * Return the metrics collected by JMXFetch. + */ + protected LinkedList> getMetrics(){ + return ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + } + + + /** + * Return the service checks collected by JMXFetch. + */ + protected LinkedList> getServiceChecks(){ + return ((ConsoleReporter) appConfig.getReporter()).getServiceChecks(); + } + +} diff --git a/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java b/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java index ef707bde7..f9bf6c4f3 100644 --- a/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java +++ b/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java @@ -14,29 +14,21 @@ import static org.junit.Assert.*; -public class TestServiceChecks { - - @BeforeClass - public static void init() { - CommonTestSetup.setupLogger(); - } +public class TestServiceChecks extends TestCommon { @Test public void testServiceCheckOK() throws Exception { // We expose a few metrics through JMX - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName objectName = new ObjectName("org.datadog.jmxfetch.test:type=ServiceCheckTest"); - SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, objectName); + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:type=ServiceCheckTest"); // We do a first collection - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx.yaml", appConfig); + initApplication("jmx.yaml"); + + run(); + LinkedList> metrics = getMetrics(); - app.doIteration(); - ConsoleReporter reporter = ((ConsoleReporter) appConfig.getReporter()); // Test that an OK service check status is sent - LinkedList> serviceChecks = reporter.getServiceChecks(); + LinkedList> serviceChecks = getServiceChecks(); assertEquals(1, serviceChecks.size()); HashMap sc = serviceChecks.getFirst(); @@ -53,32 +45,26 @@ public void testServiceCheckOK() throws Exception { assertEquals(Status.STATUS_OK, scStatus); assertEquals(scTags.length, 1); assertTrue(Arrays.asList(scTags).contains("instance:jmx_test_instance")); - mbs.unregisterMBean(objectName); } @Test public void testServiceCheckWarning() throws Exception { - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName objectName = new ObjectName("org.datadog.jmxfetch.test:type=ServiceCheckTest"); - // Test application SimpleTestJavaApp testApp = new SimpleTestJavaApp(); // Populate it with a lot of metrics (>350) ! testApp.populateHashMap(400); // Exposing a few metrics through JMX - mbs.registerMBean(testApp, objectName); + registerMBean(testApp, "org.datadog.jmxfetch.test:type=ServiceCheckTest"); - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("too_many_metrics.yaml", appConfig); + initApplication("too_many_metrics.yaml"); // JMX configuration should return > 350 metrics - app.doIteration(); - ConsoleReporter reporter = ((ConsoleReporter) appConfig.getReporter()); + run(); // Test that an WARNING service check status is sent - LinkedList> serviceChecks = reporter.getServiceChecks(); - LinkedList> metrics = reporter.getMetrics(); + LinkedList> serviceChecks = getServiceChecks(); + LinkedList> metrics = getMetrics(); assertTrue(metrics.size() >= 350 ); assertEquals(1, serviceChecks.size()); @@ -99,24 +85,16 @@ public void testServiceCheckWarning() throws Exception { assertEquals(Status.STATUS_OK, scStatus); assertEquals(scTags.length, 1); assertTrue(Arrays.asList(scTags).contains("instance:jmx_test_instance")); - mbs.unregisterMBean(objectName); } @Test public void testServiceCheckCRITICAL() throws Exception { // Test that a non-running service sends a critical service check - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName objectName = new ObjectName("org.datadog.jmxfetch.test_non_running:type=ServiceCheckTest2"); - SimpleTestJavaApp testApp = new SimpleTestJavaApp(); - mbs.registerMBean(testApp, objectName); - - AppConfig appConfig = new AppConfig(); - - App app = CommonTestSetup.initApp("non_running_process.yaml", appConfig); - ConsoleReporter reporter = ((ConsoleReporter) appConfig.getReporter()); + registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test_non_running:type=ServiceCheckTest2"); + initApplication("non_running_process.yaml"); // Test that a CRITICAL service check status is sent on initialization - LinkedList> serviceChecks = reporter.getServiceChecks(); + LinkedList> serviceChecks = getServiceChecks(); assertEquals(1, serviceChecks.size()); HashMap sc = serviceChecks.getFirst(); @@ -138,9 +116,9 @@ public void testServiceCheckCRITICAL() throws Exception { // Test that a CRITICAL service check status is sent on iteration - app.doIteration(); + run(); - serviceChecks = reporter.getServiceChecks(); + serviceChecks = getServiceChecks(); assertEquals(1, serviceChecks.size()); sc = serviceChecks.getFirst(); @@ -160,14 +138,13 @@ public void testServiceCheckCRITICAL() throws Exception { assertEquals(scTags.length, 1); assertTrue(Arrays.asList(scTags).contains("instance:jmx_test_instance")); - mbs.unregisterMBean(objectName); } @Test public void testServiceCheckCounter() throws Exception { - AppConfig appConfig = new AppConfig(); - App app = CommonTestSetup.initApp("jmx.yaml", appConfig); - Reporter repo = appConfig.getReporter(); + initApplication("jmx.yaml"); + + Reporter repo = getReporter(); // Let's check that the counter is null assertEquals(0, repo.getServiceCheckCount("jmx")); @@ -182,7 +159,7 @@ public void testServiceCheckCounter() throws Exception { // Let's check that each service check counter is reset after each app // app iteration - app.doIteration(); + run(); assertEquals(0, repo.getServiceCheckCount("jmx")); } From 5cf184b1770bf592e05cc689bd592146eb718517 Mon Sep 17 00:00:00 2001 From: Yann Mahe Date: Tue, 13 Oct 2015 16:27:45 +0200 Subject: [PATCH 3/4] [test] `assertMetric` and `assertCoverage` methods Add two new methods to `TestCommon` test class to refactor JUnit tests. * `assertMetric`: assert that a specific metric was collected * `assertCoverage`: assert that all -excluding JVM related- metrics were tested. Refactor `TestApp` tests. --- .../java/org/datadog/jmxfetch/TestApp.java | 418 +++--------------- .../java/org/datadog/jmxfetch/TestCommon.java | 157 ++++++- .../datadog/jmxfetch/TestServiceChecks.java | 1 + 3 files changed, 212 insertions(+), 364 deletions(-) diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index bf857787f..261778596 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -9,6 +9,7 @@ import java.io.File; import java.lang.management.ManagementFactory; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; @@ -260,114 +261,27 @@ public void testApp() throws Exception { assertEquals(26, metrics.size()); // 26 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + the 8 gauges that is implicitly collected, see jmx.yaml in the test/resources folder // We test for the presence and the value of the metrics we want to collect - boolean metric100Present = false; - boolean atomicIntPresent = false; - boolean atomicLongPresent = false; - boolean objectPresent = false; - boolean metric1000Present = false; - boolean convertedPresent = false; - boolean booleanPresent = false; - boolean defaultPresent = false; - boolean numberPresent = false; - boolean integerPresent = false; - boolean longPresent = false; - boolean counterAbsent = true; - boolean subattr0Present = false; - boolean subattrCounterAbsent = true; - - for (HashMap m : metrics) { - assertNotNull(m.get("name")); - assertNotNull(m.get("value")); - assertNotNull(m.get("tags")); - - String name = (String) (m.get("name")); - Double value = (Double) (m.get("value")); - String[] tags = (String[]) (m.get("tags")); - - // All metrics should be tagged with "instance:jmx_test_instance" - assertTrue(Arrays.asList(tags).contains("instance:jmx_test_instance")); - assertTrue(Arrays.asList(tags).contains("env:stage")); - assertTrue(Arrays.asList(tags).contains("newTag:test")); - - assertNotNull(value); - - if (name.equals("this.is.100")) { - assertEquals(8, tags.length); - assertEquals(new Double(100.0), value); - metric100Present = true; - - assertTrue(Arrays.asList(tags).contains("foo")); - assertTrue(Arrays.asList(tags).contains("gorch")); - assertTrue(Arrays.asList(tags).contains("bar:baz")); - } else if (name.equals("jmx.org.datadog.jmxfetch.test.number_big")) { - assertEquals(5, tags.length); - assertEquals(new Double(1.2345678890123457E20), value); - numberPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.long42424242")) { - assertEquals(5, tags.length); - assertEquals(new Double(4.2424242E7), value); - longPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.int424242")) { - assertEquals(5, tags.length); - assertEquals(new Double(424242.0), value); - integerPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.should_be1000")) { - assertEquals(5, tags.length); - assertEquals(new Double(1000.0), value); - metric1000Present = true; - } else if (name.equals("test.converted")) { - assertEquals(5, tags.length); - assertEquals(new Double(5.0), value); - convertedPresent = true; - } else if (name.equals("test.boolean")) { - assertEquals(5, tags.length); - assertEquals(new Double(1.0), value); - booleanPresent = true; - } else if (name.equals("test.defaulted")) { - assertEquals(5, tags.length); - assertEquals(new Double(32.0), value); - defaultPresent = true; - } else if (m.get("name").equals("test.counter")) { - counterAbsent = false; - - } else if (name.equals("subattr.this.is.0")) { - assertEquals(5, tags.length); - assertEquals(new Double(0.0), value); - subattr0Present = true; - - - - } else if (name.equals("subattr.counter")) { - subattrCounterAbsent = false; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic42")) { - assertEquals(5, tags.length); - assertEquals(new Double(42.0), value); - atomicIntPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic4242")) { - assertEquals(5, tags.length); - assertEquals(new Double(4242.0), value); - atomicLongPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) { - assertEquals(5, tags.length); - assertEquals(new Double(13.37), value); - objectPresent = true; - } - } - - assertTrue(metric100Present); - assertTrue(metric1000Present); - assertTrue(booleanPresent); - assertTrue(convertedPresent); - assertTrue(defaultPresent); - assertTrue(counterAbsent); - assertTrue(subattr0Present); - assertTrue(subattrCounterAbsent); - assertTrue(atomicIntPresent); - assertTrue(atomicLongPresent); - assertTrue(objectPresent); - assertTrue(numberPresent); - assertTrue(longPresent); - assertTrue(integerPresent); + ArrayList commonTags = new ArrayList() {{ + add("instance:jmx_test_instance"); + add("env:stage"); + add("newTag:test"); + }}; + + assertMetric("this.is.100", 100.0, commonTags, new ArrayList() {{add("foo");add("gorch");add("bar:baz");}} , 8); + assertMetric("jmx.org.datadog.jmxfetch.test.number_big", 1.2345678890123457E20, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.long42424242",4.2424242E7, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.int424242", 424242.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.should_be1000", 1000.0, commonTags, 5); + assertMetric("test.converted", 5.0, commonTags, 5); + assertMetric("test.boolean", 1.0, commonTags, 5); + assertMetric("test.defaulted", 32.0, commonTags, 5); + assertMetric("subattr.this.is.0", 0.0, commonTags, 5); + assertMetric("subattr.this.is.0", 0.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.atomic42", 42.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.atomic4242", 4242.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.object1337", 13.37, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.primitive_float", 123.4f, commonTags, 5); + assertCoverage(); // We run a second collection. The counter should now be present run(); @@ -375,116 +289,26 @@ public void testApp() throws Exception { assertEquals(28, metrics.size()); // 28 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 8 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder // We test for the same metrics but this time, the counter should be here - metric100Present = false; - atomicIntPresent = false; - atomicLongPresent = false; - objectPresent = false; - metric1000Present = false; - booleanPresent = false; - convertedPresent = false; - defaultPresent = false; - counterAbsent = true; - numberPresent = false; - integerPresent = false; - longPresent = false; - - for (HashMap m : metrics) { - assertNotNull(m.get("name")); - assertNotNull(m.get("value")); - assertNotNull(m.get("tags")); - - String name = (String) (m.get("name")); - Double value = (Double) (m.get("value")); - String[] tags = (String[]) (m.get("tags")); - - // All metrics should be tagged with "instance:jmx_test_instance" - assertTrue(Arrays.asList(tags).contains("instance:jmx_test_instance")); - assertTrue(Arrays.asList(tags).contains("env:stage")); - assertTrue(Arrays.asList(tags).contains("newTag:test")); - - if (name.equals("this.is.100")) { - assertEquals(tags.length, 8); - assertEquals(value, new Double(100.0)); - metric100Present = true; - - } else if (name.equals("jmx.org.datadog.jmxfetch.test.should_be1000")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(1000.0)); - metric1000Present = true; - - } else if (name.equals("jmx.org.datadog.jmxfetch.test.number_big")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(1.2345678890123457E20)); - numberPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.long42424242")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(4.2424242E7)); - longPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.int424242")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(424242.0)); - integerPresent = true; - - - } else if (name.equals("test.counter")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(0.0)); // We didn't increment the counter, hence a value of 0.0 is what we want - counterAbsent = false; - - } else if (name.equals("subattr.this.is.0")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(0.0)); - subattr0Present = true; - - } else if (name.equals("subattr.counter")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(0.0)); // We didn't increment the counter, hence a value of 0.0 is what we want - subattrCounterAbsent = false; - - } else if (name.equals("test.boolean")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(1.0)); - booleanPresent = true; - - } else if (name.equals("test.converted")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(5.0)); - convertedPresent = true; - - } else if (name.equals("test.defaulted")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(32.0)); // We didn't increment the counter, hence a value of 0.0 is what we want - defaultPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic42")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(42.0)); - atomicIntPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic4242")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(4242.0)); - atomicLongPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(13.37)); - objectPresent = true; - } - } - - assertTrue(metric100Present); - assertTrue(metric1000Present); - assertTrue(booleanPresent); - assertTrue(convertedPresent); - assertTrue(defaultPresent); - assertFalse(counterAbsent); - assertTrue(subattr0Present); - assertFalse(subattrCounterAbsent); - assertTrue(atomicIntPresent); - assertTrue(atomicLongPresent); - assertTrue(objectPresent); - assertTrue(numberPresent); - assertTrue(longPresent); - assertTrue(integerPresent); - + // Previous metrics + assertMetric("this.is.100", 100.0, commonTags, 8); + assertMetric("jmx.org.datadog.jmxfetch.test.number_big", 1.2345678890123457E20, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.long42424242",4.2424242E7, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.int424242", 424242.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.should_be1000", 1000.0, commonTags, 5); + assertMetric("test.converted", 5.0, commonTags, 5); + assertMetric("test.boolean", 1.0, commonTags, 5); + assertMetric("test.defaulted", 32.0, commonTags, 5); + assertMetric("subattr.this.is.0", 0.0, commonTags, 5); + assertMetric("subattr.this.is.0", 0.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.atomic42", 42.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.atomic4242", 4242.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.object1337", 13.37, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.primitive_float", 123.4f, commonTags, 5); + + // Counters + assertMetric("subattr.counter", 0.0, commonTags, 5); + assertMetric("test.counter", 0.0, commonTags, 5); + assertCoverage(); // We run a 3rd collection but this time we increment the counter and we sleep Thread.sleep(5000); @@ -495,143 +319,25 @@ public void testApp() throws Exception { metrics = getMetrics(); assertEquals(metrics.size(), 28); // 28 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 8 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder - metric100Present = false; - metric1000Present = false; - atomicIntPresent = false; - atomicLongPresent = false; - objectPresent = false; - booleanPresent = false; - convertedPresent = false; - defaultPresent = false; - numberPresent = false; - integerPresent = false; - longPresent = false; - - - counterAbsent = true; - HashMap jvm_metrics = new HashMap(); - jvm_metrics.put("jvm.gc.cms.count", 2); - jvm_metrics.put("jvm.gc.parnew.time", 2); - jvm_metrics.put("jvm.heap_memory", 1); - jvm_metrics.put("jvm.heap_memory_committed", 1); - jvm_metrics.put("jvm.heap_memory_init", 1); - jvm_metrics.put("jvm.heap_memory_max", 1); - jvm_metrics.put("jvm.non_heap_memory", 1); - jvm_metrics.put("jvm.non_heap_memory_committed", 1); - jvm_metrics.put("jvm.non_heap_memory_init", 1); - jvm_metrics.put("jvm.non_heap_memory_max", 1); - - jvm_metrics.put("jvm.thread_count", 1); - - for (HashMap m : metrics) { - assertNotNull(m.get("name")); - assertNotNull(m.get("value")); - assertNotNull(m.get("tags")); - - String name = (String) (m.get("name")); - Double value = (Double) (m.get("value")); - String[] tags = (String[]) (m.get("tags")); - - // All metrics should be tagged with "instance:jmx_test_instance" - assertTrue(Arrays.asList(tags).contains("instance:jmx_test_instance")); - assertTrue(Arrays.asList(tags).contains("env:stage")); - assertTrue(Arrays.asList(tags).contains("newTag:test")); - - if (name.equals("this.is.100")) { - assertEquals(tags.length, 8); - assertEquals(value, new Double(100.0)); - metric100Present = true; - - } else if (name.equals("jmx.org.datadog.jmxfetch.test.number_big")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(1.2345678890123457E20)); - numberPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.long42424242")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(4.2424242E7)); - longPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.int424242")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(424242.0)); - integerPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.should_be1000")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(1000.0)); - metric1000Present = true; - } else if (name.equals("test.counter")) { - assertEquals(tags.length, 5); - // The value should be a bit less than 1.0, as we incremented the counter by 5 and we slept for 5 seconds - assertTrue(value < 1.00); - assertTrue(value > 0.98); - counterAbsent = false; - } else if (name.equals("subattr.this.is.0")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(0.0)); - subattr0Present = true; - - } else if (name.equals("test.boolean")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(1.0)); - booleanPresent = true; - - } else if (name.equals("test.converted")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(5.0)); - convertedPresent = true; - - } else if (name.equals("test.defaulted")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(32.0)); - defaultPresent = true; - - } else if (name.equals("subattr.counter")) { - assertEquals(tags.length, 5); - // The value should be a bit less than 1.0, as we incremented the counter by 5 and we slept for 5 seconds - assertTrue(value < 1.00); - assertTrue(value > 0.98); - subattrCounterAbsent = false; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic42")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(42.0)); - atomicIntPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic4242")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(4242.0)); - atomicLongPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(13.37)); - objectPresent = true; - } else if (name.equals("jmx.org.datadog.jmxfetch.test.primitive_float")) { - assertEquals(tags.length, 5); - assertEquals(value, new Double(123.4f)); - objectPresent = true; - - } else { - // Those are jvm metrics - assertTrue(jvm_metrics.containsKey(name)); - jvm_metrics.put(name, jvm_metrics.get(name) - 1); - } - } - - assertTrue(metric100Present); - assertTrue(metric1000Present); - assertTrue(booleanPresent); - assertTrue(convertedPresent); - assertTrue(defaultPresent); - assertTrue(metric1000Present); - assertFalse(counterAbsent); - assertTrue(subattr0Present); - assertFalse(subattrCounterAbsent); - assertTrue(atomicIntPresent); - assertTrue(atomicLongPresent); - assertTrue(objectPresent); - assertTrue(numberPresent); - assertTrue(longPresent); - assertTrue(integerPresent); - - for (int i : jvm_metrics.values()) { - assertEquals(0, i); - } + // Previous metrics + assertMetric("this.is.100", 100.0, commonTags, 8); + assertMetric("jmx.org.datadog.jmxfetch.test.number_big", 1.2345678890123457E20, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.long42424242",4.2424242E7, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.int424242", 424242.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.should_be1000", 1000.0, commonTags, 5); + assertMetric("test.converted", 5.0, commonTags, 5); + assertMetric("test.boolean", 1.0, commonTags, 5); + assertMetric("test.defaulted", 32.0, commonTags, 5); + assertMetric("subattr.this.is.0", 0.0, commonTags, 5); + assertMetric("subattr.this.is.0", 0.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.atomic42", 42.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.atomic4242", 4242.0, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.object1337", 13.37, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.primitive_float", 123.4f, commonTags, 5); + + // Counter + assertMetric("subattr.counter", 0.98, 1, commonTags, 5); + assertMetric("test.counter", 0.98, 1, commonTags, 5); + assertCoverage(); } } diff --git a/src/test/java/org/datadog/jmxfetch/TestCommon.java b/src/test/java/org/datadog/jmxfetch/TestCommon.java index 08603b805..233605c77 100644 --- a/src/test/java/org/datadog/jmxfetch/TestCommon.java +++ b/src/test/java/org/datadog/jmxfetch/TestCommon.java @@ -3,17 +3,15 @@ import org.datadog.jmxfetch.reporter.ConsoleReporter; import org.datadog.jmxfetch.reporter.Reporter; import org.datadog.jmxfetch.util.CustomLogger; -import org.junit.After; -import org.junit.BeforeClass; - -import com.beust.jcommander.JCommander; - -import org.apache.log4j.Level; import java.lang.management.ManagementFactory; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; +import java.util.List; +import java.util.Set; import javax.management.InstanceAlreadyExistsException; import javax.management.InstanceNotFoundException; @@ -22,6 +20,13 @@ import javax.management.MBeanServer; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; +import org.junit.After; +import org.junit.BeforeClass; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import com.beust.jcommander.JCommander; +import org.apache.log4j.Level; public class TestCommon { @@ -29,6 +34,8 @@ public class TestCommon { App app; MBeanServer mbs; ArrayList objectNames = new ArrayList(); + LinkedList> metrics; + LinkedList> serviceChecks; /** * Setup logger. @@ -88,6 +95,7 @@ protected void initApplication(String yamlFileName){ protected void run(){ if (app != null) { app.doIteration(); + metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); } } @@ -102,10 +110,9 @@ protected Reporter getReporter(){ * Return the metrics collected by JMXFetch. */ protected LinkedList> getMetrics(){ - return ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + return metrics; } - /** * Return the service checks collected by JMXFetch. */ @@ -113,4 +120,138 @@ protected LinkedList> getServiceChecks(){ return ((ConsoleReporter) appConfig.getReporter()).getServiceChecks(); } + /** + * Assert that a specific metric was collected. + * Brand the metric so we can easily know which metric have/have not been tested + * + * @param name metric name + * + * @param value metric value + * + * @param lowerBound lower bound metric value + * + * @param upperBound upper bound metric value + * + * @param commonTags metric tags inherited from the instance configuration + * + * @param additionalTags metric tags inherited from the bean properties + * + * @param countTags number of metric tags + * + * @return fail if the metric was not found + */ + public void assertMetric(String name, Number value, Number lowerBound, Number upperBound, ArrayList commonTags, ArrayList additionalTags, int countTags){ + List tags = new ArrayList(commonTags); + tags.addAll(additionalTags); + + for (HashMap m: metrics) { + String mName = (String) (m.get("name")); + Double mValue = (Double) (m.get("value")); + Set mTags = new HashSet(Arrays.asList((String[]) (m.get("tags")))); + + if (mName.equals(name)) { + + if (lowerBound.equals(-1) && upperBound.equals(-1)){ + assertEquals((Double)value.doubleValue(), mValue); + }else{ + assertTrue(mValue > (Double)lowerBound.doubleValue()); + assertTrue(mValue < (Double)upperBound.doubleValue()); + } + + if (countTags != -1) { + assertEquals(countTags, mTags.size()); + } + for (String t: tags) { + assertTrue(mTags.contains(t)); + } + // Brand the metric + m.put("tested", true); + + return; + } + } + fail("Metric assertion failed (name: "+name+", value: "+value+", tags: "+tags+", #tags: "+countTags+")."); + } + + public void assertMetric(String name, Number value, ArrayList commonTags, ArrayList additionalTags, int countTags){ + assertMetric(name, value, -1, -1, commonTags, additionalTags, countTags); + } + + public void assertMetric(String name, Number lowerBound, Number upperBound, ArrayList commonTags, ArrayList additionalTags, int countTags){ + assertMetric(name, -1, lowerBound, upperBound, commonTags, additionalTags, countTags); + } + + public void assertMetric(String name, Number value, ArrayList tags, int countTags){ + assertMetric(name, value, tags, new ArrayList(), countTags); + } + + public void assertMetric(String name,Number lowerBound, Number upperBound, ArrayList tags, int countTags){ + assertMetric(name, lowerBound, upperBound, tags, new ArrayList(), countTags); + } + + /** + * Assert that all -excluding JVM related- metrics were tested. + * + * @return fail if a metric was not tested + */ + public void assertCoverage(){ + int totalMetrics = 0; + LinkedList> untestedMetrics = new LinkedList>(); + + for (HashMap m: metrics) { + String mName = (String) (m.get("name")); + Double mValue = (Double) (m.get("value")); + Set mTags = new HashSet(Arrays.asList((String[]) (m.get("tags")))); + + // Exclusion logic + if (mName.startsWith("jvm.")) { + continue; + } + + // End of exclusion logic + totalMetrics += 1; + if (!m.containsKey("tested")) { + untestedMetrics.add(m); + } + } + + if (untestedMetrics.size() > 0) { + String message = generateReport(untestedMetrics, totalMetrics); + fail(message); + } + return; + } + + /** + * Generate a report with untested metrics. + * + * @return String report + */ + private static String generateReport(LinkedList> untestedMetrics, int totalMetricsCount){ + StringBuilder sb = new StringBuilder(); + + // Compute indicators + int testedMetricsCount = totalMetricsCount - untestedMetrics.size(); + int coverageMetrics = (int)((testedMetricsCount * 100.0f) / totalMetricsCount); + + sb.append("Coverage\n"); + sb.append("========================================\n"); + sb.append("\tMETRICS\n"); + sb.append("\t\tTested "); + sb.append(testedMetricsCount); + sb.append("/"); + sb.append(totalMetricsCount); + sb.append(" ("); + sb.append(coverageMetrics); + sb.append("%)\n"); + sb.append("\t\tUNTESTED: \n"); + for (HashMap m: untestedMetrics) { + sb.append(m); + sb.append("\n"); + } + sb.append("========================================\n"); + return sb.toString(); + + } + } diff --git a/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java b/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java index f9bf6c4f3..0c34fc398 100644 --- a/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java +++ b/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java @@ -93,6 +93,7 @@ public void testServiceCheckCRITICAL() throws Exception { registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test_non_running:type=ServiceCheckTest2"); initApplication("non_running_process.yaml"); + // Test that a CRITICAL service check status is sent on initialization LinkedList> serviceChecks = getServiceChecks(); assertEquals(1, serviceChecks.size()); From 15e616a2988862dcf7d7851cf0c3a8bd275e107e Mon Sep 17 00:00:00 2001 From: Yann Mahe Date: Tue, 13 Oct 2015 17:46:51 +0200 Subject: [PATCH 4/4] [test] cover `Float` attribute type support Assert `java.lang.Float` attribute type support. --- .../datadog/jmxfetch/SimpleTestJavaApp.java | 4 + .../jmxfetch/SimpleTestJavaAppMBean.java | 2 + .../java/org/datadog/jmxfetch/TestApp.java | 13 +- .../java/org/datadog/jmxfetch/TestCommon.java | 414 +++++++++--------- src/test/resources/jmx_filter_issues.yaml | 23 + src/test/resources/jmx_filtering_test.yaml | 32 ++ 6 files changed, 276 insertions(+), 212 deletions(-) create mode 100644 src/test/resources/jmx_filter_issues.yaml create mode 100644 src/test/resources/jmx_filtering_test.yaml diff --git a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java index 0b3412f62..0069280b5 100644 --- a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java +++ b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java @@ -107,4 +107,8 @@ public Integer getInt424242() { public float getPrimitiveFloat(){ return primitiveFloat; } + + public Float getInstanceFloat(){ + return instanceFloat; + } } diff --git a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java index a8355e591..99841d809 100644 --- a/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java +++ b/src/test/java/org/datadog/jmxfetch/SimpleTestJavaAppMBean.java @@ -34,4 +34,6 @@ public interface SimpleTestJavaAppMBean { float getPrimitiveFloat(); + Float getInstanceFloat(); + } diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index 261778596..fd91882d1 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -66,8 +66,8 @@ public void testDomainInclude() throws Exception { run(); LinkedList> metrics = getMetrics(); - // First filter 27 = 13 metrics from java.lang + 13 metrics implicitly defined - assertEquals(27, metrics.size()); + // First filter 28 = 13 metrics from java.lang + 15 metrics implicitly defined + assertEquals(28, metrics.size()); } @Test @@ -258,7 +258,7 @@ public void testApp() throws Exception { run(); LinkedList> metrics = getMetrics(); - assertEquals(26, metrics.size()); // 26 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + the 8 gauges that is implicitly collected, see jmx.yaml in the test/resources folder + assertEquals(27, metrics.size()); // 27 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + the 9 gauges that is implicitly collected, see jmx.yaml in the test/resources folder // We test for the presence and the value of the metrics we want to collect ArrayList commonTags = new ArrayList() {{ @@ -281,12 +281,13 @@ public void testApp() throws Exception { assertMetric("jmx.org.datadog.jmxfetch.test.atomic4242", 4242.0, commonTags, 5); assertMetric("jmx.org.datadog.jmxfetch.test.object1337", 13.37, commonTags, 5); assertMetric("jmx.org.datadog.jmxfetch.test.primitive_float", 123.4f, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.instance_float", 567.8f, commonTags, 5); assertCoverage(); // We run a second collection. The counter should now be present run(); metrics = getMetrics(); - assertEquals(28, metrics.size()); // 28 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 8 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder + assertEquals(29, metrics.size()); // 29 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 9 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder // We test for the same metrics but this time, the counter should be here // Previous metrics @@ -304,6 +305,7 @@ public void testApp() throws Exception { assertMetric("jmx.org.datadog.jmxfetch.test.atomic4242", 4242.0, commonTags, 5); assertMetric("jmx.org.datadog.jmxfetch.test.object1337", 13.37, commonTags, 5); assertMetric("jmx.org.datadog.jmxfetch.test.primitive_float", 123.4f, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.instance_float", 567.8f, commonTags, 5); // Counters assertMetric("subattr.counter", 0.0, commonTags, 5); @@ -317,7 +319,7 @@ public void testApp() throws Exception { run(); metrics = getMetrics(); - assertEquals(metrics.size(), 28); // 28 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 8 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder + assertEquals(metrics.size(), 29); // 28 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 9 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder // Previous metrics assertMetric("this.is.100", 100.0, commonTags, 8); @@ -334,6 +336,7 @@ public void testApp() throws Exception { assertMetric("jmx.org.datadog.jmxfetch.test.atomic4242", 4242.0, commonTags, 5); assertMetric("jmx.org.datadog.jmxfetch.test.object1337", 13.37, commonTags, 5); assertMetric("jmx.org.datadog.jmxfetch.test.primitive_float", 123.4f, commonTags, 5); + assertMetric("jmx.org.datadog.jmxfetch.test.instance_float", 567.8f, commonTags, 5); // Counter assertMetric("subattr.counter", 0.98, 1, commonTags, 5); diff --git a/src/test/java/org/datadog/jmxfetch/TestCommon.java b/src/test/java/org/datadog/jmxfetch/TestCommon.java index 233605c77..809c5ff8f 100644 --- a/src/test/java/org/datadog/jmxfetch/TestCommon.java +++ b/src/test/java/org/datadog/jmxfetch/TestCommon.java @@ -30,228 +30,228 @@ public class TestCommon { - AppConfig appConfig = new AppConfig(); - App app; - MBeanServer mbs; - ArrayList objectNames = new ArrayList(); - LinkedList> metrics; - LinkedList> serviceChecks; - - /** - * Setup logger. - */ - @BeforeClass + AppConfig appConfig = new AppConfig(); + App app; + MBeanServer mbs; + ArrayList objectNames = new ArrayList(); + LinkedList> metrics; + LinkedList> serviceChecks; + + /** + * Setup logger. + */ + @BeforeClass public static void init() throws Exception { CustomLogger.setup(Level.toLevel("ALL"), "/tmp/jmxfetch_test.log"); } - /** - * Register a MBean with the given name, and application attributes. - * @throws NotCompliantMBeanException - * @throws MBeanRegistrationException - * @throws InstanceAlreadyExistsException - * @throws MalformedObjectNameException - */ - protected void registerMBean(Object application, String objectStringName) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, MalformedObjectNameException { - mbs = (mbs == null)? ManagementFactory.getPlatformMBeanServer() : mbs; - ObjectName objectName = new ObjectName(objectStringName); - objectNames.add(objectName); - mbs.registerMBean(application, objectName); - } - - /** - * Unregister MBeans. - * Note: executed after execution of every test. - * @throws InstanceNotFoundException - * @throws MBeanRegistrationException - */ - @After - public void unregisterMBean() throws MBeanRegistrationException, InstanceNotFoundException{ - if (mbs != null) { - for (ObjectName objectName: objectNames ) { - mbs.unregisterMBean(objectName); - } - } - } - - /** - * Init JMXFetch with the given YAML configuration file. - */ - protected void initApplication(String yamlFileName){ - // We do a first collection - // We initialize the main app that will collect these metrics using JMX - String confdDirectory = Thread.currentThread().getContextClassLoader().getResource(yamlFileName).getPath(); - confdDirectory = new String(confdDirectory.substring(0, confdDirectory.length() - yamlFileName.length())); - String[] params = {"--reporter", "console", "-c", yamlFileName, "--conf_directory", confdDirectory, "collect"}; - new JCommander(appConfig, params); - - app = new App(appConfig); - app.init(false); - } - - /** - * Run a JMXFetch iteration. - */ - protected void run(){ - if (app != null) { - app.doIteration(); - metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); - } - } - - /** - * Return JMXFetch reporter. - */ - protected Reporter getReporter(){ - return appConfig.getReporter(); - } - - /** - * Return the metrics collected by JMXFetch. - */ - protected LinkedList> getMetrics(){ - return metrics; - } - - /** - * Return the service checks collected by JMXFetch. - */ - protected LinkedList> getServiceChecks(){ - return ((ConsoleReporter) appConfig.getReporter()).getServiceChecks(); - } - - /** - * Assert that a specific metric was collected. - * Brand the metric so we can easily know which metric have/have not been tested - * - * @param name metric name - * - * @param value metric value - * - * @param lowerBound lower bound metric value - * - * @param upperBound upper bound metric value - * - * @param commonTags metric tags inherited from the instance configuration - * - * @param additionalTags metric tags inherited from the bean properties - * - * @param countTags number of metric tags - * - * @return fail if the metric was not found - */ - public void assertMetric(String name, Number value, Number lowerBound, Number upperBound, ArrayList commonTags, ArrayList additionalTags, int countTags){ - List tags = new ArrayList(commonTags); - tags.addAll(additionalTags); - - for (HashMap m: metrics) { - String mName = (String) (m.get("name")); + /** + * Register a MBean with the given name, and application attributes. + * @throws NotCompliantMBeanException + * @throws MBeanRegistrationException + * @throws InstanceAlreadyExistsException + * @throws MalformedObjectNameException + */ + protected void registerMBean(Object application, String objectStringName) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, MalformedObjectNameException { + mbs = (mbs == null)? ManagementFactory.getPlatformMBeanServer() : mbs; + ObjectName objectName = new ObjectName(objectStringName); + objectNames.add(objectName); + mbs.registerMBean(application, objectName); + } + + /** + * Unregister MBeans. + * Note: executed after execution of every test. + * @throws InstanceNotFoundException + * @throws MBeanRegistrationException + */ + @After + public void unregisterMBean() throws MBeanRegistrationException, InstanceNotFoundException{ + if (mbs != null) { + for (ObjectName objectName: objectNames ) { + mbs.unregisterMBean(objectName); + } + } + } + + /** + * Init JMXFetch with the given YAML configuration file. + */ + protected void initApplication(String yamlFileName){ + // We do a first collection + // We initialize the main app that will collect these metrics using JMX + String confdDirectory = Thread.currentThread().getContextClassLoader().getResource(yamlFileName).getPath(); + confdDirectory = new String(confdDirectory.substring(0, confdDirectory.length() - yamlFileName.length())); + String[] params = {"--reporter", "console", "-c", yamlFileName, "--conf_directory", confdDirectory, "collect"}; + new JCommander(appConfig, params); + + app = new App(appConfig); + app.init(false); + } + + /** + * Run a JMXFetch iteration. + */ + protected void run(){ + if (app != null) { + app.doIteration(); + metrics = ((ConsoleReporter) appConfig.getReporter()).getMetrics(); + } + } + + /** + * Return JMXFetch reporter. + */ + protected Reporter getReporter(){ + return appConfig.getReporter(); + } + + /** + * Return the metrics collected by JMXFetch. + */ + protected LinkedList> getMetrics(){ + return metrics; + } + + /** + * Return the service checks collected by JMXFetch. + */ + protected LinkedList> getServiceChecks(){ + return ((ConsoleReporter) appConfig.getReporter()).getServiceChecks(); + } + + /** + * Assert that a specific metric was collected. + * Brand the metric so we can easily know which metric have/have not been tested + * + * @param name metric name + * + * @param value metric value + * + * @param lowerBound lower bound metric value + * + * @param upperBound upper bound metric value + * + * @param commonTags metric tags inherited from the instance configuration + * + * @param additionalTags metric tags inherited from the bean properties + * + * @param countTags number of metric tags + * + * @return fail if the metric was not found + */ + public void assertMetric(String name, Number value, Number lowerBound, Number upperBound, ArrayList commonTags, ArrayList additionalTags, int countTags){ + List tags = new ArrayList(commonTags); + tags.addAll(additionalTags); + + for (HashMap m: metrics) { + String mName = (String) (m.get("name")); Double mValue = (Double) (m.get("value")); Set mTags = new HashSet(Arrays.asList((String[]) (m.get("tags")))); - if (mName.equals(name)) { - - if (lowerBound.equals(-1) && upperBound.equals(-1)){ - assertEquals((Double)value.doubleValue(), mValue); - }else{ - assertTrue(mValue > (Double)lowerBound.doubleValue()); - assertTrue(mValue < (Double)upperBound.doubleValue()); - } - - if (countTags != -1) { - assertEquals(countTags, mTags.size()); - } - for (String t: tags) { - assertTrue(mTags.contains(t)); - } - // Brand the metric - m.put("tested", true); - - return; - } - } - fail("Metric assertion failed (name: "+name+", value: "+value+", tags: "+tags+", #tags: "+countTags+")."); - } - - public void assertMetric(String name, Number value, ArrayList commonTags, ArrayList additionalTags, int countTags){ - assertMetric(name, value, -1, -1, commonTags, additionalTags, countTags); - } - - public void assertMetric(String name, Number lowerBound, Number upperBound, ArrayList commonTags, ArrayList additionalTags, int countTags){ - assertMetric(name, -1, lowerBound, upperBound, commonTags, additionalTags, countTags); - } - - public void assertMetric(String name, Number value, ArrayList tags, int countTags){ - assertMetric(name, value, tags, new ArrayList(), countTags); - } - - public void assertMetric(String name,Number lowerBound, Number upperBound, ArrayList tags, int countTags){ - assertMetric(name, lowerBound, upperBound, tags, new ArrayList(), countTags); - } - - /** - * Assert that all -excluding JVM related- metrics were tested. - * - * @return fail if a metric was not tested - */ - public void assertCoverage(){ - int totalMetrics = 0; - LinkedList> untestedMetrics = new LinkedList>(); - - for (HashMap m: metrics) { - String mName = (String) (m.get("name")); + if (mName.equals(name)) { + + if (lowerBound.equals(-1) && upperBound.equals(-1)){ + assertEquals((Double)value.doubleValue(), mValue); + }else{ + assertTrue(mValue > (Double)lowerBound.doubleValue()); + assertTrue(mValue < (Double)upperBound.doubleValue()); + } + + if (countTags != -1) { + assertEquals(countTags, mTags.size()); + } + for (String t: tags) { + assertTrue(mTags.contains(t)); + } + // Brand the metric + m.put("tested", true); + + return; + } + } + fail("Metric assertion failed (name: "+name+", value: "+value+", tags: "+tags+", #tags: "+countTags+")."); + } + + public void assertMetric(String name, Number value, ArrayList commonTags, ArrayList additionalTags, int countTags){ + assertMetric(name, value, -1, -1, commonTags, additionalTags, countTags); + } + + public void assertMetric(String name, Number lowerBound, Number upperBound, ArrayList commonTags, ArrayList additionalTags, int countTags){ + assertMetric(name, -1, lowerBound, upperBound, commonTags, additionalTags, countTags); + } + + public void assertMetric(String name, Number value, ArrayList tags, int countTags){ + assertMetric(name, value, tags, new ArrayList(), countTags); + } + + public void assertMetric(String name,Number lowerBound, Number upperBound, ArrayList tags, int countTags){ + assertMetric(name, lowerBound, upperBound, tags, new ArrayList(), countTags); + } + + /** + * Assert that all -excluding JVM related- metrics were tested. + * + * @return fail if a metric was not tested + */ + public void assertCoverage(){ + int totalMetrics = 0; + LinkedList> untestedMetrics = new LinkedList>(); + + for (HashMap m: metrics) { + String mName = (String) (m.get("name")); Double mValue = (Double) (m.get("value")); Set mTags = new HashSet(Arrays.asList((String[]) (m.get("tags")))); // Exclusion logic if (mName.startsWith("jvm.")) { - continue; + continue; } // End of exclusion logic totalMetrics += 1; - if (!m.containsKey("tested")) { - untestedMetrics.add(m); - } - } - - if (untestedMetrics.size() > 0) { - String message = generateReport(untestedMetrics, totalMetrics); - fail(message); - } - return; - } - - /** - * Generate a report with untested metrics. - * - * @return String report - */ - private static String generateReport(LinkedList> untestedMetrics, int totalMetricsCount){ - StringBuilder sb = new StringBuilder(); - - // Compute indicators - int testedMetricsCount = totalMetricsCount - untestedMetrics.size(); - int coverageMetrics = (int)((testedMetricsCount * 100.0f) / totalMetricsCount); - - sb.append("Coverage\n"); - sb.append("========================================\n"); - sb.append("\tMETRICS\n"); - sb.append("\t\tTested "); - sb.append(testedMetricsCount); - sb.append("/"); - sb.append(totalMetricsCount); - sb.append(" ("); - sb.append(coverageMetrics); - sb.append("%)\n"); - sb.append("\t\tUNTESTED: \n"); - for (HashMap m: untestedMetrics) { - sb.append(m); - sb.append("\n"); - } - sb.append("========================================\n"); - return sb.toString(); - - } + if (!m.containsKey("tested")) { + untestedMetrics.add(m); + } + } + + if (untestedMetrics.size() > 0) { + String message = generateReport(untestedMetrics, totalMetrics); + fail(message); + } + return; + } + + /** + * Generate a report with untested metrics. + * + * @return String report + */ + private static String generateReport(LinkedList> untestedMetrics, int totalMetricsCount){ + StringBuilder sb = new StringBuilder(); + + // Compute indicators + int testedMetricsCount = totalMetricsCount - untestedMetrics.size(); + int coverageMetrics = (int)((testedMetricsCount * 100.0f) / totalMetricsCount); + + sb.append("Coverage\n"); + sb.append("========================================\n"); + sb.append("\tMETRICS\n"); + sb.append("\t\tTested "); + sb.append(testedMetricsCount); + sb.append("/"); + sb.append(totalMetricsCount); + sb.append(" ("); + sb.append(coverageMetrics); + sb.append("%)\n"); + sb.append("\t\tUNTESTED: \n"); + for (HashMap m: untestedMetrics) { + sb.append(m); + sb.append("\n"); + } + sb.append("========================================\n"); + return sb.toString(); + + } } diff --git a/src/test/resources/jmx_filter_issues.yaml b/src/test/resources/jmx_filter_issues.yaml new file mode 100644 index 000000000..6df61daec --- /dev/null +++ b/src/test/resources/jmx_filter_issues.yaml @@ -0,0 +1,23 @@ +init_config: + +instances: + - process_name_regex: .*surefire.* + name: jmx_test_instance + conf: + - include: + domain: org.datadog.jmxfetch.test + subType: + - Uris + resource: + - "\"/v1/MyMixUser/{userGuid}/{channelGuid}/tune\"" + executionTimes: + - MethodTimes + + - include: + domain: org.datadog.jmxfetch.test + subType: + - Uris + resource: + - "\"/v1/MyMixUser/{userGuid}/{channelGuid}/clips/play\"" + executionTimes: + - MethodTimes diff --git a/src/test/resources/jmx_filtering_test.yaml b/src/test/resources/jmx_filtering_test.yaml new file mode 100644 index 000000000..aa3ea7d3d --- /dev/null +++ b/src/test/resources/jmx_filtering_test.yaml @@ -0,0 +1,32 @@ +instances: + - process_name_regex: .*surefire.* + name: jmx_test_instance + + +init_config: + conf: + - include: + domain: org.glassfish.jersey + subType: + - Uris + resource: + - "\"/v1/MyMixUser/{userGuid}/{channelGuid}/tune\"" + executionTimes: + - MethodTimes + # attribute: + # "AverageTime[ms]_1m": + # alias: jmx.siriusxm.sequencer.tune.ave_time + # metric_type: gauge + - include: + domain: org.glassfish.jersey + subType: + - Uris + resource: + - "\"/v1/MyMixUser/{userGuid}/{channelGuid}/clips/play\"" + executionTimes: + - MethodTimes + # attribute: + # "AverageTime[ms]_1m": + # alias: jmx.siriusxm.sequencer.play.ave_time + # metric_type: gauge +