Skip to content

Commit

Permalink
add testing, cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
arbll committed Dec 12, 2018
1 parent 5f67942 commit a58f488
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 deletions.
20 changes: 10 additions & 10 deletions src/main/java/org/datadog/jmxfetch/reporter/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,26 @@ public void sendMetrics(LinkedList<HashMap<String, Object>> metrics, String inst
// StatsD doesn't support rate metrics so we need to have our own aggregator to compute rates
if ("gauge".equals(metricType) || "histogram".equals(metricType)) {
sendMetricPoint(metricType, metricName, currentValue, tags);
} else if ("gauge_delta".equals(metricType)) {
} else if ("count".equals(metricType)) {
String key = generateId(metric);
if (!instanceCountersAggregator.containsKey(key)) {
instanceCountersAggregator.put(key, currentValue.longValue());
continue;
}

long oldValue = instanceCountersAggregator.get(key);
long delta = currentValue.longValue() - oldValue ;
long oldValue = instanceCountersAggregator.get(key);
long delta = currentValue.longValue() - oldValue;

boolean sane = (!Double.isNaN(delta) && !Double.isInfinite(delta));
boolean submit = (delta >= 0 || !canonicalRate);
if(Double.isNaN(delta) || Double.isInfinite(delta)) {
continue;
}

if (sane && submit) {
sendMetricPoint(metricType, metricName, delta, tags);
} else if (sane) {
LOGGER.info("Canonical rate option set, and negative delta (counter reset) - not submitting.");
if (delta < 0 && canonicalRate) {
LOGGER.debug("Counter " + metricName + " has been reset and canonical rate is enabled - not submitting.");
continue;
}

sendMetricPoint(metricType, metricName, delta, tags);
instanceCountersAggregator.put(key, currentValue.longValue());
} else { // The metric should be 'counter'
String key = generateId(metric);
Expand All @@ -113,7 +114,6 @@ public void sendMetrics(LinkedList<HashMap<String, Object>> metrics, String inst
instanceRatesAggregator.put(key, rateInfo);
continue;
}
LOGGER.info("I SHOULD NOT BE HERE");

long oldTs = (Long) instanceRatesAggregator.get(key).get("ts");
double oldValue = (Double) instanceRatesAggregator.get(key).get(VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void sendMetricPoint(String metricType, String metricName, double valu
this.statsDClient.stop();
init();
}
if (metricType.equals("gauge_delta")) {
if (metricType.equals("count")) {
statsDClient.count(metricName, (long) value, tags);
} else if (metricType.equals("histogram")) {
statsDClient.histogram(metricName, value, tags);
Expand Down
53 changes: 48 additions & 5 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +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.LinkedList;
import java.util.List;
import java.util.*;

import org.junit.Test;

Expand Down Expand Up @@ -715,6 +711,53 @@ public void testAppCanonicalRate() throws Exception {
assertCoverage();
}

/**
* Test counts.
*
*/
@Test
public void testAppCount() throws Exception {
// We expose a few metrics through JMX
SimpleTestJavaApp testApp = new SimpleTestJavaApp();
registerMBean( testApp, "org.datadog.jmxfetch.test:type=SimpleTestJavaApp");

initApplication("jmx_count.yaml");

// First collection should not contain our count
run();
metrics = getMetrics();
assertEquals(13, metrics.size());

// Since our count is still equal to 0, we should report a delta equal to 0
run();
metrics = getMetrics();
assertEquals(14, metrics.size());
assertMetric("test.counter", 0, Collections.<String>emptyList(), 3);

// For the 3rd collection we increment the count to 5 so we should get a +5 delta
testApp.incrementCounter(5);
run();
metrics = getMetrics();
assertEquals(14, metrics.size());
assertMetric("test.counter", 5, Collections.<String>emptyList(), 3);

// For the 4th collection we decrement the count by 8 so we should get a -8 delta
testApp.decrementCounter(8);
run();
metrics = getMetrics();
assertEquals(14, metrics.size());
assertMetric("test.counter", -8, Collections.<String>emptyList(), 3);

// For the 5th collection we increment the count by 3 so we should get a +3 delta
testApp.incrementCounter(3);
run();
metrics = getMetrics();
assertEquals(14, metrics.size());
assertMetric("test.counter", 3, Collections.<String>emptyList(), 3);

assertCoverage();
}

/**
* Test JMX Service Discovery.
*
Expand Down
13 changes: 13 additions & 0 deletions src/test/resources/jmx_count.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
init_config:

instances:
- process_name_regex: .*surefire.*
refresh_beans: 4
name: jmx_test_instance
conf:
- include:
domain: org.datadog.jmxfetch.test
attribute:
ShouldBeCounter:
metric_type: count
alias: test.counter

0 comments on commit a58f488

Please sign in to comment.