From 07c3f705dba1bed6b44c11343e7cfcfb31b9dd8a Mon Sep 17 00:00:00 2001 From: Raymond Zhao <35050708+rayz@users.noreply.github.com> Date: Wed, 1 Nov 2023 11:41:16 -0400 Subject: [PATCH] Add warning for duplicate instance name (#484) --- src/main/java/org/datadog/jmxfetch/App.java | 24 ++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index ddb6f1de8..3869e11a7 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; @@ -828,6 +830,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 +870,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 +905,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);