Skip to content

Commit

Permalink
Add support for AtomicInt, AtomicLong and Objects
Browse files Browse the repository at this point in the history
Fix #21 and Fix #25
  • Loading branch information
Remi Hakim committed Mar 11, 2014
1 parent 22859ae commit 9c7cac6
Show file tree
Hide file tree
Showing 5 changed files with 107 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.util.concurrent.atomic.AtomicInteger", "java.util.concurrent.atomic.AtomicLong", "java.lang.Object");
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
16 changes: 15 additions & 1 deletion src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
Expand Down Expand Up @@ -124,19 +126,31 @@ protected static String convertMetricName(String metricName) {
}

protected double _getValueAsDouble(Object value) {

if (value instanceof String) {
return Double.parseDouble((String)value);

} else if (value instanceof Integer) {
return new Double((Integer)(value));

} else if (value instanceof AtomicInteger) {
return new Double(((AtomicInteger)(value)).get());
} else if (value instanceof AtomicLong) {
Long l = ((AtomicLong)(value)).get();
return l.doubleValue();

} else if (value instanceof Double) {
return (Double)value;
} else if (value instanceof Long) {
Long l = new Long((Long) value);
return l.doubleValue();
} else {
throw new NumberFormatException();
try{
return new Double((Double) value);
} catch (Exception e) {
throw new NumberFormatException();
}

}

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

assertEquals(metrics.size(), 10); // 10 = 7 metrics from java.lang + the 2 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 2 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 counter_absent = true;
boolean subattr_0_present = false;
Expand Down Expand Up @@ -124,21 +127,43 @@ else if (m.get("name").equals("test.counter")) {
subattr_counter_absent = false;
}

else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic42")) {
assertEquals(tags.length, 3);
assertEquals(value, 42.0);
atomic_int_present = true;
}
else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic4242")) {
assertEquals(tags.length, 3);
assertEquals(value, 4242.0);
atomic_long_present = true;
}
else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) {
assertEquals(tags.length, 3);
assertEquals(value, 13.37);
object_present = true;
}

}

assertTrue(metric_100_present);
assertTrue(metric_1000_present);
assertTrue(counter_absent);
assertTrue(subattr_0_present);
assertTrue(subattr_counter_absent);
assertTrue(atomic_int_present);
assertTrue(atomic_long_present);
assertTrue(object_present);

// We run a second collection. The counter should now be present
App.doIteration(config);
metrics = ((ConsoleReporter) config.reporter).getMetrics();
assertEquals(metrics.size(), 12); // 12 = 7 metrics from java.lang + the 2 gauges we are explicitly collecting + 1 gauge implicitly collected + 2 counter, see jmx.yaml in the test/resources folder
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

// 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;
counter_absent = true;

Expand Down Expand Up @@ -176,13 +201,32 @@ else if (m.get("name").equals("test.counter")) {
subattr_counter_absent = false;
}

else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic42")) {
assertEquals(tags.length, 3);
assertEquals(value, 42.0);
atomic_int_present = true;
}
else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic4242")) {
assertEquals(tags.length, 3);
assertEquals(value, 4242.0);
atomic_long_present = true;
}
else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) {
assertEquals(tags.length, 3);
assertEquals(value, 13.37);
object_present = true;
}

}

assertTrue(metric_100_present);
assertTrue(metric_1000_present);
assertFalse(counter_absent);
assertTrue(subattr_0_present);
assertFalse(subattr_counter_absent);
assertTrue(atomic_int_present);
assertTrue(atomic_long_present);
assertTrue(object_present);


// We run a 3rd collection but this time we increment the counter and we sleep
Expand All @@ -192,10 +236,14 @@ else if (m.get("name").equals("test.counter")) {

App.doIteration(config);
metrics = ((ConsoleReporter) config.reporter).getMetrics();
assertEquals(metrics.size(), 12); // 12 = 7 metrics from java.lang + the 2 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 2 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;

counter_absent = true;
HashMap<String, Integer> jvm_metrics = new HashMap<String, Integer>();
jvm_metrics.put("jvm.gc.cms.count", 2);
Expand Down Expand Up @@ -237,6 +285,22 @@ else if (m.get("name").equals("test.counter")) {
assertTrue(value < 1.00);
assertTrue(value > 0.99);
subattr_counter_absent = false;
}
else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic42")) {
assertEquals(tags.length, 3);
assertEquals(value, 42.0);
atomic_int_present = true;
}
else if (name.equals("jmx.org.datadog.jmxfetch.test.atomic4242")) {
assertEquals(tags.length, 3);
assertEquals(value, 4242.0);
atomic_long_present = true;
}
else if (name.equals("jmx.org.datadog.jmxfetch.test.object1337")) {
assertEquals(tags.length, 3);
assertEquals(value, 13.37);
object_present = true;


} else {
// Those are jvm metrics
Expand All @@ -252,6 +316,10 @@ else if (m.get("name").equals("test.counter")) {
assertFalse(counter_absent);
assertTrue(subattr_0_present);
assertFalse(subattr_counter_absent);
assertTrue(atomic_int_present);
assertTrue(atomic_long_present);
assertTrue(object_present);


for (int i : jvm_metrics.values()) {
assertEquals(i, 0);
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
@@ -1,14 +1,19 @@
package org.datadog.jmxfetch;

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class SimpleTestJavaApp implements SimpleTestJavaAppMBean {

private int should_be_100 = 100;
private int should_be_1000 = 1000;
private int should_be_counter = 0;
private HashMap<String, Integer> hashmap = new HashMap<String, Integer>();;

private AtomicInteger atomic42 = new AtomicInteger(42);
private AtomicLong atomic4242 = new AtomicLong(4242);
private Object object1337 = new Double(13.37);

SimpleTestJavaApp() {
hashmap.put("thisis0", 0);
hashmap.put("thisis10", 10);
Expand Down Expand Up @@ -37,6 +42,15 @@ public void incrementHashMapCounter(int inc) {
public HashMap<String, Integer> getHashmap() {
return hashmap;
}
public AtomicInteger getAtomic42() {
return atomic42;
}
public AtomicLong getAtomic4242() {
return atomic4242;
}
public Object getObject1337() {
return object1337;
}


}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package org.datadog.jmxfetch;

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public interface SimpleTestJavaAppMBean {

int getShouldBe100();
Double getShouldBe1000();
int getShouldBeCounter();
HashMap<String, Integer> getHashmap();
AtomicInteger getAtomic42();
AtomicLong getAtomic4242();
Object getObject1337();

}

0 comments on commit 9c7cac6

Please sign in to comment.