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

[reporter][statsd] Allow configuration of statsd host #85

Merged
merged 1 commit into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
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
@@ -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
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