Skip to content

Commit

Permalink
Fix unicode length calculations in event strings
Browse files Browse the repository at this point in the history
Old code calculated the length of header and message body sent to statsd
using codepoints instead of raw UTF-8 lengths. The new code now ensures
that both of those field lengths are properly calculated using the
expected encoding.
  • Loading branch information
sgnn7 committed May 11, 2021
1 parent 1b8b20e commit d63ac45
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
Expand Down Expand Up @@ -102,6 +103,11 @@ String tag() {
public static final String CLIENT_TRANSPORT_TAG = "client_transport:";


/**
* UTF-8 is the expected encoding for data sent to the agent.
*/
public static final Charset UTF_8 = Charset.forName("UTF-8");

private static final StatsDClientErrorHandler NO_OP_HANDLER = new StatsDClientErrorHandler() {
@Override public void handle(final Exception ex) { /* No-op */ }
};
Expand Down Expand Up @@ -1691,9 +1697,9 @@ public void recordEvent(final Event event, final String... eventTags) {
final String text = escapeEventString(event.getText());
builder.append(Message.Type.EVENT.toString())
.append("{")
.append(title.length())
.append(getUtf8Length(title))
.append(",")
.append(text.length())
.append(getUtf8Length(text))
.append("}:")
.append(title)
.append("|").append(text);
Expand All @@ -1710,6 +1716,10 @@ private static String escapeEventString(final String title) {
return title.replace("\n", "\\n");
}

private int getUtf8Length(final String text) {
return text.getBytes(UTF_8).length;
}

/**
* Records a run status for the specified named service check.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,24 @@ public void sends_event() throws Exception {
client.recordEvent(event);
server.waitForMessage();

assertThat(server.messagesReceived(), hasItem(comparesEqualTo("_e{16,12}:my.prefix.title1|text1\\nline2|d:1234567|h:host1|k:key1|p:low|t:error|s:sourcetype1")));
assertThat(
server.messagesReceived(),
hasItem(comparesEqualTo("_e{16,12}:my.prefix.title1|text1\\nline2|d:1234567|h:host1|k:key1|p:low|t:error|s:sourcetype1"))
);
}

@Test(timeout = 5000L)
public void send_unicode_event() throws Exception {
final Event event = Event.builder()
.withTitle("Delivery - Daily Settlement Summary Report Delivery — Invoice Cloud succeeded")
.withText("Delivered — destination.csv").build();
assertEquals(event.getText(), "Delivered — destination.csv");
client.recordEvent(event);
server.waitForMessage();
assertThat(
server.messagesReceived(),
hasItem(comparesEqualTo("_e{89,29}:my.prefix.Delivery - Daily Settlement Summary Report Delivery — Invoice Cloud succeeded|Delivered — destination.csv"))
);
}

@Test(timeout = 5000L)
Expand Down

0 comments on commit d63ac45

Please sign in to comment.