From 521109e29b851f77c47a70e1d36db098628308f1 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Fri, 13 Sep 2024 16:10:10 +0300 Subject: [PATCH] Convert gc duration metrics to seconds (#12244) --- .../java17/internal/DurationUtil.java | 8 +++++++- .../G1GarbageCollectionHandler.java | 3 ++- .../OldGarbageCollectionHandler.java | 3 ++- .../YoungGarbageCollectionHandler.java | 3 ++- .../java17/internal/DurationUtilTest.java | 16 +++++++++------- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/DurationUtil.java b/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/DurationUtil.java index ff8ddb1894de..4a89420b183f 100644 --- a/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/DurationUtil.java +++ b/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/DurationUtil.java @@ -6,13 +6,15 @@ package io.opentelemetry.instrumentation.runtimemetrics.java17.internal; import java.time.Duration; +import java.util.concurrent.TimeUnit; /** * This class is internal and is hence not for public use. Its APIs are unstable and can change at * any time. */ public final class DurationUtil { - private static final double NANOS_PER_SECOND = 1e9; + private static final double NANOS_PER_SECOND = TimeUnit.SECONDS.toNanos(1); + private static final double MILLIS_PER_SECOND = TimeUnit.SECONDS.toMillis(1); /** Returns the duration as seconds, with fractional part included. */ public static double toSeconds(Duration duration) { @@ -20,5 +22,9 @@ public static double toSeconds(Duration duration) { return epochSecs + duration.getNano() / NANOS_PER_SECOND; } + public static double millisToSeconds(long milliseconds) { + return milliseconds / MILLIS_PER_SECOND; + } + private DurationUtil() {} } diff --git a/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/G1GarbageCollectionHandler.java b/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/G1GarbageCollectionHandler.java index cc5720df2fdb..53b550552ffc 100644 --- a/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/G1GarbageCollectionHandler.java +++ b/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/G1GarbageCollectionHandler.java @@ -10,6 +10,7 @@ import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.instrumentation.runtimemetrics.java17.JfrFeature; import io.opentelemetry.instrumentation.runtimemetrics.java17.internal.Constants; +import io.opentelemetry.instrumentation.runtimemetrics.java17.internal.DurationUtil; import io.opentelemetry.instrumentation.runtimemetrics.java17.internal.RecordedEventHandler; import java.time.Duration; import java.util.Optional; @@ -40,7 +41,7 @@ public G1GarbageCollectionHandler(Meter meter) { @Override public void accept(RecordedEvent ev) { - histogram.record(ev.getDouble(Constants.DURATION), ATTR); + histogram.record(DurationUtil.millisToSeconds(ev.getLong(Constants.DURATION)), ATTR); } @Override diff --git a/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/OldGarbageCollectionHandler.java b/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/OldGarbageCollectionHandler.java index 26bff6b82108..02d812948027 100644 --- a/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/OldGarbageCollectionHandler.java +++ b/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/OldGarbageCollectionHandler.java @@ -10,6 +10,7 @@ import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.instrumentation.runtimemetrics.java17.JfrFeature; import io.opentelemetry.instrumentation.runtimemetrics.java17.internal.Constants; +import io.opentelemetry.instrumentation.runtimemetrics.java17.internal.DurationUtil; import io.opentelemetry.instrumentation.runtimemetrics.java17.internal.RecordedEventHandler; import java.time.Duration; import java.util.Optional; @@ -40,7 +41,7 @@ public OldGarbageCollectionHandler(Meter meter, String gc) { @Override public void accept(RecordedEvent ev) { - histogram.record(ev.getDouble(Constants.DURATION), attributes); + histogram.record(DurationUtil.millisToSeconds(ev.getLong(Constants.DURATION)), attributes); } @Override diff --git a/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/YoungGarbageCollectionHandler.java b/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/YoungGarbageCollectionHandler.java index 534b131de2ee..1e824d08f6de 100644 --- a/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/YoungGarbageCollectionHandler.java +++ b/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/garbagecollection/YoungGarbageCollectionHandler.java @@ -10,6 +10,7 @@ import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.instrumentation.runtimemetrics.java17.JfrFeature; import io.opentelemetry.instrumentation.runtimemetrics.java17.internal.Constants; +import io.opentelemetry.instrumentation.runtimemetrics.java17.internal.DurationUtil; import io.opentelemetry.instrumentation.runtimemetrics.java17.internal.RecordedEventHandler; import java.time.Duration; import java.util.Optional; @@ -41,7 +42,7 @@ public YoungGarbageCollectionHandler(Meter meter, String gc) { @Override public void accept(RecordedEvent ev) { - histogram.record(ev.getDouble(Constants.DURATION), attributes); + histogram.record(DurationUtil.millisToSeconds(ev.getLong(Constants.DURATION)), attributes); } @Override diff --git a/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/DurationUtilTest.java b/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/DurationUtilTest.java index 589bd85df1ca..bcb3047aedf1 100644 --- a/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/DurationUtilTest.java +++ b/instrumentation/runtime-telemetry/runtime-telemetry-java17/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/java17/internal/DurationUtilTest.java @@ -5,22 +5,24 @@ package io.opentelemetry.instrumentation.runtimemetrics.java17.internal; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; import java.time.Duration; +import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; class DurationUtilTest { @Test - void shouldConvertDurationToSeconds() { - // Given + void convertDurationToSeconds() { Duration duration = Duration.ofSeconds(7, 144); - - // When double seconds = DurationUtil.toSeconds(duration); + assertThat(seconds).isEqualTo(7.000000144); + } - // Then - assertEquals(7.000000144, seconds); + @Test + void convertMillisSeconds() { + double seconds = DurationUtil.millisToSeconds(TimeUnit.SECONDS.toMillis(5)); + assertThat(seconds).isEqualTo(5); } }