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

[attribute] add TabularData support #128

Merged
merged 7 commits into from
Mar 3, 2017
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added unit tests for TabularData/JMXMultiAttribute
brothhaar authored and yannmh committed Mar 3, 2017
commit f7f3a965ccd9e3066b661ba936f003373815ccbf
58 changes: 57 additions & 1 deletion src/test/java/org/datadog/jmxfetch/SimpleTestJavaApp.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package org.datadog.jmxfetch;

import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenDataException;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
@@ -30,12 +39,18 @@ public class SimpleTestJavaApp implements SimpleTestJavaAppMBean {
private final HashMap<String, Integer> hashmap = new HashMap<String, Integer>();
private final Object object1337 = new Double(13.37);
private final BigDecimal numberBig = new BigDecimal(123456788901234567890.0);

private final TabularData tabulardata;
private final CompositeType compositetype;

SimpleTestJavaApp() {
hashmap.put("thisis0", 0);
hashmap.put("thisis10", 10);
hashmap.put("thisiscounter", 0);
compositetype = buildCompositeType();
tabulardata = buildTabularType();
if (tabulardata != null) {
tabulardata.put(buildCompositeData(1));
}
}

public int getShouldBe100() {
@@ -111,4 +126,45 @@ public float getPrimitiveFloat(){
public Float getInstanceFloat(){
return instanceFloat;
}

private TabularData buildTabularType() {
try {
CompositeType rowType = buildCompositeType();
TabularType tabularType = new TabularType("myTabularType", "My tabular type", rowType,
new String[]{"thisiskey"});
return new TabularDataSupport(tabularType);
} catch (OpenDataException e) {
return null;
}
}

private CompositeType buildCompositeType() {
try {
return new CompositeType("myCompositeType", "My composite type",
new String[]{"thisiskey", "thisisx"},
new String[]{"This is key", "This is x"},
new OpenType[]{SimpleType.STRING, SimpleType.INTEGER});
} catch (OpenDataException e) {
return null;
}
}

private CompositeData buildCompositeData(Integer i) {
try {
return new CompositeDataSupport(compositetype,
new String[]{"thisiskey", "thisisx"},
new Object[]{i.toString(), i});
} catch (OpenDataException e) {
return null;
}
}

public void populateTabularData(int count) {
tabulardata.clear();
for (Integer i = 1; i <= count; i++) {
tabulardata.put(buildCompositeData(i));
}
}

public TabularData getTabulardata() { return tabulardata; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.datadog.jmxfetch;

import javax.management.openmbean.TabularData;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
@@ -36,4 +37,5 @@ public interface SimpleTestJavaAppMBean {

Float getInstanceFloat();

TabularData getTabulardata();
}
22 changes: 19 additions & 3 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
@@ -437,7 +438,10 @@ public void testApp() throws Exception {
run();
LinkedList<HashMap<String, Object>> metrics = getMetrics();

assertEquals(27, metrics.size()); // 27 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + the 9 gauges that is implicitly collected, see jmx.yaml in the test/resources folder
// 28 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 9 gauges implicitly collected
// + 1 multi-value, see jmx.yaml in the test/resources folder
assertEquals(28, metrics.size());


// We test for the presence and the value of the metrics we want to collect
List<String> commonTags = Arrays.asList(
@@ -459,12 +463,17 @@ public void testApp() throws Exception {
assertMetric("jmx.org.datadog.jmxfetch.test.object1337", 13.37, commonTags, 5);
assertMetric("jmx.org.datadog.jmxfetch.test.primitive_float", 123.4f, commonTags, 5);
assertMetric("jmx.org.datadog.jmxfetch.test.instance_float", 567.8f, commonTags, 5);
assertMetric("multiattr.this.is.x", 1.0, commonTags, Arrays.asList("KeyName:1"), 6);

assertCoverage();

// We run a second collection. The counter should now be present
run();
metrics = getMetrics();
assertEquals(29, metrics.size()); // 29 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 9 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder
// 30 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 9 gauges implicitly collected
// + 1 multi-value + 2 counter, see jmx.yaml in the test/resources folder
assertEquals(30, metrics.size());


// We test for the same metrics but this time, the counter should be here
// Previous metrics
@@ -482,6 +491,7 @@ public void testApp() throws Exception {
assertMetric("jmx.org.datadog.jmxfetch.test.object1337", 13.37, commonTags, 5);
assertMetric("jmx.org.datadog.jmxfetch.test.primitive_float", 123.4f, commonTags, 5);
assertMetric("jmx.org.datadog.jmxfetch.test.instance_float", 567.8f, commonTags, 5);
assertMetric("multiattr.this.is.x", 1.0, commonTags, Arrays.asList("KeyName:1"), 6);

// Counters
assertMetric("subattr.counter", 0.0, commonTags, 5);
@@ -492,10 +502,14 @@ public void testApp() throws Exception {
Thread.sleep(5000);
testApp.incrementCounter(5);
testApp.incrementHashMapCounter(5);
testApp.populateTabularData(2);

run();
metrics = getMetrics();
assertEquals(metrics.size(), 29); // 28 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 9 gauges implicitly collected + 2 counter, see jmx.yaml in the test/resources folder
// 31 = 13 metrics from java.lang + the 5 gauges we are explicitly collecting + 9 gauges implicitly collected
// + 2 multi-value + 2 counter, see jmx.yaml in the test/resources folder
assertEquals(31, metrics.size());


// Previous metrics
assertMetric("this.is.100", 100.0, commonTags, 8);
@@ -512,6 +526,8 @@ public void testApp() throws Exception {
assertMetric("jmx.org.datadog.jmxfetch.test.object1337", 13.37, commonTags, 5);
assertMetric("jmx.org.datadog.jmxfetch.test.primitive_float", 123.4f, commonTags, 5);
assertMetric("jmx.org.datadog.jmxfetch.test.instance_float", 567.8f, commonTags, 5);
assertMultiMetric("multiattr.this.is.x", 1.0, commonTags, Arrays.asList("KeyName:1"), 6, "KeyName:1");
assertMultiMetric("multiattr.this.is.x", 2.0, commonTags, Arrays.asList("KeyName:2"), 6, "KeyName:2");

// Counter
assertMetric("subattr.counter", 0.98, 1, commonTags, 5);
19 changes: 12 additions & 7 deletions src/test/java/org/datadog/jmxfetch/TestCommon.java
Original file line number Diff line number Diff line change
@@ -181,16 +181,17 @@ protected LinkedList<HashMap<String, Object>> getServiceChecks(){
*
* @return fail if the metric was not found
*/
public void assertMetric(String name, Number value, Number lowerBound, Number upperBound, List<String> commonTags, List<String> additionalTags, int countTags, String metricType){
public void assertMetric(String name, Number value, Number lowerBound, Number upperBound, List<String> commonTags, List<String> additionalTags, int countTags, String metricType, String keyTag){
List<String> tags = new ArrayList<String>(commonTags);
tags.addAll(additionalTags);

for (HashMap<String, Object> m: metrics) {
String mName = (String) (m.get("name"));
Double mValue = (Double) (m.get("value"));
Set<String> mTags = new HashSet<String>(Arrays.asList((String[]) (m.get("tags"))));
boolean keyMatch = keyTag == null || mTags.contains(keyTag);

if (mName.equals(name)) {
if (mName.equals(name) && keyMatch) {

if (!value.equals(-1)){
assertEquals((Double)value.doubleValue(), mValue);
@@ -219,22 +220,26 @@ public void assertMetric(String name, Number value, Number lowerBound, Number up
}

public void assertMetric(String name, Number value, Number lowerBound, Number upperBound, List<String> commonTags, List<String> additionalTags, int countTags) {
assertMetric(name, value, lowerBound, upperBound, commonTags, additionalTags, countTags, null);
assertMetric(name, value, lowerBound, upperBound, commonTags, additionalTags, countTags, null, null);
}

public void assertMetric(String name, Number value, List<String> commonTags, List<String> additionalTags, int countTags){
assertMetric(name, value, -1, -1, commonTags, additionalTags, countTags);
assertMetric(name, value, -1, -1, commonTags, additionalTags, countTags, null, null);
}

public void assertMultiMetric(String name, Number value, List<String> commonTags, List<String> additionalTags, int countTags, String keyTag){
assertMetric(name, value, -1, -1, commonTags, additionalTags, countTags, null, keyTag);
}

public void assertMetric(String name, Number value, List<String> commonTags, List<String> additionalTags, int countTags, String metricType){
assertMetric(name, value, -1, -1, commonTags, additionalTags, countTags, metricType);
assertMetric(name, value, -1, -1, commonTags, additionalTags, countTags, metricType, null);
}

public void assertMetric(String name, Number lowerBound, Number upperBound, List<String> commonTags, List<String> additionalTags, int countTags){
assertMetric(name, -1, lowerBound, upperBound, commonTags, additionalTags, countTags);
assertMetric(name, -1, lowerBound, upperBound, commonTags, additionalTags, countTags, null, null);
}
public void assertMetric(String name, Number lowerBound, Number upperBound, List<String> commonTags, List<String> additionalTags, int countTags, String metricType){
assertMetric(name, -1, lowerBound, upperBound, commonTags, additionalTags, countTags, metricType);
assertMetric(name, -1, lowerBound, upperBound, commonTags, additionalTags, countTags, metricType, null);
}

public void assertMetric(String name, Number value, List<String> tags, int countTags){
6 changes: 6 additions & 0 deletions src/test/resources/jmx.yaml
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ init_config:

instances:
- process_name_regex: .*surefire.*
refresh_beans: 4
name: jmx_test_instance
tags:
env: stage
@@ -40,5 +41,10 @@ instances:
alias: test.defaulted
values:
default: 32
Tabulardata.thisisx:
metric_type: gauge
alias: multiattr.this.is.x
tags:
KeyName: $key
- include:
domain: org.datadog.jmxfetch.test