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

Sanitise invalid characters in metric names #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist: trusty
language: java
jdk:
- oraclejdk8
Expand Down
54 changes: 35 additions & 19 deletions src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;


/**
Expand Down Expand Up @@ -116,6 +117,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("_");
gzussa marked this conversation as resolved.
Show resolved Hide resolved
}

private final String prefix;
private final DatagramChannel clientChannel;
private final StatsDClientErrorHandler handler;
Expand Down Expand Up @@ -753,7 +760,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(aspect, tags, Long.toString(delta), "|c");
}

/**
Expand All @@ -764,7 +771,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(aspect, tags, Long.toString(delta), "|c|@", SAMPLE_RATE_FORMATTERS.get().format(sampleRate));
}

/**
Expand All @@ -781,7 +788,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(aspect, tags, NUMBER_FORMATTERS.get().format(delta), "|c");
}

/**
Expand All @@ -792,7 +799,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(aspect, tags, NUMBER_FORMATTERS.get().format(delta), "|c|@", SAMPLE_RATE_FORMATTERS.get().format(sampleRate));
}

/**
Expand Down Expand Up @@ -889,7 +896,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(aspect, tags, NUMBER_FORMATTERS.get().format(value), "|g");
}

/**
Expand All @@ -900,7 +907,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(aspect, tags, NUMBER_FORMATTERS.get().format(value), "|g|@", SAMPLE_RATE_FORMATTERS.get().format(sampleRate));
}

/**
Expand Down Expand Up @@ -934,7 +941,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(aspect, tags, Long.toString(value), "|g");
}

/**
Expand All @@ -945,7 +952,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(aspect, tags, Long.toString(value), "|g|@", SAMPLE_RATE_FORMATTERS.get().format(sampleRate));
}

/**
Expand Down Expand Up @@ -978,7 +985,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(aspect, tags, Long.toString(timeInMs), "|ms");
}

/**
Expand All @@ -989,7 +996,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(aspect, tags, Long.toString(timeInMs), "|ms|@", SAMPLE_RATE_FORMATTERS.get().format(sampleRate));
}

/**
Expand Down Expand Up @@ -1024,7 +1031,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(aspect, tags, NUMBER_FORMATTERS.get().format(value), "|h");
}

/**
Expand All @@ -1037,7 +1044,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(aspect, tags, NUMBER_FORMATTERS.get().format(value), "|h|@", SAMPLE_RATE_FORMATTERS.get().format(sampleRate));
}

/**
Expand Down Expand Up @@ -1070,7 +1077,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(aspect, tags, Long.toString(value), "|h");
}

/**
Expand All @@ -1081,7 +1088,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(aspect, tags, Long.toString(value), "|h|@", SAMPLE_RATE_FORMATTERS.get().format(sampleRate));
}

/**
Expand Down Expand Up @@ -1118,7 +1125,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(aspect, tags, NUMBER_FORMATTERS.get().format(value), "|d");
}

/**
Expand All @@ -1131,7 +1138,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(aspect, tags, NUMBER_FORMATTERS.get().format(value), "|d|@", SAMPLE_RATE_FORMATTERS.get().format(sampleRate));
}

/**
Expand Down Expand Up @@ -1165,7 +1172,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(aspect, tags, Long.toString(value), "|d");
}

/**
Expand All @@ -1176,7 +1183,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(aspect, tags, Long.toString(value), "|d|@", SAMPLE_RATE_FORMATTERS.get().format(sampleRate));
}

/**
Expand Down Expand Up @@ -1380,7 +1387,16 @@ 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 aspect, final String[] tags, final String... data) {
final StringBuilder builder = new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":");
for (final String d : data) {
builder.append(d);
}
builder.append(tagString(tags));
send(builder.toString());
}

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