Skip to content

Commit

Permalink
Merge branch 'master' into raymond.zhao/AMLII-1138-jmxfetch-origin-me…
Browse files Browse the repository at this point in the history
…trics
  • Loading branch information
rayz committed Nov 3, 2023
2 parents e08836f + 7c2921b commit 754b230
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ run_unit_tests:

artifacts:
expire_in: 1 mos
when: always
paths:
- ./target/surefire-reports/*.txt

Expand Down
28 changes: 27 additions & 1 deletion src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -828,6 +834,7 @@ public void init(final boolean forceNewConnection) {
this.brokenInstanceMap.clear();

final List<Instance> newInstances = new ArrayList<>();
final Set<String> instanceNamesSeen = new HashSet<>();

log.info("Dealing with YAML config instances...");
final Iterator<Entry<String, YamlParser>> it = this.configs.entrySet().iterator();
Expand Down Expand Up @@ -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 =
Expand All @@ -893,6 +909,16 @@ public void init(final boolean forceNewConnection) {
final String checkName = (String) checkConfig.get("check_name");
for (Map<String, Object> 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);
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -975,7 +975,7 @@ public void testServiceDiscovery() throws Exception {
List<String> 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",
Expand Down Expand Up @@ -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");

Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/datadog/jmxfetch/TestCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/jmx_alias_match.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/jmx_counter_rate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/jmx_sd_pipe.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/jmx_sd_pipe_longname.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down

0 comments on commit 754b230

Please sign in to comment.