diff --git a/java/com/appfirst/AFTransport.java b/java/com/appfirst/AFTransport.java deleted file mode 100644 index d72609c..0000000 --- a/java/com/appfirst/AFTransport.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.appfirst; - -import java.util.HashMap; -import java.util.Map; - -import com.sun.jna.Library; -import com.sun.jna.Native; - -public class AFTransport implements Transport { - private static String AFCAPIName = "/afcollectorapi"; - private static String LibName = "rt"; - private static int O_WRONLY = 01; - private static int AFCMaxMsgSize = 2048; - private static int AFCSeverityStatsd = 3; - - public AFTransport() { - } - - private UDPTransport _udptransport = null; - - public Transport getUDPTransport(){ - if (_udptransport == null){ - try { - _udptransport = new UDPTransport(); - } catch (Exception e) { - } - } - return _udptransport; - } - - @Override - public boolean doSend(String stat) { - // trim msg if over allowed size - if (stat.length() > AFCMaxMsgSize) { - stat = stat.substring(0, AFCMaxMsgSize); - } - - MQ mq = (MQ) Native.loadLibrary(LibName, MQ.class); - int mqd = mq.mq_open(AFCAPIName, O_WRONLY); - int rv = mq.mq_send(mqd, stat, stat.length(), AFCSeverityStatsd); - mq.mq_close(mqd); - - if (AFCReturnCode.valueOf(rv) == AFCReturnCode.AFCSuccess) { - return true; - } else { - Transport udp = this.getUDPTransport(); - if (udp != null){ - return udp.doSend(stat); - } else { - return false; - } - } - } - - public interface MQ extends Library { - public int mq_open(String filename, int mode); - public int mq_close(int mqd); - public int mq_send(int mqd, String msg, int len, int prio); - } - - public enum AFCReturnCode{ - AFCSuccess(0), - AFCNoMemory(1), - AFCBadParam(2), - AFCOpenError(3), - AFCPostError(4), - AFCWouldBlock(5), - AFCCloseError(6); - - private int code; - - public int code(){ - return code; - } - - private AFCReturnCode(int code){ - this.code = code; - } - - private static Map map = null; - - public static AFCReturnCode valueOf(int code) - { - synchronized(AFCReturnCode.class) { - if (map == null) { - map = new HashMap(); - for (AFCReturnCode v : values()) { - map.put(v.code, v); - } - } - } - - AFCReturnCode result = map.get(code); - return result==null ? AFCReturnCode.AFCSuccess : result; - } - } - - public static void main(String[] args){ - Transport transport = new AFTransport(); - StatsdClient client = new StatsdClient(transport); - client.gauge("gauge", 123); - client.increment(2, .5, "counter", "counter2"); - client.increment("counter"); - client.decrement(1, 0.99, "counter"); - client.decrement(-2, "counter"); - client.timing("timing", 500); - client.timing("timing", 488, 1.0, "hello"); - } -} diff --git a/java/com/appfirst/StatsdClient.java b/java/com/appfirst/StatsdClient.java deleted file mode 100644 index c0802e0..0000000 --- a/java/com/appfirst/StatsdClient.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.appfirst; - -/** - * StatsdClient.java - * - * (C) 2011 Meetup, Inc. - * Author: Andrew Gwozdziewycz , @apgwoz - * - * - * - * Example usage: - * - * StatsdClient client = new StatsdClient("statsd.example.com", 8125); - * // increment by 1 - * client.increment("foo.bar.baz"); - * // increment by 10 - * client.increment("foo.bar.baz", 10); - * // sample rate - * client.increment("foo.bar.baz", 10, .1); - * // increment multiple keys by 1 - * client.increment("foo.bar.baz", "foo.bar.boo", "foo.baz.bar"); - * // increment multiple keys by 10 -- yeah, it's "backwards" - * client.increment(10, "foo.bar.baz", "foo.bar.boo", "foo.baz.bar"); - * // multiple keys with a sample rate - * client.increment(10, .1, "foo.bar.baz", "foo.bar.boo", "foo.baz.bar"); - * - * Note: For best results, and greater availability, you'll probably want to - * create a wrapper class which creates a static client and proxies to it. - * - * You know... the "Java way." - */ - -import java.io.IOException; -import java.net.UnknownHostException; -import java.util.Date; -import java.util.Random; - -public class StatsdClient { - private static Random RNG = new Random(); - - private Transport _transport; - - public StatsdClient() throws UnknownHostException, IOException{ - _transport = new UDPTransport(); - } - - public StatsdClient(Transport transport){ - _transport = transport; - } - - public void setTransport(Transport transport){ - _transport = transport; - } - - public boolean gauge(String key, int value) { - return gauge(key, value, null); - } - - public boolean gauge(String key, int value, String message) { - String stat = buildMessage(key, value, "g", new Date().getTime(), message); - return send(stat, 1); - } - - public boolean timing(String key, int value) { - return timing(key, value, 1.0, null); - } - - public boolean timing(String key, int value, String message) { - return timing(key, value, 1.0, message); - } - - public boolean timing(String key, int value, double sampleRate) { - return timing(key, value, sampleRate, null); - } - - public boolean timing(String key, int value, double sampleRate, String message) { - String stat = buildMessage(key, value, "ms", sampleRate, message); - return send(stat, sampleRate); - } - - public boolean decrement(String... keys) { - return increment(-1, 1.0, keys); - } - - public boolean decrement(int magnitude, String... keys) { - return decrement(magnitude, 1.0, keys); - } - - public boolean decrement(int magnitude, double sampleRate, String... keys) { - magnitude = magnitude < 0 ? magnitude : -magnitude; - return increment(magnitude, sampleRate, keys); - } - - public boolean increment(String... keys) { - return increment(1, 1.0, keys); - } - - public boolean increment(int magnitude, String... keys) { - return increment(magnitude, 1.0, keys); - } - - public boolean increment(int magnitude, double sampleRate, String... keys) { - return update_stats(null, magnitude, sampleRate, keys); - } - - public boolean update_stats(String message, int magnitude, double sampleRate, String... buckets){ - boolean result = true; - for (int i = 0; i < buckets.length; i++) { - String stat = buildMessage(buckets[i], magnitude, "c", sampleRate, message); - result = result && send(stat, sampleRate); - } - return result; - } - - private String buildMessage(String bucket, int magnitude, String type, double sampleRate, String message){ - String field2 = ""; - if (sampleRate < 1) { - field2 = String.format("@%f", sampleRate); - } - return buildMessage(bucket, magnitude, type, field2, message); - } - - private String buildMessage(String bucket, int magnitude, String type, long timestamp, String message){ - String field2 = String.valueOf(timestamp); - return buildMessage(bucket, magnitude, type, field2, message); - } - - private String buildMessage(String bucket, int magnitude, String type, String field2, String message){ - // bucket: field0 | field1 | field2 | field3 - // bucket: value | type | sampele_rate/timestamp | message - String stat = String.format("%s:%d|%s", bucket, magnitude, type); - // when message is there, we always keep field2 even if it's blank: - // bucket:2|c||some_message - if (message != null && !message.equals("")){ - stat += String.format("|%s|%s", field2, message); - } else if (!field2.equals("")){ - stat += String.format("|%s", field2); - } - - return stat; - } - - private boolean send(String stat, double sampleRate) { - if (sampleRate < 1.0 && RNG.nextDouble() > sampleRate) - return false; - else { - return this._transport.doSend(stat); - } - } -} \ No newline at end of file diff --git a/java/com/appfirst/Transport.java b/java/com/appfirst/Transport.java deleted file mode 100644 index 340a7aa..0000000 --- a/java/com/appfirst/Transport.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.appfirst; - -public interface Transport{ - public boolean doSend(final String stat); -} \ No newline at end of file diff --git a/java/com/appfirst/UDPTransport.java b/java/com/appfirst/UDPTransport.java deleted file mode 100644 index ff7c386..0000000 --- a/java/com/appfirst/UDPTransport.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.appfirst; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.UnknownHostException; -import java.nio.ByteBuffer; -import java.nio.channels.DatagramChannel; - -import org.apache.log4j.Logger; - - -public class UDPTransport implements Transport{ - private static Logger log = Logger.getLogger(StatsdClient.class.getName()); - - private InetSocketAddress _address; - private DatagramChannel _channel; - - public UDPTransport() throws UnknownHostException, IOException{ - this(InetAddress.getLocalHost(), 8125); - } - - public UDPTransport(String host, int port) throws UnknownHostException, IOException { - this(InetAddress.getByName(host), port); - } - - public UDPTransport(InetAddress host, int port) throws IOException { - this._address = new InetSocketAddress(host, port); - this._channel = DatagramChannel.open(); - } - - @Override - public boolean doSend(final String stat) { - try { - final byte[] data = stat.getBytes("utf-8"); - final ByteBuffer buff = ByteBuffer.wrap(data); - final int nbSentBytes = _channel.send(buff, _address); - - if (data.length == nbSentBytes) { - return true; - } else { - log.error(String.format( - "Could not send entirely stat %s to host %s:%d. Only sent %i bytes out of %i bytes", stat, - _address.getHostName(), _address.getPort(), nbSentBytes, data.length)); - return false; - } - - } catch (IOException e) { - log.error( - String.format("Could not send stat %s to host %s:%d", stat, _address.getHostName(), - _address.getPort()), e); - return false; - } - } -}