Skip to content

Commit

Permalink
Convert gc duration metrics to seconds (#12244)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Sep 13, 2024
1 parent 54c7b16 commit 521109e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@
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) {
double epochSecs = (double) duration.getSeconds();
return epochSecs + duration.getNano() / NANOS_PER_SECOND;
}

public static double millisToSeconds(long milliseconds) {
return milliseconds / MILLIS_PER_SECOND;
}

private DurationUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 521109e

Please sign in to comment.