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

Adding 'histogram' metric type to JMXFetch. #115

Merged
merged 1 commit into from
Dec 8, 2016
Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -14,14 +14,15 @@ public class ConsoleReporter extends Reporter {
private LinkedList<HashMap<String, Object>> serviceChecks = new LinkedList<HashMap<String, Object>>();

@Override
protected void sendMetricPoint(String metricName, double value, String[] tags) {
protected void sendMetricPoint(String metricType, String metricName, double value, String[] tags) {
String tagString = "[" + Joiner.on(",").join(tags) + "]";
System.out.println(metricName + tagString + " - " + System.currentTimeMillis() / 1000 + " = " + value);

HashMap<String, Object> m = new HashMap<String, Object>();
m.put("name", metricName);
m.put("value", value);
m.put("tags", tags);
m.put("type", metricType);
metrics.add(m);
}

10 changes: 5 additions & 5 deletions src/main/java/org/datadog/jmxfetch/reporter/Reporter.java
Original file line number Diff line number Diff line change
@@ -70,7 +70,9 @@ public void sendMetrics(LinkedList<HashMap<String, Object>> metrics, String inst
String[] tags = Arrays.asList((String[]) metric.get("tags")).toArray(new String[0]);

// StatsD doesn't support rate metrics so we need to have our own aggregator to compute rates
if (!"gauge".equals(metricType)) {
if ("gauge".equals(metricType) || "histogram".equals(metricType)) {
sendMetricPoint(metricType, metricName, currentValue, tags);
} else { // The metric should be 'counter'
String key = generateId(metric);
if (!instanceRatesAggregator.containsKey(key)) {
HashMap<String, Object> rateInfo = new HashMap<String, Object>();
@@ -87,13 +89,11 @@ public void sendMetrics(LinkedList<HashMap<String, Object>> metrics, String inst
double rate = 1000 * (currentValue - oldValue) / (now - oldTs);

if (!Double.isNaN(rate) && !Double.isInfinite(rate)) {
sendMetricPoint(metricName, rate, tags);
sendMetricPoint(metricType, metricName, rate, tags);
}

instanceRatesAggregator.get(key).put("ts", now);
instanceRatesAggregator.get(key).put(VALUE, currentValue);
} else { // The metric is a gauge
sendMetricPoint(metricName, currentValue, tags);
}
}

@@ -131,7 +131,7 @@ public static String formatServiceCheckPrefix(String fullname){
return StringUtils.join(chunks, ".");
}

protected abstract void sendMetricPoint(String metricName, double value, String[] tags);
protected abstract void sendMetricPoint(String metricType, String metricName, double value, String[] tags);

protected abstract void doSendServiceCheck(String checkName, String status, String message, String[] tags);

Original file line number Diff line number Diff line change
@@ -26,12 +26,16 @@ private void init() {
statsDClient = new NonBlockingStatsDClient(null, this.statsdHost, this.statsdPort, new String[]{});
}

protected void sendMetricPoint(String metricName, double value, String[] tags) {
protected void sendMetricPoint(String metricType, String metricName, double value, String[] tags) {
if (System.currentTimeMillis() - this.initializationTime > 300 * 1000) {
this.statsDClient.stop();
init();
}
statsDClient.gauge(metricName, value, tags);
if (metricType.equals("histogram")) {
statsDClient.histogram(metricName, value, tags);
} else {
statsDClient.gauge(metricName, value, tags);
}
}

private int statusToInt(String status) {
37 changes: 37 additions & 0 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
@@ -329,6 +329,43 @@ public void testExitWatcher() throws Exception {
assertFalse(exitWatcher.shouldExit());
}

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

// We do a first collection
initApplication("jmx_histogram.yaml");

run();
LinkedList<HashMap<String, Object>> metrics = getMetrics();

// We test for the presence and the value of the metrics we want to collect
List<String> commonTags = Arrays.asList(
"instance:jmx_test_instance",
"env:stage",
"newTag:test");

// 15 = 13 metrics from java.lang + the 3 collected (gauge and histogram)
assertEquals(16, metrics.size());

assertMetric("test.gauge", 1000.0, commonTags, 5, "gauge");
assertMetric("test.gauge_by_default", 42.0, commonTags, 5, "gauge");
assertMetric("test.histogram", 424242, commonTags, 5, "histogram");

// We run a second collection. The counter should now be present
run();
metrics = getMetrics();

// 16 = 13 metrics from java.lang + the 4 collected (gauge, histogram and counter)
assertEquals(17, metrics.size());
assertMetric("test.gauge", 1000.0, commonTags, 5, "gauge");
assertMetric("test.gauge_by_default", 42.0, commonTags, 5, "gauge");
assertMetric("test.histogram", 424242, commonTags, 5, "histogram");
assertMetric("test.counter", 0.0, commonTags, 5, "counter");
}

/**
* FIXME: Split this test in multiple sub-tests.
*/
31 changes: 30 additions & 1 deletion src/test/java/org/datadog/jmxfetch/TestCommon.java
Original file line number Diff line number Diff line change
@@ -177,9 +177,11 @@ protected LinkedList<HashMap<String, Object>> getServiceChecks(){
*
* @param countTags number of metric tags
*
* @param metricType type of the metric (gauge, histogram, ...)
*
* @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){
public void assertMetric(String name, Number value, Number lowerBound, Number upperBound, List<String> commonTags, List<String> additionalTags, int countTags, String metricType){
List<String> tags = new ArrayList<String>(commonTags);
tags.addAll(additionalTags);

@@ -203,6 +205,10 @@ public void assertMetric(String name, Number value, Number lowerBound, Number up
for (String t: tags) {
assertTrue(mTags.contains(t));
}

if (metricType != null) {
assertEquals(metricType, m.get("type"));
}
// Brand the metric
m.put("tested", true);

@@ -212,26 +218,49 @@ public void assertMetric(String name, Number value, Number lowerBound, Number up
fail("Metric assertion failed (name: "+name+", value: "+value+", tags: "+tags+", #tags: "+countTags+").");
}

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);
}

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

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);
}

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);
}
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);
}

public void assertMetric(String name, Number value, List<String> tags, int countTags){
assertMetric(name, value, tags, new ArrayList<String>(), countTags);
}

public void assertMetric(String name, Number value, List<String> tags, int countTags, String metricType){
assertMetric(name, value, tags, new ArrayList<String>(), countTags, metricType);
}

public void assertMetric(String name, Number lowerBound, Number upperBound, List<String> tags, int countTags){
assertMetric(name, lowerBound, upperBound, tags, new ArrayList<String>(), countTags);
}

public void assertMetric(String name, Number lowerBound, Number upperBound, List<String> tags, int countTags, String metricType){
assertMetric(name, lowerBound, upperBound, tags, new ArrayList<String>(), countTags, metricType);
}

public void assertMetric(String name, List<String> tags, int countTags){
assertMetric(name, -1, tags, new ArrayList<String>(), countTags);
}

public void assertMetric(String name, List<String> tags, int countTags, String metricType){
assertMetric(name, -1, tags, new ArrayList<String>(), countTags, metricType);
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍰

* Assert that all -excluding JVM related- metrics were tested.
*
23 changes: 23 additions & 0 deletions src/test/resources/jmx_histogram.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
init_config:

instances:
- process_name_regex: .*surefire.*
name: jmx_test_instance
tags:
env: stage
newTag: test
conf:
- include:
domain: org.datadog.jmxfetch.test
attribute:
Atomic42:
alias: test.gauge_by_default
ShouldBe100:
metric_type: counter
alias: test.counter
ShouldBe1000:
metric_type: gauge
alias: test.gauge
Int424242:
metric_type: histogram
alias: test.histogram