Skip to content
This repository was archived by the owner on Nov 27, 2022. It is now read-only.
Open
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,10 +1,8 @@
package com.flozano.metrics.client.statsd;

import static java.util.Objects.requireNonNull;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -73,10 +71,7 @@ protected void encode(ChannelHandlerContext ctx, MetricValue[] msg,
} else {
assert false;
}

ByteCountingWriter writer = new ByteCountingWriter(buf);
MetricToBytesEncoder.toStringParts(m, writer);
currentBytes += writer.getWrittenBytes();
currentBytes += MetricToBytesEncoder.write(m, buf);
}

if (buf != null) {
Expand All @@ -85,26 +80,4 @@ protected void encode(ChannelHandlerContext ctx, MetricValue[] msg,
LOGGER.trace("Encoded {} metrics into {} different packets",
msg.length, out.size());
}

private static class ByteCountingWriter implements Consumer<String> {

private final ByteBuf outputBuffer;
private int writtenBytes = 0;

public ByteCountingWriter(ByteBuf outputBuffer) {
this.outputBuffer = requireNonNull(outputBuffer);
}

@Override
public void accept(String t) {
byte[] partBytes = t.getBytes(StandardCharsets.UTF_8);
outputBuffer.writeBytes(partBytes);
writtenBytes += partBytes.length;
}

public int getWrittenBytes() {
return writtenBytes;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.flozano.metrics.client.statsd;

import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -10,6 +9,7 @@
import com.flozano.metrics.client.MetricsClient;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
Expand All @@ -27,30 +27,31 @@ public boolean acceptOutboundMessage(Object msg) throws Exception {
@Override
protected void encode(ChannelHandlerContext ctx, MetricValue msg, ByteBuf out) throws Exception {
LOGGER.trace("Writing {} ", msg);
toStringParts(msg, (part) -> out.writeBytes(part.getBytes(StandardCharsets.UTF_8)));
write(msg, out);
LOGGER.trace("Wrote {} ", msg);
}

public static String toString(MetricValue msg) {
StringBuilder sb = new StringBuilder();
toStringParts(msg, sb::append);
return sb.toString();
ByteBuf buf = Unpooled.buffer(100);
int length = write(msg, buf);
return buf.readCharSequence(length, StandardCharsets.UTF_8).toString();
}

public static void toStringParts(MetricValue msg, Consumer<String> parts) {
parts.accept(msg.getName());
parts.accept(":");
public static int write(MetricValue msg, ByteBuf out) {
int start = out.writerIndex();
out.writeCharSequence(msg.getName(), StandardCharsets.UTF_8);
out.writeByte(':');
if (msg.isSignRequiredInValue() && msg.getValue() > 0) {
parts.accept("+");
out.writeByte('+');
}
parts.accept(Long.toString(msg.getValue()));
parts.accept("|");
parts.accept(msg.getCode());
out.writeCharSequence(Long.toString(msg.getValue()), StandardCharsets.US_ASCII);
out.writeByte('|');
out.writeCharSequence(msg.getCode(), StandardCharsets.US_ASCII);
Double r = msg.getSampleRate();
if (r != null) {
parts.accept("|@");
parts.accept(String.format("%1.2f", r));
out.writeCharSequence("|@", StandardCharsets.US_ASCII);
out.writeCharSequence(String.format("%1.2f", r), StandardCharsets.US_ASCII);
}
return out.writerIndex() - start;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public MetricToBytesEncoderTest(Class<? extends MetricValue> type, String name,
@Test
public void toStringPartsTest() {
MetricValue metricValue = newMetric();
StringBuilder sb = new StringBuilder();
MetricToBytesEncoder.toStringParts(metricValue, sb::append);
assertThat(sb.toString(), is(equalTo(expectedValue(metricValue.getCode()))));
assertThat(MetricToBytesEncoder.toString(metricValue), is(equalTo(expectedValue(metricValue.getCode()))));
}


Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Versions -->
<awaitility.version>1.7.0</awaitility.version>
<guava.version>19.0</guava.version>
<guava.version>21.0</guava.version>
<jacoco-maven-plugin.version>0.7.2.201409121644</jacoco-maven-plugin.version>
<java.version>1.8</java.version>
<javassist.version>3.20.0-GA</javassist.version>
Expand All @@ -57,7 +57,7 @@
<maven-source-plugin.version>2.4</maven-source-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<mockito.version>1.10.17</mockito.version>
<netty.version>4.1.0.CR2</netty.version>
<netty.version>4.1.12.Final</netty.version>
<slf4j.version>1.7.14</slf4j.version>
</properties>
<dependencyManagement>
Expand Down