Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename 'host' bean parameter to 'bean_host' in tags #59

Merged
merged 2 commits into from
May 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public abstract class JMXAttribute {
String[] splitBeanName = this.beanName.split(":");
String domain = splitBeanName[0];
String beanParameters = splitBeanName[1];
LinkedList<String> defaultTags = getBeanTags(instanceName, domain, beanParameters, instanceTags);
HashMap<String, String> beanParametersHash = getBeanParametersHash(defaultTags);
LinkedList<String> beanParametersList = getBeanParametersList(instanceName, domain, beanParameters, instanceTags);
HashMap<String, String> beanParametersHash = getBeanParametersHash(beanParametersList);

this.domain = domain;
this.beanParameters = beanParametersHash;
this.defaultTagsList = defaultTags;
this.defaultTagsList = renameConflictingParameters(beanParametersList);
}

private static HashMap<String, String> getBeanParametersHash(LinkedList<String> beanParameters) {
Expand All @@ -74,7 +74,7 @@ private static HashMap<String, String> getBeanParametersHash(LinkedList<String>
}


private static LinkedList<String> getBeanTags(String instanceName, String domain, String beanParameters, HashMap<String, String> instanceTags) {
private static LinkedList<String> getBeanParametersList(String instanceName, String domain, String beanParameters, HashMap<String, String> instanceTags) {
LinkedList<String> beanTags = new LinkedList<String>(Arrays.asList(new String(beanParameters).replace("=", ":").split(",")));
beanTags.add("instance:" + instanceName);
beanTags.add("jmx_domain:" + domain);
Expand All @@ -88,6 +88,20 @@ private static LinkedList<String> getBeanTags(String instanceName, String domain
return beanTags;
}

private static LinkedList<String> renameConflictingParameters(LinkedList<String> beanParametersList) {
LinkedList<String> defaultTagsList = new LinkedList<String>();
for (String beanParameter: beanParametersList) {
// the 'host' parameter is renamed to 'bean_host'
if (beanParameter.startsWith("host:")) {
defaultTagsList.add("bean_host:" + beanParameter.substring("host:".length()));
} else {
defaultTagsList.add(beanParameter);
}
}

return defaultTagsList;
}

static String convertMetricName(String metricName) {
metricName = metricName.replaceAll(FIRST_CAP_PATTERN, "$1_$2");
metricName = metricName.replaceAll(ALL_CAP_PATTERN, "$1_$2").toLowerCase();
Expand Down
34 changes: 18 additions & 16 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static App initApp(String yamlFileName, AppConfig appConfig){
public void testBeanTags() throws Exception {
// We expose a few metrics through JMX
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=CoolScope");
ObjectName objectName = new ObjectName("org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=CoolScope,host=localhost");
SimpleTestJavaApp testApp = new SimpleTestJavaApp();
mbs.registerMBean(testApp, objectName);

Expand All @@ -66,19 +66,21 @@ public void testBeanTags() throws Exception {

// Fetching our 'defined' metric tags
for (HashMap<String, Object> m : metrics) {
String name = (String) (m.get("name"));
if(!name.equals("this.is.100")){
continue;
}
String[] tags = (String[]) (m.get("tags"));
Set<String> tagsSet = new HashSet<String>(Arrays.asList(tags));

// We should find bean parameters as tags
assertEquals(4, tags.length);
assertEquals(true, tagsSet.contains("type:SimpleTestJavaApp"));
assertEquals(true, tagsSet.contains("scope:CoolScope"));
assertEquals(true, tagsSet.contains("instance:jmx_test_instance"));
assertEquals(true, tagsSet.contains("jmx_domain:org.datadog.jmxfetch.test"));
String name = (String) (m.get("name"));
if(!name.equals("this.is.100")){
continue;
}
String[] tags = (String[]) (m.get("tags"));
Set<String> tagsSet = new HashSet<String>(Arrays.asList(tags));

// We should find bean parameters as tags
assertEquals(5, tags.length);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you fix the indentation here please ?

assertEquals(true, tagsSet.contains("type:SimpleTestJavaApp"));
assertEquals(true, tagsSet.contains("scope:CoolScope"));
assertEquals(true, tagsSet.contains("instance:jmx_test_instance"));
assertEquals(true, tagsSet.contains("jmx_domain:org.datadog.jmxfetch.test"));
// Special case of the 'host' parameter which tag is renamed to 'bean_host'
assertEquals(true, tagsSet.contains("bean_host:localhost"));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

}
mbs.unregisterMBean(objectName);
}
Expand Down Expand Up @@ -126,8 +128,8 @@ public void testDomainExclude() throws Exception {
// First filter 14 = 13 metrics from java.lang + 2 metrics explicitly define- 1 implicitly defined in the exclude section
assertEquals(14, metrics.size());

mbs.unregisterMBean(includeMe);
mbs.unregisterMBean(excludeMe);
mbs.unregisterMBean(includeMe);
mbs.unregisterMBean(excludeMe);
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/jmx_bean_tags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ instances:
name: jmx_test_instance
conf:
- include:
bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=CoolScope
bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=CoolScope,host=localhost
attribute:
ShouldBe100:
metric_type: gauge
alias: this.is.100
- include:
bean: org.datadog.jmxfetch.test:type=WrongType,scope=WrongScope
bean: org.datadog.jmxfetch.test:type=WrongType,scope=WrongScope,host=localhost
attribute:
Hashmap.thisis0:
metric_type: gauge
Expand Down