Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace String.format #49

Merged
merged 1 commit into from
May 17, 2018
Merged
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
69 changes: 43 additions & 26 deletions src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ protected NumberFormat initialValue() {
}
};

private static final ThreadLocal<NumberFormat> SAMPLE_RATE_FORMATTERS = new ThreadLocal<NumberFormat>() {
@Override
protected NumberFormat initialValue() {
final NumberFormat numberFormatter = NumberFormat.getInstance(Locale.US);
numberFormatter.setGroupingUsed(false);
numberFormatter.setMinimumFractionDigits(6);

if (numberFormatter instanceof DecimalFormat) {
final DecimalFormat decimalFormat = (DecimalFormat) numberFormatter;
final DecimalFormatSymbols symbols = decimalFormat.getDecimalFormatSymbols();
symbols.setNaN("NaN");
decimalFormat.setDecimalFormatSymbols(symbols);
}
return numberFormatter;
}
};

private final String prefix;
private final DatagramChannel clientChannel;
private final StatsDClientErrorHandler handler;
Expand Down Expand Up @@ -290,7 +307,7 @@ public NonBlockingStatsDClient(final String prefix, final String hostname, final
public NonBlockingStatsDClient(final String prefix, final int queueSize, String[] constantTags, final StatsDClientErrorHandler errorHandler,
final Callable<InetSocketAddress> addressLookup) throws StatsDClientException {
if((prefix != null) && (!prefix.isEmpty())) {
this.prefix = String.format("%s.", prefix);
this.prefix = new StringBuilder(prefix).append(".").toString();
} else {
this.prefix = "";
}
Expand Down Expand Up @@ -399,7 +416,7 @@ String tagString(final String[] tags) {
*/
@Override
public void count(final String aspect, final long delta, final String... tags) {
send(String.format("%s%s:%d|c%s", prefix, aspect, delta, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(delta).append("|c").append(tagString(tags)).toString());
}

/**
Expand All @@ -410,7 +427,7 @@ public void count(final String aspect, final long delta, final double sampleRate
if(isInvalidSample(sampleRate)) {
return;
}
send(String.format("%s%s:%d|c|@%f%s", prefix, aspect, delta, sampleRate, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(delta).append("|c|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString());
}

/**
Expand All @@ -427,7 +444,7 @@ public void count(final String aspect, final long delta, final double sampleRate
*/
@Override
public void count(final String aspect, final double delta, final String... tags) {
send(String.format("%s%s:%s|c%s", prefix, aspect, NUMBER_FORMATTERS.get().format(delta), tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(delta)).append("|c").append(tagString(tags)).toString());
}

/**
Expand All @@ -438,7 +455,7 @@ public void count(final String aspect, final double delta, final double sampleRa
if(isInvalidSample(sampleRate)) {
return;
}
send(String.format("%s%s:%s|c|@%f%s", prefix, aspect, NUMBER_FORMATTERS.get().format(delta), sampleRate, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(delta)).append("|c|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString());
}

/**
Expand Down Expand Up @@ -535,7 +552,7 @@ public void decrement(final String aspect, final double sampleRate, final String
public void recordGaugeValue(final String aspect, final double value, final String... tags) {
/* Intentionally using %s rather than %f here to avoid
* padding with extra 0s to represent precision */
send(String.format("%s%s:%s|g%s", prefix, aspect, NUMBER_FORMATTERS.get().format(value), tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|g").append(tagString(tags)).toString());
}

/**
Expand All @@ -546,7 +563,7 @@ public void recordGaugeValue(final String aspect, final double value, final doub
if(isInvalidSample(sampleRate)) {
return;
}
send(String.format("%s%s:%s|g|@%f%s", prefix, aspect, NUMBER_FORMATTERS.get().format(value), sampleRate, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|g|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString());
}

/**
Expand Down Expand Up @@ -580,7 +597,7 @@ public void gauge(final String aspect, final double value, final double sampleRa
*/
@Override
public void recordGaugeValue(final String aspect, final long value, final String... tags) {
send(String.format("%s%s:%d|g%s", prefix, aspect, value, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|g").append(tagString(tags)).toString());
}

/**
Expand All @@ -591,7 +608,7 @@ public void recordGaugeValue(final String aspect, final long value, final double
if(isInvalidSample(sampleRate)) {
return;
}
send(String.format("%s%s:%d|g|@%f%s", prefix, aspect, value, sampleRate, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|g|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString());
}

/**
Expand Down Expand Up @@ -624,7 +641,7 @@ public void gauge(final String aspect, final long value, final double sampleRate
*/
@Override
public void recordExecutionTime(final String aspect, final long timeInMs, final String... tags) {
send(String.format("%s%s:%d|ms%s", prefix, aspect, timeInMs, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(timeInMs).append("|ms").append(tagString(tags)).toString());
}

/**
Expand All @@ -635,7 +652,7 @@ public void recordExecutionTime(final String aspect, final long timeInMs, final
if(isInvalidSample(sampleRate)) {
return;
}
send(String.format("%s%s:%d|ms|@%f%s", prefix, aspect, timeInMs, sampleRate, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(timeInMs).append("|ms|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString());
}

/**
Expand Down Expand Up @@ -670,7 +687,7 @@ public void time(final String aspect, final long value, final double sampleRate,
public void recordHistogramValue(final String aspect, final double value, final String... tags) {
/* Intentionally using %s rather than %f here to avoid
* padding with extra 0s to represent precision */
send(String.format("%s%s:%s|h%s", prefix, aspect, NUMBER_FORMATTERS.get().format(value), tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|h").append(tagString(tags)).toString());
}

/**
Expand All @@ -683,7 +700,7 @@ public void recordHistogramValue(final String aspect, final double value, final
}
/* Intentionally using %s rather than %f here to avoid
* padding with extra 0s to represent precision */
send(String.format("%s%s:%s|h|@%f%s", prefix, aspect, NUMBER_FORMATTERS.get().format(value), sampleRate, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|h|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString());
}

/**
Expand Down Expand Up @@ -716,7 +733,7 @@ public void histogram(final String aspect, final double value, final double samp
*/
@Override
public void recordHistogramValue(final String aspect, final long value, final String... tags) {
send(String.format("%s%s:%d|h%s", prefix, aspect, value, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|h").append(tagString(tags)).toString());
}

/**
Expand All @@ -727,7 +744,7 @@ public void recordHistogramValue(final String aspect, final long value, final do
if(isInvalidSample(sampleRate)) {
return;
}
send(String.format("%s%s:%d|h|@%f%s", prefix, aspect, value, sampleRate, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|h|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString());
}

/**
Expand Down Expand Up @@ -764,7 +781,7 @@ public void histogram(final String aspect, final long value, final double sample
public void recordDistributionValue(final String aspect, final double value, final String... tags) {
/* Intentionally using %s rather than %f here to avoid
* padding with extra 0s to represent precision */
send(String.format("%s%s:%s|d%s", prefix, aspect, NUMBER_FORMATTERS.get().format(value), tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|d").append(tagString(tags)).toString());
}

/**
Expand All @@ -777,7 +794,7 @@ public void recordDistributionValue(final String aspect, final double value, fin
}
/* Intentionally using %s rather than %f here to avoid
* padding with extra 0s to represent precision */
send(String.format("%s%s:%s|d|@%f%s", prefix, aspect, NUMBER_FORMATTERS.get().format(value), sampleRate, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|d|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString());
}

/**
Expand Down Expand Up @@ -811,7 +828,7 @@ public void distribution(final String aspect, final double value, final double s
*/
@Override
public void recordDistributionValue(final String aspect, final long value, final String... tags) {
send(String.format("%s%s:%d|d%s", prefix, aspect, value, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|d").append(tagString(tags)).toString());
}

/**
Expand All @@ -822,7 +839,7 @@ public void recordDistributionValue(final String aspect, final long value, final
if(isInvalidSample(sampleRate)) {
return;
}
send(String.format("%s%s:%d|d|@%f%s", prefix, aspect, value, sampleRate, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|d|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString());
}

/**
Expand Down Expand Up @@ -890,8 +907,8 @@ private String eventMap(final Event event) {
public void recordEvent(final Event event, final String... tags) {
final String title = escapeEventString(prefix + event.getTitle());
final String text = escapeEventString(event.getText());
send(String.format("_e{%d,%d}:%s|%s%s%s",
title.length(), text.length(), title, text, eventMap(event), tagString(tags)));
send(new StringBuilder("_e{").append(title.length()).append(",").append(text.length()).append("}:").append(title)
.append("|").append(text).append(eventMap(event)).append(tagString(tags)).toString());
}

private String escapeEventString(final String title) {
Expand All @@ -916,16 +933,16 @@ public void recordServiceCheckRun(final ServiceCheck sc) {
private String toStatsDString(final ServiceCheck sc) {
// see http://docs.datadoghq.com/guides/dogstatsd/#service-checks
final StringBuilder sb = new StringBuilder();
sb.append(String.format("_sc|%s|%d", sc.getName(), sc.getStatus()));
sb.append("_sc|").append(sc.getName()).append("|").append(sc.getStatus());
if (sc.getTimestamp() > 0) {
sb.append(String.format("|d:%d", sc.getTimestamp()));
sb.append("|d:").append(sc.getTimestamp());
}
if (sc.getHostname() != null) {
sb.append(String.format("|h:%s", sc.getHostname()));
sb.append("|h:").append(sc.getHostname());
}
sb.append(tagString(sc.getTags()));
if (sc.getMessage() != null) {
sb.append(String.format("|m:%s", sc.getEscapedMessage()));
sb.append("|m:").append(sc.getEscapedMessage());
}
return sb.toString();
}
Expand Down Expand Up @@ -962,7 +979,7 @@ public void serviceCheck(final ServiceCheck sc) {
public void recordSetValue(final String aspect, final String value, final String... tags) {
// documentation is light, but looking at dogstatsd source, we can send string values
// here instead of numbers
send(String.format("%s%s:%s|s%s", prefix, aspect, value, tagString(tags)));
send(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|s").append(tagString(tags)).toString());
}

private void send(final String message) {
Expand Down