Skip to content

Commit

Permalink
Merge pull request #26 from coupacooke/master
Browse files Browse the repository at this point in the history
Support for boolean & conversions
  • Loading branch information
Remi Hakim committed Mar 12, 2014
2 parents dc3d12b + 5f7a03c commit 6c62322
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class Instance {
private final static Logger LOGGER = Logger.getLogger(Instance.class.getName());
private final static List<String> SIMPLE_TYPES = Arrays.asList("long", "java.lang.String", "int", "double", "java.lang.Double", "java.lang.Integer", "java.lang.Long", "java.util.concurrent.atomic.AtomicInteger", "java.util.concurrent.atomic.AtomicLong", "java.lang.Object");
private final static List<String> SIMPLE_TYPES = Arrays.asList("long", "java.lang.String", "int", "double", "java.lang.Double", "java.lang.Integer", "java.lang.Long", "java.util.concurrent.atomic.AtomicInteger", "java.util.concurrent.atomic.AtomicLong", "java.lang.Object", "java.lang.Boolean", "boolean");
private final static List<String> COMPOSED_TYPES = Arrays.asList("javax.management.openmbean.CompositeData", "java.util.HashMap");
private final static int MAX_RETURNED_METRICS = 100;

Expand Down
37 changes: 36 additions & 1 deletion src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -24,6 +25,7 @@ public abstract class JMXAttribute {
protected String domain;
protected String beanName;
protected String attributeName;
protected LinkedHashMap<Object,Object> valueConversions;
protected String[] tags;
protected Configuration matching_conf;
private static final String[] EXCLUDED_BEAN_PARAMS = {"domain", "bean_name", "bean", "attribute"};
Expand Down Expand Up @@ -125,7 +127,21 @@ protected static String convertMetricName(String metricName) {
return metricName;
}

protected double _getValueAsDouble(Object value) {
protected Object convertMetricValue(Object metricValue) {
Object converted = metricValue;

if (!getValueConversions().isEmpty()) {
converted = this.getValueConversions().get(metricValue);
if (converted == null && this.getValueConversions().get("default") != null) {
converted = this.getValueConversions().get("default");
}
}

return converted;
}

protected double _getValueAsDouble(Object metricValue) {
Object value = convertMetricValue(metricValue);

if (value instanceof String) {
return Double.parseDouble((String)value);
Expand All @@ -141,6 +157,8 @@ protected double _getValueAsDouble(Object value) {

} else if (value instanceof Double) {
return (Double)value;
} else if (value instanceof Boolean) {
return ((Boolean)value ? 1.0 : 0.0);
} else if (value instanceof Long) {
Long l = new Long((Long) value);
return l.doubleValue();
Expand Down Expand Up @@ -189,4 +207,21 @@ protected boolean matchBean(Configuration configuration) {
return true;
}

@SuppressWarnings("unchecked")
protected HashMap<Object, Object> getValueConversions() {
if (this.valueConversions == null) {
if (this.matching_conf.include.get("attribute") instanceof LinkedHashMap<?, ?>) {
LinkedHashMap<String, LinkedHashMap<Object, Object>> attribute = ((LinkedHashMap<String, LinkedHashMap<String, LinkedHashMap<Object, Object>>>)(this.matching_conf.include.get("attribute"))).get(this.attribute.getName());

if (attribute != null) {
this.valueConversions = attribute.get("values");
}
}
if (this.valueConversions == null) {
this.valueConversions = new LinkedHashMap<Object, Object>();
}
}

return this.valueConversions;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private String _getMetricType() {

private double _getValue() throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, IOException, NumberFormatException {
Object value = this.getJmxValue();

return _getValueAsDouble(value);
}

Expand Down
72 changes: 69 additions & 3 deletions src/test/java/org/datadog/jmxfetch/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,17 @@ public void testApp() throws Exception {
App.doIteration(config);
LinkedList<HashMap<String, Object>> metrics = ((ConsoleReporter) config.reporter).getMetrics();

assertEquals(metrics.size(), 13); // 13 = 7 metrics from java.lang + the 2 gauges we are explicitly collecting + the 4 gauges that is implicitly collected, see jmx.yaml in the test/resources folder
assertEquals(metrics.size(), 16); // 16 = 7 metrics from java.lang + the 5 gauges we are explicitly collecting + the 4 gauges that is implicitly collected, see jmx.yaml in the test/resources folder

// We test for the presence and the value of the metrics we want to collect
boolean metric_100_present = false;
boolean atomic_int_present = false;
boolean atomic_long_present = false;
boolean object_present = false;
boolean metric_1000_present = false;
boolean converted_present = false;
boolean boolean_present = false;
boolean default_present = false;
boolean counter_absent = true;
boolean subattr_0_present = false;
boolean subattr_counter_absent = true;
Expand All @@ -115,6 +118,24 @@ else if (name.equals("jmx.org.datadog.jmxfetch.test.should_be1000")) {
metric_1000_present = true;
}

else if (name.equals("test.converted")) {
assertEquals(tags.length, 3);
assertEquals(value, 5.0);
converted_present = true;
}

else if (name.equals("test.boolean")) {
assertEquals(tags.length, 3);
assertEquals(value, 1.0);
boolean_present = true;
}

else if (name.equals("test.defaulted")) {
assertEquals(tags.length, 3);
assertEquals(value, 32.0);
default_present = true;
}

else if (m.get("name").equals("test.counter")) {
counter_absent = false;

Expand Down Expand Up @@ -147,6 +168,9 @@ else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) {

assertTrue(metric_100_present);
assertTrue(metric_1000_present);
assertTrue(boolean_present);
assertTrue(converted_present);
assertTrue(default_present);
assertTrue(counter_absent);
assertTrue(subattr_0_present);
assertTrue(subattr_counter_absent);
Expand All @@ -157,14 +181,17 @@ else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) {
// We run a second collection. The counter should now be present
App.doIteration(config);
metrics = ((ConsoleReporter) config.reporter).getMetrics();
assertEquals(metrics.size(), 15); // 12 = 7 metrics from java.lang + the 2 gauges we are explicitly collecting + 4 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder
assertEquals(metrics.size(), 18); // 18 = 7 metrics from java.lang + the 5 gauges we are explicitly collecting + 4 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder

// We test for the same metrics but this time, the counter should be here
metric_100_present = false;
atomic_int_present = false;
atomic_long_present = false;
object_present = false;
metric_1000_present = false;
boolean_present = false;
converted_present = false;
default_present = false;
counter_absent = true;

for (HashMap<String, Object> m : metrics) {
Expand Down Expand Up @@ -199,6 +226,21 @@ else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) {
assertEquals(tags.length, 3);
assertEquals(value, 0.0); // We didn't increment the counter, hence a value of 0.0 is what we want
subattr_counter_absent = false;

} else if(name.equals("test.boolean")) {
assertEquals(tags.length, 3);
assertEquals(value, 1.0);
boolean_present = true;

} else if(name.equals("test.converted")) {
assertEquals(tags.length, 3);
assertEquals(value, 5.0);
converted_present = true;

} else if(name.equals("test.defaulted")) {
assertEquals(tags.length, 3);
assertEquals(value, 32.0); // We didn't increment the counter, hence a value of 0.0 is what we want
default_present = true;
}

else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic42")) {
Expand All @@ -221,6 +263,8 @@ else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) {

assertTrue(metric_100_present);
assertTrue(metric_1000_present);
assertTrue(boolean_present);
assertTrue(converted_present);
assertFalse(counter_absent);
assertTrue(subattr_0_present);
assertFalse(subattr_counter_absent);
Expand All @@ -236,13 +280,16 @@ else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) {

App.doIteration(config);
metrics = ((ConsoleReporter) config.reporter).getMetrics();
assertEquals(metrics.size(), 15); // 15 = 7 metrics from java.lang + the 2 gauges we are explicitly collecting + 4 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder
assertEquals(metrics.size(), 18); // 18 = 7 metrics from java.lang + the 5 gauges we are explicitly collecting + 4 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder

metric_100_present = false;
metric_1000_present = false;
atomic_int_present = false;
atomic_long_present = false;
object_present = false;
boolean_present = false;
converted_present = false;
default_present = false;

counter_absent = true;
HashMap<String, Integer> jvm_metrics = new HashMap<String, Integer>();
Expand Down Expand Up @@ -279,6 +326,21 @@ else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) {
assertEquals(value, 0.0);
subattr_0_present = true;

} else if (name.equals("test.boolean")) {
assertEquals(tags.length, 3);
assertEquals(value, 1.0);
boolean_present = true;

} else if (name.equals("test.converted")) {
assertEquals(tags.length, 3);
assertEquals(value, 5.0);
converted_present = true;

} else if (name.equals("test.defaulted")) {
assertEquals(tags.length, 3);
assertEquals(value, 32.0);
default_present = true;

} else if(name.equals("subattr.counter")) {
assertEquals(tags.length, 3);
// The value should be a bit less than 1.0, as we incremented the counter by 5 and we slept for 5 seconds
Expand Down Expand Up @@ -313,6 +375,10 @@ else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) {

assertTrue(metric_100_present);
assertTrue(metric_1000_present);
assertTrue(boolean_present);
assertTrue(converted_present);
assertTrue(default_present);
assertTrue(metric_1000_present);
assertFalse(counter_absent);
assertTrue(subattr_0_present);
assertFalse(subattr_counter_absent);
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class SimpleTestJavaApp implements SimpleTestJavaAppMBean {
private int should_be_100 = 100;
private int should_be_1000 = 1000;
private int should_be_counter = 0;
private String should_be_converted = "ShouldBe5";
private String should_be_defaulted = "DefaultMe";
private boolean should_be_boolean = true;
private HashMap<String, Integer> hashmap = new HashMap<String, Integer>();;
private AtomicInteger atomic42 = new AtomicInteger(42);
private AtomicLong atomic4242 = new AtomicLong(4242);
Expand All @@ -30,7 +33,19 @@ public Double getShouldBe1000() {
public int getShouldBeCounter() {
return should_be_counter;
}

public String getShouldBeConverted() {
return should_be_converted;
}

public String getShouldBeDefaulted() {
return should_be_defaulted;
}

public boolean getShouldBeBoolean() {
return should_be_boolean;
}

public void incrementCounter(int inc) {
should_be_counter += inc;
}
Expand All @@ -54,4 +69,3 @@ public Object getObject1337() {


}

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public interface SimpleTestJavaAppMBean {
int getShouldBe100();
Double getShouldBe1000();
int getShouldBeCounter();
String getShouldBeConverted();
String getShouldBeDefaulted();
boolean getShouldBeBoolean();
HashMap<String, Integer> getHashmap();
AtomicInteger getAtomic42();
AtomicLong getAtomic4242();
Expand Down
15 changes: 14 additions & 1 deletion src/test/resources/jmx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@ instances:
ShouldBeCounter:
metric_type: counter
alias: test.counter
ShouldBeBoolean:
metric_type: gauge
alias: test.boolean
Hashmap.thisis0:
metric_type: gauge
alias: subattr.this.is.0
Hashmap.thisiscounter:
metric_type: counter
alias: subattr.counter
ShouldBeConverted:
metric_type: gauge
alias: test.converted
values:
ShouldBe0: 0
ShouldBe5: 5
ShouldBeDefaulted:
metric_type: gauge
alias: test.defaulted
values:
default: 32
- include:
domain: org.datadog.jmxfetch.test

0 comments on commit 6c62322

Please sign in to comment.