diff --git a/pom.xml b/pom.xml index 1995b2e..6f93231 100644 --- a/pom.xml +++ b/pom.xml @@ -2,25 +2,17 @@ 4.0.0 - studyblue + com.bealetech metrics-statsd Metrics Statsd Support - 2.1.3.0 + 2.3.0.1 jar - 2.1.3 - 1.6.4 + 2.2.0 + 1.7.10 - - - Sean Laurent - organicveggie@gmail.com - -6 - - - Apache License 2.0 @@ -29,34 +21,6 @@ - - scm:git:git://github.com/organicveggie/metrics-statsd.git - scm:git:git@github.com:organicveggie/metrics-statsd.git - http://github.com/organicveggie/metrics-statsd/ - - - - github - http://github.com/organicveggie/metrics-statsd/issues#issue/ - - - - - central - http://oss.sonatype.org/content/repositories/releases - - false - - - - snapshots - http://oss.sonatype.org/content/repositories/snapshots - - true - - - - com.yammer.metrics @@ -86,13 +50,13 @@ junit junit-dep - 4.10 + 4.11 test org.mockito mockito-all - 1.9.0 + 1.9.5 test @@ -102,30 +66,31 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.2 + 3.2 - 1.6 - 1.6 + 1.7 + 1.7 org.apache.felix maven-bundle-plugin - 2.3.7 + 2.5.3 true org.apache.maven.plugins maven-surefire-plugin - 2.8.1 + 2.18.1 - classes + 3 + true org.apache.maven.plugins maven-source-plugin - 2.1.2 + 2.4 attach-sources @@ -138,7 +103,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.8.1 + 2.10.1 attach-javadocs @@ -151,7 +116,7 @@ org.apache.maven.plugins maven-release-plugin - 2.2.1 + 2.5.1 true forked-path diff --git a/src/main/java/com/studyblue/metrics/reporting/StatsdReporter.java b/src/main/java/com/bealetech/metrics/reporting/StatsdReporter.java similarity index 87% rename from src/main/java/com/studyblue/metrics/reporting/StatsdReporter.java rename to src/main/java/com/bealetech/metrics/reporting/StatsdReporter.java index d58d3d3..eb457d0 100644 --- a/src/main/java/com/studyblue/metrics/reporting/StatsdReporter.java +++ b/src/main/java/com/bealetech/metrics/reporting/StatsdReporter.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.studyblue.metrics.reporting; +package com.bealetech.metrics.reporting; import com.yammer.metrics.Metrics; import com.yammer.metrics.core.*; @@ -25,7 +25,7 @@ import java.io.*; import java.net.DatagramPacket; import java.net.DatagramSocket; -import java.net.InetSocketAddress; +import java.net.InetAddress; import java.util.Locale; import java.util.Map; import java.util.SortedMap; @@ -33,6 +33,15 @@ public class StatsdReporter extends AbstractPollingReporter implements MetricProcessor { + /* + MTU size of the host handling the PDU (most of the case it will be 1500) - + size of the IP header (20 bytes) - + size of UDP header (8 bytes) + + 1500 MTU - 20 IP hdr - 8 UDP hdr = 1472 bytes + */ + private static final int MAX_UDPDATAGRAM_LENGTH = 1472; + public static enum StatType { COUNTER, TIMER, GAUGE } private static final Logger LOG = LoggerFactory.getLogger(StatsdReporter.class); @@ -41,11 +50,16 @@ public static enum StatType { COUNTER, TIMER, GAUGE } protected final MetricPredicate predicate; protected final Locale locale = Locale.US; protected final Clock clock; + protected final UDPSocketProvider socketProvider; + protected DatagramSocket currentSocket = null; + protected final VirtualMachineMetrics vm; + protected Writer writer; protected ByteArrayOutputStream outputData; + private boolean prependNewline = false; private boolean printVMMetrics = true; public interface UDPSocketProvider { @@ -78,7 +92,7 @@ public StatsdReporter(MetricsRegistry metricsRegistry, String prefix, MetricPred } public StatsdReporter(MetricsRegistry metricsRegistry, String prefix, MetricPredicate predicate, UDPSocketProvider socketProvider, Clock clock, VirtualMachineMetrics vm) throws IOException { - this(metricsRegistry, prefix, predicate, socketProvider, clock, vm, "graphite-reporter"); + this(metricsRegistry, prefix, predicate, socketProvider, clock, vm, "statsd-reporter"); } public StatsdReporter(MetricsRegistry metricsRegistry, String prefix, MetricPredicate predicate, UDPSocketProvider socketProvider, Clock clock, VirtualMachineMetrics vm, String name) throws IOException { @@ -109,28 +123,24 @@ public void setPrintVMMetrics(boolean printVMMetrics) { @Override public void run() { - DatagramSocket socket = null; + try { - socket = this.socketProvider.get(); - outputData.reset(); - writer = new BufferedWriter(new OutputStreamWriter(this.outputData)); + currentSocket = this.socketProvider.get(); + resetWriterState(); final long epoch = clock.time() / 1000; - if (this.printVMMetrics) { + if (printVMMetrics) { printVmMetrics(epoch); } printRegularMetrics(epoch); // Send UDP data - writer.flush(); - DatagramPacket packet = this.socketProvider.newPacket(outputData); - packet.setData(outputData.toByteArray()); - socket.send(packet); + sendDatagram(); } catch (Exception e) { if (LOG.isDebugEnabled()) { - LOG.debug("Error writing to Graphite", e); + LOG.debug("Error writing to Statsd", e); } else { - LOG.warn("Error writing to Graphite: {}", e.getMessage()); + LOG.warn("Error writing to Statsd: {}", e.getMessage()); } if (writer != null) { try { @@ -140,13 +150,28 @@ public void run() { } } } finally { - if (socket != null) { - socket.close(); + if (currentSocket != null) { + currentSocket.close(); } writer = null; } } + private void resetWriterState() { + outputData.reset(); + prependNewline = false; + writer = new BufferedWriter(new OutputStreamWriter(this.outputData)); + } + + private void sendDatagram() throws IOException { + writer.flush(); + if (outputData.size() > 0) { // Don't send an empty datagram + DatagramPacket packet = this.socketProvider.newPacket(outputData); + packet.setData(outputData.toByteArray()); + currentSocket.send(packet); + } + } + protected void printVmMetrics(long epoch) { // Memory sendFloat("jvm.memory.totalInit", StatType.GAUGE, vm.totalInit()); @@ -304,8 +329,11 @@ protected void sendData(String name, String value, StatType statType) { statTypeStr = "ms"; break; } - + try { + if (prependNewline) { + writer.write("\n"); + } if (!prefix.isEmpty()) { writer.write(prefix); } @@ -314,10 +342,16 @@ protected void sendData(String name, String value, StatType statType) { writer.write(value); writer.write("|"); writer.write(statTypeStr); - writer.write('\n'); + prependNewline = true; writer.flush(); + + if (outputData.size() > MAX_UDPDATAGRAM_LENGTH) { + // Need to send our UDP packet now before it gets too big. + sendDatagram(); + resetWriterState(); + } } catch (IOException e) { - LOG.error("Error sending to Graphite:", e); + LOG.error("Error sending to Statsd:", e); } } @@ -333,19 +367,25 @@ public DefaultSocketProvider(String host, int port) { @Override public DatagramSocket get() throws Exception { - return new DatagramSocket(new InetSocketAddress(this.host, this.port)); + return new DatagramSocket(); } - + @Override public DatagramPacket newPacket(ByteArrayOutputStream out) { byte[] dataBuffer; + if (out != null) { dataBuffer = out.toByteArray(); } else { dataBuffer = new byte[8192]; } - return new DatagramPacket(dataBuffer, dataBuffer.length); + + try { + return new DatagramPacket(dataBuffer, dataBuffer.length, InetAddress.getByName(this.host), this.port); + } catch (Exception e) { + return null; + } } } } diff --git a/src/test/java/com/studyblue/metrics/reporting/StatsdReporterTest.java b/src/test/java/com/bealetech/metrics/reporting/StatsdReporterTest.java similarity index 99% rename from src/test/java/com/studyblue/metrics/reporting/StatsdReporterTest.java rename to src/test/java/com/bealetech/metrics/reporting/StatsdReporterTest.java index 5e55da2..fd61497 100644 --- a/src/test/java/com/studyblue/metrics/reporting/StatsdReporterTest.java +++ b/src/test/java/com/bealetech/metrics/reporting/StatsdReporterTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.studyblue.metrics.reporting; +package com.bealetech.metrics.reporting; import com.yammer.metrics.core.*; import com.yammer.metrics.reporting.AbstractPollingReporter; @@ -33,7 +33,6 @@ import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; public class StatsdReporterTest {