Skip to content

Commit

Permalink
Sanitise invalid characters in metric names
Browse files Browse the repository at this point in the history
resolves DataDog#27
  • Loading branch information
ben-healthforge committed Sep 10, 2018
1 parent 54c2de0 commit fe3372e
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixDatagramChannel;
import jnr.unixsocket.UnixSocketOptions;
Expand Down Expand Up @@ -106,6 +107,12 @@ protected NumberFormat initialValue() {
}
};

private static final Pattern invalidAspectChar = Pattern.compile("[^.0-9A-Za-z_]");

private static String sanitiseAspect(String aspect) {
return invalidAspectChar.matcher(aspect).replaceAll("_");
}

private final String prefix;
private final DatagramChannel clientChannel;
private final StatsDClientErrorHandler handler;
Expand Down Expand Up @@ -427,7 +434,7 @@ String tagString(final String[] tags) {
*/
@Override
public void count(final String aspect, final long delta, final String... tags) {
send(new StringBuilder(prefix).append(aspect).append(":").append(delta).append("|c").append(tagString(tags)).toString());
send(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(delta).append("|c").append(tagString(tags)).toString());
}

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

/**
Expand All @@ -455,7 +462,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(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(delta)).append("|c").append(tagString(tags)).toString());
send(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(delta)).append("|c").append(tagString(tags)).toString());
}

/**
Expand All @@ -466,7 +473,7 @@ public void count(final String aspect, final double delta, final double sampleRa
if(isInvalidSample(sampleRate)) {
return;
}
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());
send(new StringBuilder(prefix).append(sanitiseAspect(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 @@ -563,7 +570,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(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|g").append(tagString(tags)).toString());
send(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|g").append(tagString(tags)).toString());
}

/**
Expand All @@ -574,7 +581,7 @@ public void recordGaugeValue(final String aspect, final double value, final doub
if(isInvalidSample(sampleRate)) {
return;
}
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());
send(new StringBuilder(prefix).append(sanitiseAspect(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 @@ -608,7 +615,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(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|g").append(tagString(tags)).toString());
send(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(value).append("|g").append(tagString(tags)).toString());
}

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

/**
Expand Down Expand Up @@ -652,7 +659,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(new StringBuilder(prefix).append(aspect).append(":").append(timeInMs).append("|ms").append(tagString(tags)).toString());
send(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(timeInMs).append("|ms").append(tagString(tags)).toString());
}

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

/**
Expand Down Expand Up @@ -698,7 +705,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(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|h").append(tagString(tags)).toString());
send(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|h").append(tagString(tags)).toString());
}

/**
Expand All @@ -711,7 +718,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(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());
send(new StringBuilder(prefix).append(sanitiseAspect(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 @@ -744,7 +751,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(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|h").append(tagString(tags)).toString());
send(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(value).append("|h").append(tagString(tags)).toString());
}

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

/**
Expand Down Expand Up @@ -792,7 +799,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(new StringBuilder(prefix).append(aspect).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|d").append(tagString(tags)).toString());
send(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|d").append(tagString(tags)).toString());
}

/**
Expand All @@ -805,7 +812,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(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());
send(new StringBuilder(prefix).append(sanitiseAspect(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 @@ -839,7 +846,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(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|d").append(tagString(tags)).toString());
send(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(value).append("|d").append(tagString(tags)).toString());
}

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

/**
Expand Down Expand Up @@ -990,7 +997,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(new StringBuilder(prefix).append(aspect).append(":").append(value).append("|s").append(tagString(tags)).toString());
send(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(value).append("|s").append(tagString(tags)).toString());
}

private void send(final String message) {
Expand Down
Loading

0 comments on commit fe3372e

Please sign in to comment.