Skip to content

Commit

Permalink
Make tags to be serialized according to DogStatsD and Sysdig protocols
Browse files Browse the repository at this point in the history
Fixes #123
  • Loading branch information
ncolomer authored and Cyrille Le Clerc committed Feb 1, 2018
1 parent 4d46c28 commit cfb0f50
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/jmxtrans/agent/StatsDOutputWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public synchronized void postConstruct(Map<String, String> settings) {
if (statsType.equals(STATSD_DATADOG)) {
String tagsStr = ConfigurationUtils.getString(settings, SETTINGS_TAGS, "");
tags = Tag.tagsFromCommaSeparatedString(tagsStr);
tags.add(new Tag("#host", ConfigurationUtils.getString(settings, SETTING_HOST_NAMME, getHostName().replaceAll("\\.", "_") )));
tags.add(new Tag("host", ConfigurationUtils.getString(settings, SETTING_HOST_NAMME, getHostName().replaceAll("\\.", "_") )));
metricNamePrefix = ConfigurationUtils.getString(settings, SETTING_ROOT_PREFIX, "java");
} else if (statsType.equals(STATSD_SYSDIG)) {
String tagsStr = ConfigurationUtils.getString(settings, SETTINGS_TAGS, "");
Expand Down Expand Up @@ -147,13 +147,14 @@ public synchronized void writeQueryResult(String metricName, String metricType,
.append(value)
.append("|")
.append(type)
.append("|")
.append("|#")
.append(StringUtils2.join(Tag.convertTagsToStrings(tags), ","))
.append("\n");
} else if (statsType.equals(STATSD_SYSDIG)) {
sb.append(metricNamePrefix)
.append(".")
.append(metricName)
.append("#")
.append(StringUtils2.join(Tag.convertTagsToStrings(tags), ","))
.append(":")
.append(value)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jmxtrans/agent/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static Tag parseOneTag(String part) {
throw new RuntimeException(
"Error when parsing tags from substring " + part + ", must be on format <name>:<value>,...");
}
Tag tag = new Tag("#"+nameAndValue[0].trim(), getValueProperties(nameAndValue[1].trim()));
Tag tag = new Tag(nameAndValue[0].trim(), getValueProperties(nameAndValue[1].trim()));
return tag;
}

Expand All @@ -100,7 +100,7 @@ private static Tag parseOneTag(String part, String separator) {
throw new RuntimeException(
"Error when parsing influx from substring " + part + ", must be on format <name"+separator+"<value>,...");
}
Tag tag = new Tag("#"+nameAndValue[0].trim(), getValueProperties(nameAndValue[1].trim()), separator);
Tag tag = new Tag(nameAndValue[0].trim(), getValueProperties(nameAndValue[1].trim()), separator);
return tag;
}

Expand Down
20 changes: 10 additions & 10 deletions src/test/java/org/jmxtrans/agent/StatsDOutputWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ public void test_write_counter_metric_dd() throws IOException {

writer.postConstruct(settings);
writer.writeQueryResult("my-metric", "gauge", 12);
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.my-metric:12|g|#tag1:ok,#tag2:woff,#host:bar\n"));
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.my-metric:12|g|#tag1:ok,tag2:woff,host:bar\n"));
writer.writeQueryResult("my-metric", "g", 13);
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.my-metric:13|g|#tag1:ok,#tag2:woff,#host:bar\n"));
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.my-metric:13|g|#tag1:ok,tag2:woff,host:bar\n"));

writer.writeQueryResult("the.answer", "counter", 42);
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer:42|c|#tag1:ok,#tag2:woff,#host:bar\n"));
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer:42|c|#tag1:ok,tag2:woff,host:bar\n"));
writer.writeQueryResult("the.answer", "c", 43);
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer:43|c|#tag1:ok,#tag2:woff,#host:bar\n"));
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer:43|c|#tag1:ok,tag2:woff,host:bar\n"));

writer.writeQueryResult("the.answer", "lala", 44);
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer:44|c|#tag1:ok,#tag2:woff,#host:bar\n"));
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer:44|c|#tag1:ok,tag2:woff,host:bar\n"));

}

Expand All @@ -100,17 +100,17 @@ public void test_write_counter_metric_sysdig() throws IOException {

writer.postConstruct(settings);
writer.writeQueryResult("my-metric", "gauge", 12);
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.my-metric#tag1=ok,#tag2=woff:12|g\n"));
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.my-metric#tag1=ok,tag2=woff:12|g\n"));
writer.writeQueryResult("my-metric", "g", 13);
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.my-metric#tag1=ok,#tag2=woff:13|g\n"));
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.my-metric#tag1=ok,tag2=woff:13|g\n"));

writer.writeQueryResult("the.answer", "counter", 42);
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer#tag1=ok,#tag2=woff:42|c\n"));
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer#tag1=ok,tag2=woff:42|c\n"));
writer.writeQueryResult("the.answer", "c", 43);
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer#tag1=ok,#tag2=woff:43|c\n"));
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer#tag1=ok,tag2=woff:43|c\n"));

writer.writeQueryResult("the.answer", "lala", 44);
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer#tag1=ok,#tag2=woff:44|c\n"));
Assert.assertThat(writer.receivedStat, equalTo("foo.bar.the.answer#tag1=ok,tag2=woff:44|c\n"));

}

Expand Down

0 comments on commit cfb0f50

Please sign in to comment.