Skip to content

Commit

Permalink
Fix #91, fine tune decimal number formatting.
Browse files Browse the repository at this point in the history
The expected string for `123_456_789.012_345 ` is `123456789.012345` and is NOT `1.23456789012345E8` as we generate today.
See https://github.com/influxdata/influxdb-java/blob/influxdb-java-2.5/src/main/java/org/influxdb/dto/Point.java#L321
  • Loading branch information
cyrille-leclerc committed Feb 16, 2017
1 parent d509982 commit 7a919cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/main/java/org/jmxtrans/agent/influxdb/InfluxMetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,35 @@
*/
package org.jmxtrans.agent.influxdb;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import org.jmxtrans.agent.util.StringUtils2;

import javax.annotation.concurrent.NotThreadSafe;

/**
* @author Kristoffer Erlandsson
*/
public class InfluxMetric {

private static final String FIELD_NAME = "value";

/*
* See https://github.com/influxdata/influxdb-java/blob/influxdb-java-2.5/src/main/java/org/influxdb/dto/Point.java#L321
*/
protected final static NumberFormat NUMBER_FORMAT;
static {
NUMBER_FORMAT = NumberFormat.getInstance(Locale.ENGLISH);
NUMBER_FORMAT.setMaximumFractionDigits(340);
NUMBER_FORMAT.setGroupingUsed(false);
NUMBER_FORMAT.setMinimumFractionDigits(1);
}
private final long timestampMillis;
private final List<InfluxTag> tags;
private final String measurement;
Expand Down Expand Up @@ -83,6 +100,11 @@ private String valueAsStr() {
if (value instanceof Integer || value instanceof Long) {
return value.toString() + "i";
}
if (value instanceof Float || value instanceof Double || value instanceof BigDecimal) {
synchronized (NUMBER_FORMAT) {
return NUMBER_FORMAT.format(value);
}
}
return value.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void toInfluxFormatInteger() throws Exception {
@Test
public void toInfluxFormatBigFloat() throws Exception {
InfluxMetric metric = new InfluxMetric("foo", EMPTY_TAG_LIST, 123_456_789.012_345, 2123l);
assertThat(metric.toInfluxFormat(), equalTo("foo value=1.23456789012345E8 2123"));
assertThat(metric.toInfluxFormat(), equalTo("foo value=123456789.012345 2123"));
}

@Test
Expand Down

0 comments on commit 7a919cc

Please sign in to comment.