diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 81ff2e906..122018a57 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -29,6 +29,7 @@ run_unit_tests: artifacts: expire_in: 1 mos + when: always paths: - ./target/surefire-reports/*.txt diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index ddb6f1de8..6d2d2e16b 100644 --- a/src/main/java/org/datadog/jmxfetch/App.java +++ b/src/main/java/org/datadog/jmxfetch/App.java @@ -29,11 +29,13 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.concurrent.CancellationException; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutionException; @@ -242,6 +244,10 @@ public TaskStatusHandler invoke( } } + protected void clearAllInstances() { + this.clearInstances(this.instances); + } + /** * Builds an {@link ExecutorService} of the specified fixed size. Threads will be created * and executed as daemons if {@link AppConfig#isDaemon()} is true. Defaults to false. @@ -828,6 +834,7 @@ public void init(final boolean forceNewConnection) { this.brokenInstanceMap.clear(); final List newInstances = new ArrayList<>(); + final Set instanceNamesSeen = new HashSet<>(); log.info("Dealing with YAML config instances..."); final Iterator> it = this.configs.entrySet().iterator(); @@ -867,7 +874,16 @@ public void init(final boolean forceNewConnection) { isDirectInstance(configInstance)); continue; } - + final String instanceName = (String) configInstance.get("name"); + if (instanceName != null) { + if (instanceNamesSeen.contains(instanceName)) { + log.warn("Found multiple instances with name: '{}'. " + + "Instance names should be unique, " + + "update the 'name' field on your instances to be unique.", + instanceName); + } + instanceNamesSeen.add(instanceName); + } // Create a new Instance object log.info("Instantiating instance for: {}", name); final Instance instance = @@ -893,6 +909,16 @@ public void init(final boolean forceNewConnection) { final String checkName = (String) checkConfig.get("check_name"); for (Map configInstance : configInstances) { log.info("Instantiating instance for: " + checkName); + final String instanceName = (String) configInstance.get("name"); + if (instanceName != null) { + if (instanceNamesSeen.contains(instanceName)) { + log.warn("Found multiple instances with name: '{}'. " + + "Instance names should be unique, " + + "update the 'name' field on your instances to be unique.", + instanceName); + } + instanceNamesSeen.add(instanceName); + } final Instance instance = instantiate(configInstance, initConfig, checkName, this.appConfig); newInstances.add(instance); diff --git a/src/test/java/org/datadog/jmxfetch/TestApp.java b/src/test/java/org/datadog/jmxfetch/TestApp.java index a7e2ee1f6..03b72caf5 100644 --- a/src/test/java/org/datadog/jmxfetch/TestApp.java +++ b/src/test/java/org/datadog/jmxfetch/TestApp.java @@ -157,7 +157,7 @@ public void testRegexpAliasing() throws Exception { Arrays.asList( "jmx_domain:org.datadog.jmxfetch.test", "jmx_check_name:jmx_alias_match", - "instance:jmx_test_instance", + "instance:jmx_test_instance1", "foo:Bar", "qux:Baz"); @@ -975,7 +975,7 @@ public void testServiceDiscovery() throws Exception { List tags = Arrays.asList( "type:SimpleTestJavaApp", "scope:CoolScope", - "instance:jmx_test_instance", + "instance:jmx_test_instance2", "jmx_domain:org.datadog.jmxfetch.test", "jmx_check_name:AD-jmx_0", "bean_host:localhost", @@ -1014,7 +1014,7 @@ public void testServiceDiscovery() throws Exception { Arrays.asList( "jmx_domain:org.datadog.jmxfetch.test", "jmx_check_name:jmx_alias_match", - "instance:jmx_test_instance", + "instance:jmx_test_instance1", "foo:Bar", "qux:Baz"); diff --git a/src/test/java/org/datadog/jmxfetch/TestCommon.java b/src/test/java/org/datadog/jmxfetch/TestCommon.java index 2b6ea4e5e..edb8bde68 100644 --- a/src/test/java/org/datadog/jmxfetch/TestCommon.java +++ b/src/test/java/org/datadog/jmxfetch/TestCommon.java @@ -112,6 +112,16 @@ public void unregisterMBean() throws MBeanRegistrationException, InstanceNotFoun } } + /** + * Clear instances and their instance telemetry bean after execution of every test. + */ + @After + public void clearInstances() { + if (app != null) { + app.clearAllInstances(); + } + } + /** Init JMXFetch with the given YAML configuration file. */ protected void initApplication(String yamlFileName, String autoDiscoveryPipeFile) throws FileNotFoundException, IOException { diff --git a/src/test/resources/jmx_alias_match.yaml b/src/test/resources/jmx_alias_match.yaml index 3d5d51193..8626e89ed 100644 --- a/src/test/resources/jmx_alias_match.yaml +++ b/src/test/resources/jmx_alias_match.yaml @@ -2,7 +2,7 @@ init_config: instances: - process_name_regex: .*surefire.* - name: jmx_test_instance + name: jmx_test_instance1 conf: - include: domain: org.datadog.jmxfetch.test diff --git a/src/test/resources/jmx_counter_rate.yaml b/src/test/resources/jmx_counter_rate.yaml index ab58e7aad..4485994ee 100644 --- a/src/test/resources/jmx_counter_rate.yaml +++ b/src/test/resources/jmx_counter_rate.yaml @@ -3,7 +3,7 @@ init_config: instances: - process_name_regex: .*surefire.* refresh_beans: 4 - name: jmx_test_instance + name: jmx_test_instance1 conf: - include: domain: org.datadog.jmxfetch.test @@ -13,7 +13,7 @@ instances: alias: test.counter - process_name_regex: .*surefire.* refresh_beans: 4 - name: jmx_test_instance + name: jmx_test_instance2 conf: - include: domain: org.datadog.jmxfetch.test diff --git a/src/test/resources/jmx_sd_pipe.txt b/src/test/resources/jmx_sd_pipe.txt index be9ef7688..945fbc4c0 100644 --- a/src/test/resources/jmx_sd_pipe.txt +++ b/src/test/resources/jmx_sd_pipe.txt @@ -25,7 +25,7 @@ init_config: instances: - process_name_regex: .*surefire.* - name: jmx_test_instance + name: jmx_test_instance2 conf: - include: bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=Co|olScope,host=localhost,component= diff --git a/src/test/resources/jmx_sd_pipe_longname.txt b/src/test/resources/jmx_sd_pipe_longname.txt index d1f58c2ee..8e6380525 100644 --- a/src/test/resources/jmx_sd_pipe_longname.txt +++ b/src/test/resources/jmx_sd_pipe_longname.txt @@ -4,7 +4,7 @@ init_config: instances: - process_name_regex: .*surefire.* - name: jmx_test_instance + name: jmx_test_instance2 conf: - include: bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=Co|olScope,host=localhost,component= @@ -25,7 +25,7 @@ init_config: instances: - process_name_regex: .*surefire.* - name: jmx_test_instance + name: jmx_test_instance3 conf: - include: bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=Co|olScope,host=localhost,component=