Skip to content

Commit

Permalink
Merge pull request #16 from jontejj/master
Browse files Browse the repository at this point in the history
Fixes #15. Serializing of non numbers (Float & Double)
  • Loading branch information
davidB committed Feb 25, 2015
2 parents ff0f46a + dccba6a commit fd3512a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/main/java/metrics_influxdb/JsonBuilderDefault.java
Expand Up @@ -51,7 +51,16 @@ public void appendSeries(String namePrefix, String name, String nameSuffix, Stri
json.append('"').append(value).append('"');
} else if((value instanceof Collection) && ((Collection<?>)value).size()<1) {
json.append("null");
} else {
}
else if (value instanceof Double && !Double.isFinite((double) value))
{
json.append("null");
}
else if (value instanceof Float && !Float.isFinite((float) value))
{
json.append("null");
}
else {
json.append(value);
}
}
Expand Down
18 changes: 17 additions & 1 deletion src/test/java/sandbox/SendToLocalInfluxDB.java
Expand Up @@ -18,6 +18,7 @@
import metrics_influxdb.InfluxdbReporter;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
Expand All @@ -33,6 +34,9 @@ public static void main(String[] args) {
r0 = startConsoleReporter(registry);
r1 = startInfluxdbReporter(registry);

registerGaugeWithValues(registry, "double", Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1);
registerGaugeWithValues(registry, "float", Float.NaN, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, 1);

final Meter mymeter0 = registry.meter("MyMeter.0");
for (int i = 0; i < 100; i++) {
mymeter0.mark();
Expand All @@ -55,8 +59,20 @@ public static void main(String[] args) {
}
}

private static InfluxdbReporter startInfluxdbReporter(MetricRegistry registry) throws Exception {
private static void registerGaugeWithValues(MetricRegistry registry, String prefix, Object ...values) {
for(final Object value : values) {
registry.register(prefix + value, new Gauge<Object>() {
@Override
public Object getValue() {
return value;
}
});
}
}

private static InfluxdbReporter startInfluxdbReporter(MetricRegistry registry) throws Exception {
final InfluxdbHttp influxdb = new InfluxdbHttp("127.0.0.1", 8086, "dev", "u0", "u0PWD");
//influxdb.debugJson = true;
final InfluxdbReporter reporter = InfluxdbReporter
.forRegistry(registry)
.prefixedWith("test")
Expand Down

0 comments on commit fd3512a

Please sign in to comment.