Skip to content

Commit

Permalink
Add percentiles and histogramCounts back to Timer/DistributionSummary…
Browse files Browse the repository at this point in the history
… for backwards compat
  • Loading branch information
Jon Schneider committed Apr 4, 2018
1 parent a142ec1 commit fa78620
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
*/
package io.micrometer.core.instrument;

import io.micrometer.core.instrument.distribution.CountAtBucket;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.HistogramSupport;
import io.micrometer.core.instrument.distribution.ValueAtPercentile;
import io.micrometer.core.lang.Nullable;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* Track the sample distribution of events. An example would be the response sizes for requests
Expand Down Expand Up @@ -66,6 +69,41 @@ default double mean() {
*/
double max();

/**
* Provides cumulative histogram counts.
*
* @param value The histogram bucket to retrieve a count for.
* @return The count of all events less than or equal to the bucket. If value does not
* match a preconfigured bucket boundary, returns NaN.
* @deprecated Use {@link #takeSnapshot()} to retrieve bucket counts.
*/
@Deprecated
default double histogramCountAtValue(long value) {
for (CountAtBucket countAtBucket : takeSnapshot().histogramCounts()) {
if ((long) countAtBucket.bucket(TimeUnit.NANOSECONDS) == value) {
return countAtBucket.count();
}
}
return Double.NaN;
}

/**
* @param percentile A percentile in the domain [0, 1]. For example, 0.5 represents the 50th percentile of the
* distribution.
* @return The latency at a specific percentile. This value is non-aggregable across dimensions. Returns NaN if
* percentile is not a preconfigured percentile that Micrometer is tracking.
* @deprecated Use {@link #takeSnapshot()} to retrieve percentiles.
*/
@Deprecated
default double percentile(double percentile) {
for (ValueAtPercentile valueAtPercentile : takeSnapshot().percentileValues()) {
if (valueAtPercentile.percentile() == percentile) {
return valueAtPercentile.value();
}
}
return Double.NaN;
}

@Override
default Iterable<Measurement> measure() {
return Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package io.micrometer.core.instrument;

import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.distribution.CountAtBucket;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.HistogramSupport;
import io.micrometer.core.instrument.distribution.ValueAtPercentile;
import io.micrometer.core.instrument.distribution.pause.PauseDetector;
import io.micrometer.core.lang.Nullable;

Expand Down Expand Up @@ -169,6 +171,42 @@ default Iterable<Measurement> measure() {
);
}

/**
* Provides cumulative histogram counts.
*
* @param valueNanos The histogram bucket to retrieve a count for.
* @return The count of all events less than or equal to the bucket. If valueNanos does not
* match a preconfigured bucket boundary, returns NaN.
* @deprecated Use {@link #takeSnapshot()} to retrieve bucket counts.
*/
@Deprecated
default double histogramCountAtValue(long valueNanos) {
for (CountAtBucket countAtBucket : takeSnapshot().histogramCounts()) {
if ((long) countAtBucket.bucket(TimeUnit.NANOSECONDS) == valueNanos) {
return countAtBucket.count();
}
}
return Double.NaN;
}

/**
* @param percentile A percentile in the domain [0, 1]. For example, 0.5 represents the 50th percentile of the
* distribution.
* @param unit The base unit of time to scale the percentile value to.
* @return The latency at a specific percentile. This value is non-aggregable across dimensions. Returns NaN if
* percentile is not a preconfigured percentile that Micrometer is tracking.
* @deprecated Use {@link #takeSnapshot()} to retrieve bucket counts.
*/
@Deprecated
default double percentile(double percentile, TimeUnit unit) {
for (ValueAtPercentile valueAtPercentile : takeSnapshot().percentileValues()) {
if (valueAtPercentile.percentile() == percentile) {
return valueAtPercentile.value(unit);
}
}
return Double.NaN;
}

/**
* @return The base time unit of the timer to which all published metrics will be scaled
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.MeterRegistry;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -84,4 +85,28 @@ default void scale(MeterRegistry registry) {
clock(registry).add(step());
assertThat(ds.totalAmount()).isEqualTo(2.0);
}

@Deprecated
@Test
default void percentiles(MeterRegistry registry) {
DistributionSummary s = DistributionSummary.builder("my.summary")
.publishPercentiles(1)
.register(registry);

s.record(1);
assertThat(s.percentile(1)).isEqualTo(1, Offset.offset(0.3));
assertThat(s.percentile(0.5)).isEqualTo(Double.NaN);
}

@Deprecated
@Test
default void histogramCounts(MeterRegistry registry) {
DistributionSummary s = DistributionSummary.builder("my.summmary")
.sla(1)
.register(registry);

s.record(1);
assertThat(s.histogramCountAtValue(1)).isEqualTo(1);
assertThat(s.histogramCountAtValue(2)).isEqualTo(Double.NaN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -27,6 +28,7 @@
import java.util.function.Supplier;

import static io.micrometer.core.instrument.MockClock.clock;
import static io.micrometer.core.instrument.util.TimeUtils.millisToUnit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -61,7 +63,7 @@ default void recordDuration(MeterRegistry registry) {
clock(registry).add(step());

assertAll(() -> assertEquals(1L, t.count()),
() -> assertEquals(42, t.totalTime(TimeUnit.MILLISECONDS), 1.0e-12));
() -> assertEquals(42, t.totalTime(TimeUnit.MILLISECONDS), 1.0e-12));
}

@Test
Expand Down Expand Up @@ -95,7 +97,7 @@ default void recordWithRunnable(MeterRegistry registry) {
clock(registry).add(step());
} finally {
assertAll(() -> assertEquals(1L, t.count()),
() -> assertEquals(10, t.totalTime(TimeUnit.NANOSECONDS) ,1.0e-12));
() -> assertEquals(10, t.totalTime(TimeUnit.NANOSECONDS), 1.0e-12));
}
}

Expand All @@ -110,7 +112,7 @@ default void recordWithSample(MeterRegistry registry) {
clock(registry).add(step());

assertAll(() -> assertEquals(1L, timer.count()),
() -> assertEquals(10, timer.totalTime(TimeUnit.NANOSECONDS) ,1.0e-12));
() -> assertEquals(10, timer.totalTime(TimeUnit.NANOSECONDS), 1.0e-12));
}

@Test
Expand Down Expand Up @@ -145,4 +147,28 @@ default void recordCallableException(MeterRegistry registry) {
assertAll(() -> assertEquals(1L, t.count()),
() -> assertEquals(10, t.totalTime(TimeUnit.NANOSECONDS), 1.0e-12));
}

@Deprecated
@Test
default void percentiles(MeterRegistry registry) {
Timer t = Timer.builder("my.timer")
.publishPercentiles(1)
.register(registry);

t.record(1, TimeUnit.MILLISECONDS);
assertThat(t.percentile(1, TimeUnit.MILLISECONDS)).isEqualTo(1, Offset.offset(0.3));
assertThat(t.percentile(0.5, TimeUnit.MILLISECONDS)).isEqualTo(Double.NaN);
}

@Deprecated
@Test
default void histogramCounts(MeterRegistry registry) {
Timer t = Timer.builder("my.timer")
.sla(Duration.ofMillis(1))
.register(registry);

t.record(1, TimeUnit.MILLISECONDS);
assertThat(t.histogramCountAtValue((long) millisToUnit(1, TimeUnit.NANOSECONDS))).isEqualTo(1);
assertThat(t.histogramCountAtValue(1)).isEqualTo(Double.NaN);
}
}

0 comments on commit fa78620

Please sign in to comment.