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

Convert gc duration metrics to seconds #12244

Merged
merged 1 commit into from
Sep 13, 2024
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
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);
}
}
Loading