From f6cf591dbbc360ff15b1db6135b593049cf6615b Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Thu, 12 Sep 2024 00:35:23 +0900 Subject: [PATCH] Polish gh-5145 --- .../io/micrometer/core/annotation/Timed.java | 1 + .../micrometer/core/aop/TimedAspectTest.java | 22 +++++++++---------- .../MetricsRequestEventListenerTimedTest.java | 15 +++++-------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/annotation/Timed.java b/micrometer-core/src/main/java/io/micrometer/core/annotation/Timed.java index fc62d882a3..ea181ce4fc 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/annotation/Timed.java +++ b/micrometer-core/src/main/java/io/micrometer/core/annotation/Timed.java @@ -80,6 +80,7 @@ * should be passed as {@code 0.1}. * @return service level objectives to calculate * @see io.micrometer.core.instrument.Timer.Builder#serviceLevelObjectives(java.time.Duration...) + * @since 1.14.0 */ double[] serviceLevelObjectives() default {}; diff --git a/micrometer-core/src/test/java/io/micrometer/core/aop/TimedAspectTest.java b/micrometer-core/src/test/java/io/micrometer/core/aop/TimedAspectTest.java index b12f5d4d78..900495960e 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/aop/TimedAspectTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/aop/TimedAspectTest.java @@ -28,6 +28,7 @@ import io.micrometer.core.instrument.distribution.pause.PauseDetector; import io.micrometer.core.instrument.search.MeterNotFoundException; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import io.micrometer.core.instrument.util.TimeUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -35,9 +36,9 @@ import org.springframework.aop.aspectj.annotation.AspectJProxyFactory; import javax.annotation.Nonnull; -import java.util.Arrays; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; +import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import static java.util.concurrent.CompletableFuture.supplyAsync; @@ -108,16 +109,15 @@ void timeMethodWithSloTimer() { service.sloCall(); - assertThat(Arrays - .stream(registry.get("sloCall") - .tag("class", getClass().getName() + "$TimedService") - .tag("method", "sloCall") - .tag("extra", "tag") - .timer() - .takeSnapshot() - .histogramCounts()) - .mapToDouble(CountAtBucket::bucket) - .toArray()).isEqualTo(new double[] { Math.pow(10, 9) * 0.1, Math.pow(10, 9) * 0.5 }); + assertThat(registry.get("sloCall") + .tag("class", getClass().getName() + "$TimedService") + .tag("method", "sloCall") + .tag("extra", "tag") + .timer() + .takeSnapshot() + .histogramCounts()).extracting(CountAtBucket::bucket) + .containsExactly(TimeUtils.secondsToUnit(0.1, TimeUnit.NANOSECONDS), + TimeUtils.secondsToUnit(0.5, TimeUnit.NANOSECONDS)); } @Test diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListenerTimedTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListenerTimedTest.java index fa55243a97..1a5937f4a4 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListenerTimedTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jersey/server/MetricsRequestEventListenerTimedTest.java @@ -23,6 +23,7 @@ import io.micrometer.core.instrument.binder.jersey.server.resources.TimedResource; import io.micrometer.core.instrument.distribution.CountAtBucket; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import io.micrometer.core.instrument.util.TimeUtils; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.junit.jupiter.api.Test; @@ -95,17 +96,13 @@ void resourcesWithAnnotationAreTimed() { } @Test - void sloTaskTimerSupported() throws InterruptedException, ExecutionException, TimeoutException { + void sloTimerSupported() { target("timed-slo").request().get(); - CountAtBucket[] slos = registry.get("timedSlo") - .tags(tagsFrom("/timed-slo", 200)) - .timer() - .takeSnapshot() - .histogramCounts(); - assertThat(slos.length).isEqualTo(2); - assertThat(slos[0].bucket()).isEqualTo(Math.pow(10, 9) * 0.1); - assertThat(slos[1].bucket()).isEqualTo(Math.pow(10, 9) * 0.5); + assertThat(registry.get("timedSlo").tags(tagsFrom("/timed-slo", 200)).timer().takeSnapshot().histogramCounts()) + .extracting(CountAtBucket::bucket) + .containsExactly(TimeUtils.secondsToUnit(0.1, TimeUnit.NANOSECONDS), + TimeUtils.secondsToUnit(0.5, TimeUnit.NANOSECONDS)); } @Test