Skip to content

Commit

Permalink
skip NaN value in StatsDOutputWriter (#151)
Browse files Browse the repository at this point in the history
Skip `NaN` and `INF` values in StatsDOutputWriter
  • Loading branch information
zhengyd2014 authored Jan 5, 2021
1 parent e583f83 commit f5f4649
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: java
sudo: false
jdk:
- oraclejdk8
- openjdk11
- openjdk7
cache:
directories:
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/jmxtrans/agent/StatsDOutputWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ public void writeInvocationResult(String invocationName, Object value) throws IO
@Override
public synchronized void writeQueryResult(String metricName, String metricType, Object value) throws IOException
{
// statsd expects a number value for the metric.
//
// skip if value's string representation equals to "NaN" or "INF", which are meaningless values to statsd.
// passing the invalid values down will trigger error in downstream parsing applications.
String strValue = String.valueOf(value);
if (strValue.equals("NaN") || strValue.equals("INF")) {
return;
}

//DataDog statsd with tags (https://docs.datadoghq.com/guides/dogstatsd/),
// metric.name:value|type|@sample_rate|#tag1:value,tag2
//Sysdig metric tags (https://support.sysdig.com/hc/en-us/articles/204376099-Metrics-integrations-StatsD-)
Expand All @@ -142,7 +151,7 @@ public synchronized void writeQueryResult(String metricName, String metricType,
.append(".")
.append(metricName)
.append(":")
.append(value)
.append(strValue)
.append("|")
.append(type)
.append("|#")
Expand All @@ -155,7 +164,7 @@ public synchronized void writeQueryResult(String metricName, String metricType,
.append("#")
.append(StringUtils2.join(Tag.convertTagsToStrings(tags), ","))
.append(":")
.append(value)
.append(strValue)
.append("|")
.append(type)
.append("\n");
Expand All @@ -164,7 +173,7 @@ public synchronized void writeQueryResult(String metricName, String metricType,
.append(".")
.append(metricName)
.append(":")
.append(value)
.append(strValue)
.append("|")
.append(type)
.append("\n");
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/jmxtrans/agent/StatsDOutputWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ public void test_write_counter_metric_sysdig() throws IOException {

}

@Test
public void test_skip_counter_with_NaN_value() throws IOException {
Map<String, String> 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");

writer.postConstruct(settings);
writer.writeQueryResult("my-metric", "gauge", "NaN");
Assert.assertNull(writer.receivedStat);

writer.writeQueryResult("my-metric", "gauge", "INF");
Assert.assertNull(writer.receivedStat);
}

/**
* https://github.com/jmxtrans/jmxtrans-agent/issues/98
*/
Expand Down

0 comments on commit f5f4649

Please sign in to comment.