Skip to content

Commit

Permalink
[instance] support instance tags in list format
Browse files Browse the repository at this point in the history
Support instance tags defined in an array/list format.
  • Loading branch information
yannmh committed Mar 3, 2017
1 parent 2f58003 commit 94cbee9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
34 changes: 30 additions & 4 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -69,7 +70,7 @@ public Instance(LinkedHashMap<String, Object> yamlInstance, LinkedHashMap<String
this.yaml = yamlInstance != null ? new LinkedHashMap<String, Object>(yamlInstance) : null;
this.initConfig = initConfig != null ? new LinkedHashMap<String, Object>(initConfig) : null;
this.instanceName = (String) yaml.get("name");
this.tags = (LinkedHashMap<String, String>) yaml.get("tags");
this.tags = getTagsMap(yaml.get("tags"));
this.checkName = checkName;
this.matchingAttributes = new LinkedList<JMXAttribute>();
this.failingAttributes = new HashSet<JMXAttribute>();
Expand Down Expand Up @@ -126,6 +127,27 @@ public Instance(LinkedHashMap<String, Object> yamlInstance, LinkedHashMap<String
configurationList.add(new Configuration((LinkedHashMap<String, Object>) 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<String, String> getTagsMap(Object yamlTags){
try {
// Input has `Map` format
return (LinkedHashMap<String, String>) yamlTags;
}
catch (ClassCastException e){
// Input has `List` format
LinkedHashMap<String, String> tags = new LinkedHashMap<String, String>();

for (String tag: (List<String>)yamlTags) {
tags.put(tag, null);
}

return tags;
}
}

public Connection getConnection(LinkedHashMap<String, Object> connectionParams, boolean forceNewConnection) throws IOException {
if (connection == null || !connection.isAlive()) {
LOGGER.info("Connection closed or does not exist. Creating a new connection!");
Expand Down Expand Up @@ -335,9 +357,13 @@ public String[] getServiceCheckTags() {
tags.add("jmx_server:" + this.yaml.get("host"));
}
if (this.tags != null) {
for (Entry<String, String> e : this.tags.entrySet()) {
tags.add(e.getKey() + ":" + e.getValue());
}
for (Entry<String, String> 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()]);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ private LinkedList<String> getBeanParametersList(String instanceName, Map<String

if (instanceTags != null) {
for (Map.Entry<String, String> tag : instanceTags.entrySet()) {
beanTags.add(tag.getKey() + ":" + tag.getValue());
if (tag.getValue() != null) {
beanTags.add(tag.getKey() + ":" + tag.getValue());
}
else {
beanTags.add(tag.getKey());
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/jmx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 94cbee9

Please sign in to comment.