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

Adjust JSON timer histogram output using the units (if any) that were set on the timer (2.x) #3132

Merged
merged 1 commit into from
Jun 18, 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
65 changes: 55 additions & 10 deletions metrics/metrics/src/main/java/io/helidon/metrics/HelidonTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.microprofile.metrics.Meter;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricType;
import org.eclipse.microprofile.metrics.MetricUnits;
import org.eclipse.microprofile.metrics.Snapshot;
import org.eclipse.microprofile.metrics.Timer;

Expand Down Expand Up @@ -137,20 +138,64 @@ public void jsonData(JsonObjectBuilder builder, MetricID metricID) {
.add(jsonFullKey("fiveMinRate", metricID), getFiveMinuteRate())
.add(jsonFullKey("fifteenMinRate", metricID), getFifteenMinuteRate());
Snapshot snapshot = getSnapshot();
myBuilder = myBuilder.add(jsonFullKey("min", metricID), snapshot.getMin())
.add(jsonFullKey("max", metricID), snapshot.getMax())
.add(jsonFullKey("mean", metricID), snapshot.getMean())
.add(jsonFullKey("stddev", metricID), snapshot.getStdDev())
.add(jsonFullKey("p50", metricID), snapshot.getMedian())
.add(jsonFullKey("p75", metricID), snapshot.get75thPercentile())
.add(jsonFullKey("p95", metricID), snapshot.get95thPercentile())
.add(jsonFullKey("p98", metricID), snapshot.get98thPercentile())
.add(jsonFullKey("p99", metricID), snapshot.get99thPercentile())
.add(jsonFullKey("p999", metricID), snapshot.get999thPercentile());
// Convert snapshot output according to units.
long divisor = conversionFactor();
myBuilder = myBuilder.add(jsonFullKey("min", metricID), snapshot.getMin() / divisor)
.add(jsonFullKey("max", metricID), snapshot.getMax() / divisor)
.add(jsonFullKey("mean", metricID), snapshot.getMean() / divisor)
.add(jsonFullKey("stddev", metricID), snapshot.getStdDev() / divisor)
.add(jsonFullKey("p50", metricID), snapshot.getMedian() / divisor)
.add(jsonFullKey("p75", metricID), snapshot.get75thPercentile() / divisor)
.add(jsonFullKey("p95", metricID), snapshot.get95thPercentile() / divisor)
.add(jsonFullKey("p98", metricID), snapshot.get98thPercentile() / divisor)
.add(jsonFullKey("p99", metricID), snapshot.get99thPercentile() / divisor)
.add(jsonFullKey("p999", metricID), snapshot.get999thPercentile() / divisor);

builder.add(metricID.getName(), myBuilder);
}

private long conversionFactor() {
Units units = getUnits();
String metricUnit = units.getMetricUnit();
if (metricUnit == null) {
return 1;
}
long divisor = 1;
switch (metricUnit) {
case MetricUnits.NANOSECONDS:
divisor = 1;
break;

case MetricUnits.MICROSECONDS:
divisor = 1000;
break;

case MetricUnits.MILLISECONDS:
divisor = 1000 * 1000;
break;

case MetricUnits.SECONDS:
divisor = 1000 * 1000 * 1000;
break;

case MetricUnits.MINUTES:
divisor = 1000 * 1000 * 1000 * 60;
break;

case MetricUnits.HOURS:
divisor = 1000 * 1000 * 1000 * 60 * 60;
break;

case MetricUnits.DAYS:
divisor = 1000 * 1000 * 1000 * 60 * 60 * 24;
break;

default:
divisor = 1;
}
return divisor;
}

void appendPrometheusTimerStatElement(StringBuilder sb,
PrometheusName name,
String statName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package io.helidon.metrics;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import javax.json.Json;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;

import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricType;
import org.eclipse.microprofile.metrics.MetricUnits;
import org.eclipse.microprofile.metrics.Snapshot;
Expand Down Expand Up @@ -240,4 +243,34 @@ void testNaNAvoidance() {
}
}
}

@Test
void testUnitsOnHistogram() {
TestClock testClock = TestClock.create();
String timerName = "jsonDataUnitsTimer";
Metadata metadata = Metadata.builder()
.withName(timerName)
.withDisplayName("Response time test")
.withDescription("Server response time for checking histo units")
.withType(MetricType.TIMER)
.withUnit(MetricUnits.MILLISECONDS)
.build();

HelidonTimer helidonTimer = HelidonTimer.create(MetricRegistry.Type.APPLICATION.getName(), metadata, testClock);

Stream.of(24L, 28L, 32L, 36L)
.forEach(value -> {
testClock.addNanos(450, TimeUnit.MILLISECONDS);
helidonTimer.update(value, TimeUnit.MILLISECONDS);
});
MetricID timerID = new MetricID(timerName);
JsonObjectBuilder builder = Json.createObjectBuilder();
helidonTimer.jsonData(builder, timerID);
JsonObject jsonObject = builder.build();
JsonObject metricObject = jsonObject.getJsonObject(timerName);
assertThat("Metric JSON object", metricObject, is(notNullValue()));
JsonNumber jsonNumber = metricObject.getJsonNumber("min");
assertThat("Min JSON value", jsonNumber, is(notNullValue()));
assertThat("Min histo value", jsonNumber.longValue(), is(24L));
}
}