From 4cd18d4fdd154c6e36277506efe42388a7568869 Mon Sep 17 00:00:00 2001 From: Ben Spencer Date: Mon, 10 Sep 2018 13:57:26 +0100 Subject: [PATCH 1/3] Sanitise invalid characters in metric names resolves DataDog/java-dogstatsd-client#27 --- .../statsd/NonBlockingStatsDClient.java | 45 +++-- .../statsd/NonBlockingStatsDClientTest.java | 178 +++++++++++++++++- 2 files changed, 199 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java b/src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java index 73bc9a91..c6af4763 100644 --- a/src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java +++ b/src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java @@ -21,6 +21,7 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; /** @@ -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("_"); + } + private final String prefix; private final DatagramChannel clientChannel; private final StatsDClientErrorHandler handler; @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(delta).append("|c").append(tagString(tags)).toString()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(delta).append("|c|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(delta)).append("|c").append(tagString(tags)).toString()); } /** @@ -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(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()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|g").append(tagString(tags)).toString()); } /** @@ -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(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()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(value).append("|g").append(tagString(tags)).toString()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(value).append("|g|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(timeInMs).append("|ms").append(tagString(tags)).toString()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(timeInMs).append("|ms|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|h").append(tagString(tags)).toString()); } /** @@ -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(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()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(value).append("|h").append(tagString(tags)).toString()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(value).append("|h|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|d").append(tagString(tags)).toString()); } /** @@ -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(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()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(value).append("|d").append(tagString(tags)).toString()); } /** @@ -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(new StringBuilder(prefix).append(sanitiseAspect(aspect)).append(":").append(value).append("|d|@").append(SAMPLE_RATE_FORMATTERS.get().format(sampleRate)).append(tagString(tags)).toString()); } /** @@ -1380,7 +1387,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) { diff --git a/src/test/java/com/timgroup/statsd/NonBlockingStatsDClientTest.java b/src/test/java/com/timgroup/statsd/NonBlockingStatsDClientTest.java index 1738920b..2204228e 100644 --- a/src/test/java/com/timgroup/statsd/NonBlockingStatsDClientTest.java +++ b/src/test/java/com/timgroup/statsd/NonBlockingStatsDClientTest.java @@ -173,10 +173,44 @@ public void sends_counter_decrement_with_sample_rate_to_statsd_with_tags() throw assertThat(server.messagesReceived(), contains("my.prefix.mydec:-1|c|@1.000000|#baz,foo:bar")); } - @Test(timeout = 5000L) - public void sends_gauge_to_statsd() throws Exception { + @Test(timeout=5000L) + public void sanitises_counter_name() throws Exception { + + client.count("Foo:bar:baz.123", 1); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|c")); + } + + @Test(timeout=5000L) + public void sanitises_counter_name_with_sample_rate() throws Exception { + + client.count("Foo:bar:baz.123", 1, 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|c|@1.000000")); + } + + @Test(timeout=5000L) + public void sanitises_counter_name_with_delta() throws Exception { + client.count("Foo:bar:baz.123", -1d); + server.waitForMessage(); + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:-1|c")); + } + + @Test(timeout=5000L) + public void sanitises_counter_name_with_delta_and_sample_rate() throws Exception { + + client.count("Foo:bar:baz.123", -1d, 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:-1|c|@1.000000")); + } + + @Test(timeout=5000L) + public void sends_gauge_to_statsd() throws Exception { client.recordGaugeValue("mygauge", 423); server.waitForMessage(); @@ -253,7 +287,43 @@ public void sends_double_gauge_to_statsd_with_tags() throws Exception { assertThat(server.messagesReceived(), contains("my.prefix.mygauge:0.423|g|#baz,foo:bar")); } - @Test(timeout = 5000L) + @Test(timeout=5000L) + public void sanitises_gauge_name_with_double() throws Exception { + + client.gauge("Foo:bar:baz.123", 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|g")); + } + + @Test(timeout=5000L) + public void sanitises_gauge_name_with_double_and_sample_rate() throws Exception { + + client.gauge("Foo:bar:baz.123", 1d, 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|g|@1.000000")); + } + + @Test(timeout=5000L) + public void sanitises_gauge_name_with_long() throws Exception { + + client.gauge("Foo:bar:baz.123", 1L); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|g")); + } + + @Test(timeout=5000L) + public void sanitises_gauge_name_with_long_and_sample_rate() throws Exception { + + client.gauge("Foo:bar:baz.123", 1L, 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|g|@1.000000")); + } + + @Test(timeout=5000L) public void sends_histogram_to_statsd() throws Exception { client.recordHistogramValue("myhistogram", 423); @@ -312,7 +382,43 @@ public void sends_double_histogram_with_sample_rate_to_statsd_with_tags() throws assertThat(server.messagesReceived(), contains("my.prefix.myhistogram:0.423|h|@1.000000|#baz,foo:bar")); } - @Test(timeout = 5000L) + @Test(timeout=5000L) + public void sanitises_histogram_name_with_double() throws Exception { + + client.histogram("Foo:bar:baz.123", 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|h")); + } + + @Test(timeout=5000L) + public void sanitises_histogram_name_with_double_and_sample_rate() throws Exception { + + client.histogram("Foo:bar:baz.123", 1d, 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|h|@1.000000")); + } + + @Test(timeout=5000L) + public void sanitises_histogram_name_with_long() throws Exception { + + client.histogram("Foo:bar:baz.123", 1L); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|h")); + } + + @Test(timeout=5000L) + public void sanitises_histogram_name_with_long_and_sample_rate() throws Exception { + + client.histogram("Foo:bar:baz.123", 1L, 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|h|@1.000000")); + } + + @Test(timeout=5000L) public void sends_distribtuion_to_statsd() throws Exception { client.recordDistributionValue("mydistribution", 423); @@ -371,7 +477,43 @@ public void sends_double_distribution_with_sample_rate_to_statsd_with_tags() thr assertThat(server.messagesReceived(), contains("my.prefix.mydistribution:0.423|d|@1.000000|#baz,foo:bar")); } - @Test(timeout = 5000L) + @Test(timeout=5000L) + public void sanitises_distribution_name_with_double() throws Exception { + + client.distribution("Foo:bar:baz.123", 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|d")); + } + + @Test(timeout=5000L) + public void sanitises_distribution_name_with_double_and_sample_rate() throws Exception { + + client.distribution("Foo:bar:baz.123", 1d, 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|d|@1.000000")); + } + + @Test(timeout=5000L) + public void sanitises_distribution_name_with_long() throws Exception { + + client.distribution("Foo:bar:baz.123", 1L); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|d")); + } + + @Test(timeout=5000L) + public void sanitises_distribution_name_with_long_and_sample_rate() throws Exception { + + client.distribution("Foo:bar:baz.123", 1L, 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|d|@1.000000")); + } + + @Test(timeout=5000L) public void sends_timer_to_statsd() throws Exception { @@ -429,6 +571,23 @@ public void sends_timer_with_sample_rate_to_statsd_with_tags() throws Exception assertThat(server.messagesReceived(), contains("my.prefix.mytime:123|ms|@1.000000|#baz,foo:bar")); } + @Test(timeout=5000L) + public void sanitises_timer_name() throws Exception { + + client.time("Foo:bar:baz.123", 1L); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|ms")); + } + + @Test(timeout=5000L) + public void sanitises_timer_name_with_sample_rate() throws Exception { + + client.time("Foo:bar:baz.123", 1L, 1d); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:1|ms|@1.000000")); + } @Test(timeout = 5000L) public void sends_gauge_mixed_tags() throws Exception { @@ -662,6 +821,15 @@ public void sends_set_to_statsd_with_tags() throws Exception { } + @Test(timeout=5000L) + public void sanitises_set_name() throws Exception { + + client.recordSetValue("Foo:bar:baz.123", "foo"); + server.waitForMessage(); + + assertThat(server.messagesReceived(), contains("my.prefix.Foo_bar_baz.123:foo|s")); + } + @Test(timeout=5000L) public void sends_too_large_message() throws Exception { final RecordingErrorHandler errorHandler = new RecordingErrorHandler(); From 19ce1727f5252d6bab8c4759c05055cd413f95b0 Mon Sep 17 00:00:00 2001 From: Ben Spencer Date: Thu, 15 Aug 2019 16:27:28 +0100 Subject: [PATCH 2/3] Remove some boilerplate. --- .../statsd/NonBlockingStatsDClient.java | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java b/src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java index c6af4763..a0f64195 100644 --- a/src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java +++ b/src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java @@ -760,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(sanitiseAspect(aspect)).append(":").append(delta).append("|c").append(tagString(tags)).toString()); + send(aspect, tags, Long.toString(delta), "|c"); } /** @@ -771,7 +771,7 @@ public void count(final String aspect, final long delta, final double sampleRate if(isInvalidSample(sampleRate)) { return; } - send(new StringBuilder(prefix).append(sanitiseAspect(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)); } /** @@ -788,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(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(delta)).append("|c").append(tagString(tags)).toString()); + send(aspect, tags, NUMBER_FORMATTERS.get().format(delta), "|c"); } /** @@ -799,7 +799,7 @@ public void count(final String aspect, final double delta, final double sampleRa if(isInvalidSample(sampleRate)) { return; } - 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()); + send(aspect, tags, NUMBER_FORMATTERS.get().format(delta), "|c|@", SAMPLE_RATE_FORMATTERS.get().format(sampleRate)); } /** @@ -896,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(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|g").append(tagString(tags)).toString()); + send(aspect, tags, NUMBER_FORMATTERS.get().format(value), "|g"); } /** @@ -907,7 +907,7 @@ public void recordGaugeValue(final String aspect, final double value, final doub if(isInvalidSample(sampleRate)) { return; } - 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()); + send(aspect, tags, NUMBER_FORMATTERS.get().format(value), "|g|@", SAMPLE_RATE_FORMATTERS.get().format(sampleRate)); } /** @@ -941,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(sanitiseAspect(aspect)).append(":").append(value).append("|g").append(tagString(tags)).toString()); + send(aspect, tags, Long.toString(value), "|g"); } /** @@ -952,7 +952,7 @@ public void recordGaugeValue(final String aspect, final long value, final double if(isInvalidSample(sampleRate)) { return; } - send(new StringBuilder(prefix).append(sanitiseAspect(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)); } /** @@ -985,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(sanitiseAspect(aspect)).append(":").append(timeInMs).append("|ms").append(tagString(tags)).toString()); + send(aspect, tags, Long.toString(timeInMs), "|ms"); } /** @@ -996,7 +996,7 @@ public void recordExecutionTime(final String aspect, final long timeInMs, final if(isInvalidSample(sampleRate)) { return; } - send(new StringBuilder(prefix).append(sanitiseAspect(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)); } /** @@ -1031,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(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|h").append(tagString(tags)).toString()); + send(aspect, tags, NUMBER_FORMATTERS.get().format(value), "|h"); } /** @@ -1044,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(sanitiseAspect(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)); } /** @@ -1077,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(sanitiseAspect(aspect)).append(":").append(value).append("|h").append(tagString(tags)).toString()); + send(aspect, tags, Long.toString(value), "|h"); } /** @@ -1088,7 +1088,7 @@ public void recordHistogramValue(final String aspect, final long value, final do if(isInvalidSample(sampleRate)) { return; } - send(new StringBuilder(prefix).append(sanitiseAspect(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)); } /** @@ -1125,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(sanitiseAspect(aspect)).append(":").append(NUMBER_FORMATTERS.get().format(value)).append("|d").append(tagString(tags)).toString()); + send(aspect, tags, NUMBER_FORMATTERS.get().format(value), "|d"); } /** @@ -1138,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(sanitiseAspect(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)); } /** @@ -1172,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(sanitiseAspect(aspect)).append(":").append(value).append("|d").append(tagString(tags)).toString()); + send(aspect, tags, Long.toString(value), "|d"); } /** @@ -1183,7 +1183,7 @@ public void recordDistributionValue(final String aspect, final long value, final if(isInvalidSample(sampleRate)) { return; } - send(new StringBuilder(prefix).append(sanitiseAspect(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)); } /** @@ -1390,6 +1390,15 @@ public void recordSetValue(final String aspect, final String value, final String 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) { statsDSender.send(message); } From 16474f21a6c0647f4c26e09be14630a72e6e20d2 Mon Sep 17 00:00:00 2001 From: Ben Spencer Date: Fri, 16 Aug 2019 10:13:59 +0100 Subject: [PATCH 3/3] Travis broken jdk7/8 builds? https://travis-ci.community/t/error-installing-oraclejdk8-expected-feature-release-number-in-range-of-9-to-14-but-got-8/3766/4 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index f20be51a..493d6732 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +dist: trusty language: java jdk: - oraclejdk8