From f1a73996704cc9420c33acf45366c25e7c9ecb28 Mon Sep 17 00:00:00 2001 From: booktrail Date: Sat, 23 Apr 2022 19:48:45 -0700 Subject: [PATCH] Fix dogstatsd metric without tags in StatsDOutputWriter --- .../org/jmxtrans/agent/StatsDOutputWriter.java | 10 ++++++---- .../org/jmxtrans/agent/StatsDOutputWriterTest.java | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jmxtrans/agent/StatsDOutputWriter.java b/src/main/java/org/jmxtrans/agent/StatsDOutputWriter.java index 69f3dd36..86037a83 100644 --- a/src/main/java/org/jmxtrans/agent/StatsDOutputWriter.java +++ b/src/main/java/org/jmxtrans/agent/StatsDOutputWriter.java @@ -147,10 +147,12 @@ protected synchronized String buildMetricsString(String metricName, String metri .append(":") .append(strValue) .append("|") - .append(type) - .append("|#") - .append(StringUtils2.join(Tag.convertTagsToStrings(tags), ",")) - .append("\n"); + .append(type); + if (!tags.isEmpty()) { + sb.append("|#"); + sb.append(StringUtils2.join(Tag.convertTagsToStrings(tags), ",")); + } + sb.append("\n"); } else if (statsType.equals(STATSD_SYSDIG)) { sb.append(metricNamePrefix) .append(".") diff --git a/src/test/java/org/jmxtrans/agent/StatsDOutputWriterTest.java b/src/test/java/org/jmxtrans/agent/StatsDOutputWriterTest.java index 6e32cba6..467de935 100644 --- a/src/test/java/org/jmxtrans/agent/StatsDOutputWriterTest.java +++ b/src/test/java/org/jmxtrans/agent/StatsDOutputWriterTest.java @@ -87,6 +87,20 @@ public void test_write_counter_metric_dd() throws IOException { } + @Test + public void test_write_counter_metric_dd_without_tags() throws IOException { + Map settings = new HashMap<>(); + settings.put(StatsDOutputWriter.SETTING_ROOT_PREFIX, "foo.bar"); + // No real connect is done. Config is here to please the postConstruct. + settings.put(StatsDOutputWriter.SETTING_HOST, "localhost"); + settings.put(StatsDOutputWriter.SETTING_PORT, "8125"); + settings.put(StatsDOutputWriter.SETTINGS_STATSD_TYPE, "dd"); + + writer.postConstruct(settings); + writer.writeQueryResult("my-metric", "gauge", 12); + Assert.assertThat(writer.receivedStat, equalTo("foo.bar.my-metric:12|g\n")); + } + @Test public void test_write_counter_metric_sysdig() throws IOException { Map settings = new HashMap<>();