Skip to content

Commit

Permalink
[activemq58] Proper prefix on service check
Browse files Browse the repository at this point in the history
I hacked the StatsDReporter to send the status of activemq.can_connect
instead of activemq_58.can_connect.

It's a bit better than a simple Hack. We decided that the prefix name
shouldn't contain any number, underscore or capital letter (should we
add the dash to this list ?). This way we can have version dependant
configuration in the .yaml config files for JMXFetch without having
multiple and dirty metrics and service check names in the HQ.

UNIT TEST SUITE UPDATE
----------------------

Added a simple unit test to the prefix formatter, when adding versionned
integration like activemq_58, it would be good to check that its prefix
is correctly formatted here.
  • Loading branch information
Etienne LAFARGE committed May 13, 2015
1 parent 56d092f commit 23f1ec9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<properties>
<commons-io.version>2.4</commons-io.version>
<commons-lang.version>2.6</commons-lang.version>
<guava.version>17.0</guava.version>
<java-dogstatsd-client.version>2.1.0</java-dogstatsd-client.version>
<jcommander.version>1.35</jcommander.version>
Expand Down Expand Up @@ -52,6 +53,11 @@
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>com.datadoghq</groupId>
<artifactId>java-dogstatsd-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ public LinkedList<HashMap<String, Object>> getMetrics() {
}

public void sendServiceCheck(String checkName, String status, String message, String hostname, String[] tags) {
String dataName = Reporter.formatDataName(checkName);
String tagString = "";
if (tags != null && tags.length > 0) {
tagString = "[" + Joiner.on(",").join(tags) + "]";
}
System.out.println(checkName + tagString + " - " + System.currentTimeMillis() / 1000 + " = " + status);
System.out.println(dataName + tagString + " - " + System.currentTimeMillis() / 1000 + " = " + status);

HashMap<String, Object> sc = new HashMap<String, Object>();
sc.put("name", checkName);
sc.put("name", dataName);
sc.put("status", status);
sc.put("message", message);
sc.put("hostname", hostname);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/datadog/jmxfetch/reporter/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.datadog.jmxfetch.App;
import org.datadog.jmxfetch.Instance;
import org.datadog.jmxfetch.JMXAttribute;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -106,6 +107,16 @@ private void postProcessCassandra(HashMap<String, Object> metric) {
metric.put("alias", ((String) metric.get("alias")).replace("jmx.org.apache.", ""));
}

public static String formatDataName(String name){
String[] chunks = name.split("\\.");

// Escaping the prefix name
chunks[0] = chunks[0].replaceAll("[A-Z0-9:_\\-]", "");

// Putting the chunks together
return StringUtils.join(chunks, ".");
}

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

public abstract void sendServiceCheck(String checkName, String status, String message, String hostname, String[] tags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public void sendServiceCheck(String checkName, String status, String message,
init();
}

ServiceCheck sc = new ServiceCheck(String.format("%s.can_connect", checkName),
this.statusToInt(status), message, hostname, tags);
String dataName = Reporter.formatDataName(String.format("%s.can_connect", checkName));
ServiceCheck sc = new ServiceCheck(dataName, this.statusToInt(status), message, hostname, tags);

statsDClient.serviceCheck(sc);
}

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.apache.log4j.Level;
import org.datadog.jmxfetch.reporter.ConsoleReporter;
import org.datadog.jmxfetch.reporter.Reporter;
import org.datadog.jmxfetch.util.CustomLogger;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -396,6 +397,20 @@ public void testServiceCheckCRITICAL() throws Exception {
mbs.unregisterMBean(objectName);
}

@Test
public void testPrefixFormatter() throws Exception {
// Let's get a list of Strings to test (add real versionned check names
// here when you add new versionned check)
String[][] data = {
{"activemq_58.foo.bar12", "activemq.foo.bar12"},
{"test_package-X86_64-VER1:0.weird.metric_name", "testpackage.weird.metric_name" }
};

// Let's test them all
for(int i=0; i<data.length; ++i)
assertEquals(data[i][1], Reporter.formatDataName(data[i][0]));
}

@Test
public void testApp() throws Exception {
// We expose a few metrics through JMX
Expand Down

0 comments on commit 23f1ec9

Please sign in to comment.