Skip to content

Commit

Permalink
Merge pull request #85 from DataDog/olivielpeau/bind-to-custom-host
Browse files Browse the repository at this point in the history
[reporter][statsd] Allow configuration of statsd host
  • Loading branch information
olivielpeau committed Mar 22, 2016
2 parents b957b28 + 2aff1c2 commit 1cd5831
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/datadog/jmxfetch/reporter/ReporterFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.datadog.jmxfetch.reporter;

import com.google.common.base.Joiner;
import java.util.Arrays;

public class ReporterFactory {

public static Reporter getReporter(String type) {
Expand All @@ -9,7 +12,13 @@ public static Reporter getReporter(String type) {
if ("console".equals(type)) {
return new ConsoleReporter();
} else if (type.startsWith("statsd:")) {
return new StatsdReporter(Integer.valueOf(type.split(":")[1]));
String[] typeElements = type.split(":");
String host = "localhost";
Integer port = Integer.valueOf(typeElements[typeElements.length - 1]);
if (typeElements.length > 2) {
host = Joiner.on(":").join(Arrays.copyOfRange(typeElements, 1, typeElements.length - 1));
}
return new StatsdReporter(host, port);
} else {
throw new IllegalArgumentException("Invalid reporter type: " + type);
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/datadog/jmxfetch/reporter/StatsdReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
public class StatsdReporter extends Reporter {

private StatsDClient statsDClient;
private String statsdHost;
private int statsdPort;
private long initializationTime;

public StatsdReporter(int statsdPort) {
public StatsdReporter(String statsdHost, int statsdPort) {
this.statsdHost = statsdHost;
this.statsdPort = statsdPort;
this.init();
}

private void init() {
initializationTime = System.currentTimeMillis();
statsDClient = new NonBlockingStatsDClient(null, "localhost", this.statsdPort, new String[]{});
statsDClient = new NonBlockingStatsDClient(null, this.statsdHost, this.statsdPort, new String[]{});
}

protected void sendMetricPoint(String metricName, double value, String[] tags) {
Expand Down Expand Up @@ -71,6 +73,10 @@ public void displayInstanceName(Instance instance) {
throw new UnsupportedOperationException();
}

public String getStatsdHost() {
return statsdHost;
}

public int getStatsdPort() {
return statsdPort;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class ReporterValidator implements IParameterValidator {

public void validate(String name, String value) throws ParameterException {
if (value.startsWith(STATSD_PREFIX) && value.length() > STATSD_PREFIX.length()) {
String port = new String(value.split(":")[1]);
String[] splitValue = value.split(":");
String port = splitValue[splitValue.length - 1];
try {
positiveIntegerValidator.validate(name, port);
} catch (ParameterException pe) {
Expand All @@ -19,7 +20,7 @@ public void validate(String name, String value) throws ParameterException {
return;
}
if (!value.equals("console")) {
throw new ParameterException("Parameter " + name + " should be either 'console' or 'statsd:[STATSD_PORT]'");
throw new ParameterException("Parameter " + name + " should be either 'console', 'statsd:[STATSD_PORT]' or 'statsd:[STATSD_HOST]:[STATSD_PORT]'");
}
}
}
29 changes: 28 additions & 1 deletion src/test/java/org/datadog/jmxfetch/TestParsingJCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,33 @@ public void testParsingReporter() {
appConfig = testCommand(params);
assertNotNull(appConfig.getReporter());
assertTrue(appConfig.getReporter() instanceof StatsdReporter);
assertEquals("localhost", ((StatsdReporter) appConfig.getReporter()).getStatsdHost());
assertEquals(10, ((StatsdReporter) appConfig.getReporter()).getStatsdPort());

// statsd reporter with custom ipv4 host
params = new String[]{
"--reporter", "statsd:127.0.0.1:10",
"--check", SINGLE_CHECK,
"--conf_directory", CONF_DIR,
AppConfig.ACTION_COLLECT
};
appConfig = testCommand(params);
assertNotNull(appConfig.getReporter());
assertTrue(appConfig.getReporter() instanceof StatsdReporter);
assertEquals("127.0.0.1", ((StatsdReporter) appConfig.getReporter()).getStatsdHost());
assertEquals(10, ((StatsdReporter) appConfig.getReporter()).getStatsdPort());

// statsd reporter with custom ipv6 host
params = new String[]{
"--reporter", "statsd:[::1]:10",
"--check", SINGLE_CHECK,
"--conf_directory", CONF_DIR,
AppConfig.ACTION_COLLECT
};
appConfig = testCommand(params);
assertNotNull(appConfig.getReporter());
assertTrue(appConfig.getReporter() instanceof StatsdReporter);
assertEquals("[::1]", ((StatsdReporter) appConfig.getReporter()).getStatsdHost());
assertEquals(10, ((StatsdReporter) appConfig.getReporter()).getStatsdPort());

// invalid reporter
Expand All @@ -135,7 +162,7 @@ public void testParsingReporter() {
testCommand(params);
fail("Should have failed because reporter is invalid");
} catch (ParameterException pe) {
assertEquals("Parameter --reporter should be either 'console' or 'statsd:[STATSD_PORT]'", pe.getMessage());
assertEquals("Parameter --reporter should be either 'console', 'statsd:[STATSD_PORT]' or 'statsd:[STATSD_HOST]:[STATSD_PORT]'", pe.getMessage());
}

// invalid port
Expand Down

0 comments on commit 1cd5831

Please sign in to comment.