Skip to content

Commit

Permalink
Add support for booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
coupacooke committed Mar 7, 2014
1 parent 9aac3b5 commit 9fa26e6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 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");
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.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
4 changes: 3 additions & 1 deletion src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class JMXAttribute {
protected LinkedHashMap<Object,Object> valueConversions;
protected String[] tags;
protected Configuration matching_conf;
protected static final List<String> SIMPLE_TYPES = Arrays.asList("long", "java.lang.String", "int", "double");
protected static final List<String> SIMPLE_TYPES = Arrays.asList("long", "java.lang.String", "int", "double", "java.lang.Boolean");
protected static final List<String> COMPOSED_TYPES = Arrays.asList("javax.management.openmbean.CompositeData");
private static final String[] EXCLUDED_BEAN_PARAMS = {"domain", "bean_name", "bean", "attribute"};

Expand Down Expand Up @@ -151,6 +151,8 @@ protected double _getValueAsDouble(Object metricValue) {

} 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
30 changes: 26 additions & 4 deletions src/test/java/org/datadog/jmxfetch/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ public void testApp() throws Exception {
App.doIteration(config);
LinkedList<HashMap<String, Object>> metrics = ((ConsoleReporter) config.reporter).getMetrics();

assertEquals(metrics.size(), 12); // 11 = 7 metrics from java.lang + the 4 gauges we are explicitly collecting + the 1 gauge that is implicitly collected, see jmx.yaml in the test/resources folder
assertEquals(metrics.size(), 13); // 13 = 7 metrics from java.lang + the 5 gauges we are explicitly collecting + the 1 gauge 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 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;
Expand Down Expand Up @@ -120,6 +121,12 @@ else if (name.equals("test.converted")) {
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);
Expand All @@ -142,6 +149,7 @@ else if (m.get("name").equals("test.counter")) {

assertTrue(metric_100_present);
assertTrue(metric_1000_present);
assertTrue(boolean_present);
assertTrue(converted_present);
assertTrue(default_present);
assertTrue(counter_absent);
Expand All @@ -151,11 +159,12 @@ else if (m.get("name").equals("test.counter")) {
// We run a second collection. The counter should now be present
App.doIteration(config);
metrics = ((ConsoleReporter) config.reporter).getMetrics();
assertEquals(metrics.size(), 14); // 14 = 7 metrics from java.lang + the 4 gauges we are explicitly collecting + 1 gauge implicitly collected + 2 counter, see jmx.yaml in the test/resources folder
assertEquals(metrics.size(), 15); // 15 = 7 metrics from java.lang + the 5 gauges we are explicitly collecting + 1 gauge 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;
metric_1000_present = false;
boolean_present = false;
converted_present = false;
default_present = false;
counter_absent = true;
Expand Down Expand Up @@ -193,9 +202,14 @@ else if (m.get("name").equals("test.counter")) {
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); // We didn't increment the counter, hence a value of 0.0 is what we want
assertEquals(value, 5.0);
converted_present = true;

} else if(name.equals("test.defaulted")) {
Expand All @@ -208,6 +222,7 @@ else if (m.get("name").equals("test.counter")) {

assertTrue(metric_100_present);
assertTrue(metric_1000_present);
assertTrue(boolean_present);
assertTrue(converted_present);
assertFalse(counter_absent);
assertTrue(subattr_0_present);
Expand All @@ -221,10 +236,11 @@ else if (m.get("name").equals("test.counter")) {

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

metric_100_present = false;
metric_1000_present = false;
boolean_present = false;
converted_present = false;
default_present = false;
counter_absent = true;
Expand Down Expand Up @@ -262,6 +278,11 @@ else if (m.get("name").equals("test.counter")) {
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);
Expand Down Expand Up @@ -290,6 +311,7 @@ else if (m.get("name").equals("test.counter")) {

assertTrue(metric_100_present);
assertTrue(metric_1000_present);
assertTrue(boolean_present);
assertTrue(converted_present);
assertTrue(default_present);
assertTrue(metric_1000_present);
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class SimpleTestJavaApp implements SimpleTestJavaAppMBean {
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>();;

SimpleTestJavaApp() {
Expand Down Expand Up @@ -36,6 +37,10 @@ public String getShouldBeDefaulted() {
return should_be_defaulted;
}

public boolean getShouldBeBoolean() {
return should_be_boolean;
}

public void incrementCounter(int inc) {
should_be_counter += inc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface SimpleTestJavaAppMBean {
int getShouldBeCounter();
String getShouldBeConverted();
String getShouldBeDefaulted();
boolean getShouldBeBoolean();
HashMap<String, Integer> getHashmap();

}
3 changes: 3 additions & 0 deletions src/test/resources/jmx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ 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
Expand Down

0 comments on commit 9fa26e6

Please sign in to comment.