Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid NaN values causing problems in metrics output #2812

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -185,6 +185,9 @@ private void rescale(long now, long next) {
final WeightedSnapshot.WeightedSample sample = values.remove(key);
final WeightedSnapshot.WeightedSample newSample = new WeightedSnapshot.WeightedSample(sample.getValue(),
sample.getWeight() * scalingFactor);
if (Double.compare(newSample.getWeight(), 0) == 0) {
continue;
}
values.put(key * scalingFactor, newSample);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -287,7 +287,7 @@ private static class TimerImpl implements Timer {
this.histogram = HelidonHistogram.create(repoType, Metadata.builder()
.withName(name)
.withType(MetricType.HISTOGRAM)
.build());
.build(), clock);
this.clock = clock;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -65,7 +65,11 @@ class WeightedSnapshot extends Snapshot {

for (int i = 0; i < copy.length; i++) {
this.values[i] = copy[i].value;
this.normWeights[i] = copy[i].weight / sumWeight;
/*
* A zero denominator could cause the resulting double to be infinity or, if the numerator is also 0,
* NaN. Either causes problems when formatting for JSON output. Just use 0 instead.
*/
this.normWeights[i] = sumWeight != 0 ? copy[i].weight / sumWeight : 0;
}

for (int i = 1; i < copy.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,6 +32,7 @@
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;

import org.eclipse.microprofile.metrics.Histogram;
import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricType;
Expand All @@ -43,6 +44,8 @@
import static io.helidon.metrics.HelidonMetricsMatcher.withinTolerance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -227,6 +230,27 @@ void testStatisticalValues() {
testSnapshot(10, "delegating longs", delegatingHistoLong.getSnapshot(), 506.3, 294.389);
}

@Test
void testNaNAvoidance() {
Metadata metadata = Metadata.builder()
.withName("long_idle_test")
.withDisplayName("theDisplayName")
.withDescription("Simulates a long idle period")
.withType(MetricType.HISTOGRAM)
.withUnit(MetricUnits.SECONDS)
.build();
TestClock testClock = TestClock.create();
Histogram idleHistogram = HelidonHistogram.create("application", metadata, testClock);

idleHistogram.update(100);

for (int i = 1; i < 48; i++) {
testClock.add(1, TimeUnit.HOURS);
assertThat("Idle histogram failed after simulating " + i + " hours", idleHistogram.getSnapshot().getMean(),
not(equalTo(Double.NaN)));
}
}

private void testSnapshot(int factor, String description, Snapshot snapshot, double mean, double stddev) {
assertAll("Testing statistical values for " + description,
() -> assertThat("median", snapshot.getMedian(), is(withinTolerance(factor * 48))),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -221,4 +221,23 @@ void testPrometheus() {
+ "/index.html\n"
+ "application_response_time_seconds_count 200"));
}

@Test
void testNaNAvoidance() {
TestClock testClock = TestClock.create();
HelidonTimer helidonTimer = HelidonTimer.create("application", meta, testClock);
MetricID metricID = new MetricID("idleTimer");

JsonObjectBuilder builder = MetricImpl.JSON.createObjectBuilder();
helidonTimer.update(1L, TimeUnit.MILLISECONDS);

for (int i = 1; i < 48; i++) {
testClock.add(1L, TimeUnit.HOURS);
try {
helidonTimer.jsonData(builder, metricID);
} catch (Throwable t) {
fail("Failed after simulating " + i + " hours");
}
}
}
}