Skip to content

Commit

Permalink
Add warning for duplicate instance name
Browse files Browse the repository at this point in the history
  • Loading branch information
rayz committed Oct 11, 2023
1 parent 93e6f90 commit 99f96b2
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 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 @@ -828,6 +830,8 @@ public void init(final boolean forceNewConnection) {
this.brokenInstanceMap.clear();

final List<Instance> newInstances = new ArrayList<>();
// used to track instance names already seen
Set<String> instanceNames = new HashSet<String>();

log.info("Dealing with YAML config instances...");
final Iterator<Entry<String, YamlParser>> it = this.configs.entrySet().iterator();
Expand Down Expand Up @@ -867,7 +871,11 @@ public void init(final boolean forceNewConnection) {
isDirectInstance(configInstance));
continue;
}

final String instanceName = (String) configInstance.get("name");
if (instanceNames.contains(instanceName)) {
log.warn("Instance with name: {} already exists", instanceName);
}
instanceNames.add(instanceName);
// Create a new Instance object
log.info("Instantiating instance for: {}", name);
final Instance instance =
Expand All @@ -893,6 +901,11 @@ 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 (instanceNames.contains(instanceName)) {
log.warn("Instance with name: {} already exists", instanceName);
}
instanceNames.add(instanceName);
final Instance instance =
instantiate(configInstance, initConfig, checkName, this.appConfig);
newInstances.add(instance);
Expand Down

0 comments on commit 99f96b2

Please sign in to comment.