Skip to content

Commit

Permalink
Don't send scientific notation to New Relic (fixes #539)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Schneider committed Apr 4, 2018
1 parent fa78620 commit 3d0f1fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.config.NamingConvention;
import io.micrometer.core.instrument.step.StepMeterRegistry;
import io.micrometer.core.instrument.util.DoubleFormat;
import io.micrometer.core.lang.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -134,11 +135,13 @@ private Stream<String> writeCounter(Counter counter) {
}

private Stream<String> writeGauge(Gauge gauge) {
return Stream.of(event(gauge.getId(), new Attribute("value", gauge.value())));
Double value = gauge.value();
return value.isNaN() ? Stream.empty() : Stream.of(event(gauge.getId(), new Attribute("value", value)));
}

private Stream<String> writeGauge(TimeGauge gauge) {
return Stream.of(event(gauge.getId(), new Attribute("value", gauge.value(getBaseTimeUnit()))));
Double value = gauge.value(getBaseTimeUnit());
return value.isNaN() ? Stream.empty() : Stream.of(event(gauge.getId(), new Attribute("value", value)));
}

private Stream<String> writeSummary(DistributionSummary summary) {
Expand Down Expand Up @@ -198,7 +201,7 @@ private String event(Meter.Id id, Iterable<Tag> extraTags, Attribute... attribut
}

return "{\"eventType\":\"" + getConventionName(id) + "\"" +
Arrays.stream(attributes).map(attr -> ",\"" + attr.getName() + "\":" + Double.toString(attr.getValue().doubleValue()))
Arrays.stream(attributes).map(attr -> ",\"" + attr.getName() + "\":" + DoubleFormat.decimalOrWhole(attr.getValue().doubleValue()))
.collect(Collectors.joining("")) + tagsJson.toString() + "}";
}

Expand All @@ -218,6 +221,9 @@ private void sendEvents(URL insightsEndpoint, List<String> events) {

String body = "[" + events.stream().collect(Collectors.joining(",")) + "]";

logger.trace("Sending payload to New Relic:");
logger.trace(body);

try (OutputStream os = con.getOutputStream()) {
os.write(body.getBytes());
os.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micrometer.core.instrument.util;

import io.micrometer.core.Issue;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -32,4 +33,11 @@ void decimalOrWhole() {
assertThat(DoubleFormat.decimalOrWhole(123456.1234567)).isEqualTo("123456.123457");
assertThat(DoubleFormat.decimalOrWhole(1)).isEqualTo("1");
}

@Issue("#539")
@Test
void noScientificNotation() {
assertThat(DoubleFormat.decimalOrWhole(4.6875392E7)).isEqualTo("46875392");
assertThat(DoubleFormat.decimalOrNan(4.6875392E7)).isEqualTo("46875392");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.samples.utils.SampleConfig;
import reactor.core.publisher.Flux;

import java.util.concurrent.atomic.AtomicInteger;

/**
* Demonstrates how monitoring systems deal with NaN values coming out of gauges.
*/
public class NullGaugeSample {
public static void main(String[] args) throws InterruptedException {
public static void main(String[] args) {
MeterRegistry registry = SampleConfig.myMonitoringSystem();
AtomicInteger n = new AtomicInteger(100);
AtomicInteger n = new AtomicInteger(46875392);

registry.gauge("my.null.gauge", (Object) null, o -> 1.0);
registry.gauge("my.nonnull.gauge", n);

for(;;) {
Thread.sleep(100);
}
Flux.never().blockLast();
}
}

0 comments on commit 3d0f1fd

Please sign in to comment.