diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 85e704b7c..6246fa885 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -1,6 +1,7 @@ package org.datadog.jmxfetch; import java.io.IOException; +import java.lang.ClassCastException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -69,7 +70,7 @@ public Instance(LinkedHashMap yamlInstance, LinkedHashMap(yamlInstance) : null; this.initConfig = initConfig != null ? new LinkedHashMap(initConfig) : null; this.instanceName = (String) yaml.get("name"); - this.tags = (LinkedHashMap) yaml.get("tags"); + this.tags = getTagsMap(yaml.get("tags")); this.checkName = checkName; this.matchingAttributes = new LinkedList(); this.failingAttributes = new HashSet(); @@ -126,6 +127,27 @@ public Instance(LinkedHashMap yamlInstance, LinkedHashMap) new YamlParser(this.getClass().getResourceAsStream("/jmx-2.yaml")).getParsedYaml())); } + /** + * Format the instance tags defined in the YAML configuration file to a `LinkedHashMap`. + * Supported inputs: `List`, `Map`. + */ + private static LinkedHashMap getTagsMap(Object yamlTags){ + try { + // Input has `Map` format + return (LinkedHashMap) yamlTags; + } + catch (ClassCastException e){ + // Input has `List` format + LinkedHashMap tags = new LinkedHashMap(); + + for (String tag: (List)yamlTags) { + tags.put(tag, null); + } + + return tags; + } + } + public Connection getConnection(LinkedHashMap connectionParams, boolean forceNewConnection) throws IOException { if (connection == null || !connection.isAlive()) { LOGGER.info("Connection closed or does not exist. Creating a new connection!"); @@ -335,9 +357,13 @@ public String[] getServiceCheckTags() { tags.add("jmx_server:" + this.yaml.get("host")); } if (this.tags != null) { - for (Entry e : this.tags.entrySet()) { - tags.add(e.getKey() + ":" + e.getValue()); - } + for (Entry e : this.tags.entrySet()) { + if (e.getValue()!=null){ + tags.add(e.getKey() + ":" + e.getValue()); + } else { + tags.add(e.getKey()); + } + } } tags.add("instance:" + this.instanceName); return tags.toArray(new String[tags.size()]); diff --git a/src/main/java/org/datadog/jmxfetch/JMXAttribute.java b/src/main/java/org/datadog/jmxfetch/JMXAttribute.java index aa7c6eee2..cbbcc55f6 100644 --- a/src/main/java/org/datadog/jmxfetch/JMXAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JMXAttribute.java @@ -134,7 +134,12 @@ private LinkedList getBeanParametersList(String instanceName, Map tag : instanceTags.entrySet()) { - beanTags.add(tag.getKey() + ":" + tag.getValue()); + if (tag.getValue() != null) { + beanTags.add(tag.getKey() + ":" + tag.getValue()); + } + else { + beanTags.add(tag.getKey()); + } } } diff --git a/src/test/resources/jmx.yaml b/src/test/resources/jmx.yaml index 57fb97f02..9c78aee9b 100644 --- a/src/test/resources/jmx.yaml +++ b/src/test/resources/jmx.yaml @@ -5,8 +5,8 @@ instances: refresh_beans: 4 name: jmx_test_instance tags: - env: stage - newTag: test + - "env:stage" + - "newTag:test" conf: - include: domain: org.datadog.jmxfetch.test