initial checkin
This commit is contained in:
commit
fc64f422b6
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="src" path="src/test/java"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||
<classpathentry kind="lib" path="vendor/buildlib/hamcrest-core-1.3.0RC2.jar"/>
|
||||
<classpathentry kind="lib" path="vendor/buildlib/hamcrest-library-1.3.0RC2.jar"/>
|
||||
<classpathentry kind="lib" path="vendor/buildlib/junit-dep-4.10.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
|
@ -0,0 +1,2 @@
|
|||
bin
|
||||
build
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>java-statsd-client</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,11 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
|
@ -0,0 +1,79 @@
|
|||
package com.timgroup.statsd;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public final class StatsDClient {
|
||||
|
||||
private final String prefix;
|
||||
private final DatagramSocket clientSocket;
|
||||
|
||||
public StatsDClient(String prefix, String hostname, int port) {
|
||||
this.prefix = prefix;
|
||||
|
||||
try {
|
||||
this.clientSocket = new DatagramSocket();
|
||||
this.clientSocket.connect(new InetSocketAddress(hostname, port));
|
||||
} catch (SocketException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
|
||||
final ThreadFactory delegate = Executors.defaultThreadFactory();
|
||||
@Override public Thread newThread(Runnable r) {
|
||||
Thread result = delegate.newThread(r);
|
||||
result.setName("StatsD-" + result.getName());
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private void send(final String message) {
|
||||
executor.execute(new Runnable() {
|
||||
@Override public void run() {
|
||||
blockingSend(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
try {
|
||||
executor.shutdown();
|
||||
executor.awaitTermination(30, TimeUnit.SECONDS);
|
||||
}
|
||||
catch (Exception e) { }
|
||||
finally {
|
||||
if (clientSocket != null) {
|
||||
clientSocket.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void blockingSend(String message) {
|
||||
try {
|
||||
final byte[] sendData = message.getBytes();
|
||||
final DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length);
|
||||
clientSocket.send(sendPacket);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void incrementCounter(String aspect) {
|
||||
send(String.format("%s.%s:%d|c", prefix, aspect, 1));
|
||||
}
|
||||
|
||||
public void recordGaugeValue(String aspect, int value) {
|
||||
send(String.format("%s.%s:%d|g", prefix, aspect, 1));
|
||||
}
|
||||
|
||||
public void recordExecutionTime(String aspect, int timeInMs) {
|
||||
send(String.format("%s.%s:%d|ms", prefix, aspect, 1));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.timgroup.statsd;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StatsDClientTest {
|
||||
|
||||
private static final int STATSD_SERVER_PORT = 17254;
|
||||
private final StatsDClient client = new StatsDClient("my.prefix", "localhost", STATSD_SERVER_PORT);
|
||||
|
||||
@After
|
||||
public void stop() throws Exception {
|
||||
client.stop();
|
||||
}
|
||||
|
||||
@Test(timeout=5000L) public void
|
||||
sends_records_to_statsd() throws Exception {
|
||||
final List<String> messagesReceived = new ArrayList<String>();
|
||||
final DatagramSocket server = new DatagramSocket(STATSD_SERVER_PORT);
|
||||
|
||||
new Thread(new Runnable() {
|
||||
@Override public void run() {
|
||||
try {
|
||||
final DatagramPacket packet = new DatagramPacket(new byte[256], 256);
|
||||
server.receive(packet);
|
||||
messagesReceived.add(new String(packet.getData()).trim());
|
||||
server.close();
|
||||
} catch (Exception e) { }
|
||||
}
|
||||
}).start();
|
||||
|
||||
client.incrementCounter("blah");
|
||||
while (messagesReceived.isEmpty()) { Thread.sleep(50L); }
|
||||
|
||||
assertThat(messagesReceived, contains("my.prefix.blah:1|c"));
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue