Skip to content

Commit

Permalink
Fix runtime-metrics-java17 tests on openj9 (#8499)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored May 16, 2023
1 parent 30fc14e commit f916243
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,15 @@ public final class RuntimeMetrics implements Closeable {

private final AtomicBoolean isClosed = new AtomicBoolean();
private final OpenTelemetry openTelemetry;
private final List<RecordedEventHandler> recordedEventHandlers;
private final RecordingStream recordingStream;
private final CountDownLatch startUpLatch = new CountDownLatch(1);
private final List<AutoCloseable> observables = new ArrayList<>();
private final JfrRuntimeMetrics jfrRuntimeMetrics;

@SuppressWarnings("CatchingUnchecked")
RuntimeMetrics(
OpenTelemetry openTelemetry, Predicate<JfrFeature> featurePredicate, boolean disableJmx) {
this.openTelemetry = openTelemetry;
this.recordedEventHandlers = HandlerRegistry.getHandlers(openTelemetry, featurePredicate);
try {
recordingStream = new RecordingStream();
recordedEventHandlers.forEach(
handler -> {
EventSettings eventSettings = recordingStream.enable(handler.getEventName());
handler.getPollingDuration().ifPresent(eventSettings::withPeriod);
handler.getThreshold().ifPresent(eventSettings::withThreshold);
recordingStream.onEvent(handler.getEventName(), handler);
});
recordingStream.onMetadata(event -> startUpLatch.countDown());
Thread daemonRunner = new Thread(() -> recordingStream.start());
daemonRunner.setDaemon(true);
daemonRunner.start();
jfrRuntimeMetrics = JfrRuntimeMetrics.build(openTelemetry, featurePredicate);

// Set up metrics gathered by JMX
if (!disableJmx) {
Expand Down Expand Up @@ -98,18 +84,8 @@ OpenTelemetry getOpenTelemetry() {
}

// Visible for testing
List<RecordedEventHandler> getRecordedEventHandlers() {
return recordedEventHandlers;
}

// Visible for testing
RecordingStream getRecordingStream() {
return recordingStream;
}

// Visible for testing
CountDownLatch getStartUpLatch() {
return startUpLatch;
JfrRuntimeMetrics getJfrRuntimeMetrics() {
return jfrRuntimeMetrics;
}

/** Stop recording JFR events. */
Expand All @@ -119,9 +95,70 @@ public void close() {
logger.log(Level.WARNING, "RuntimeMetrics is already closed");
return;
}
recordingStream.close();
recordedEventHandlers.forEach(RecordedEventHandler::close);
if (jfrRuntimeMetrics != null) {
jfrRuntimeMetrics.close();
}

JmxRuntimeMetricsUtil.closeObservers(observables);
}

static class JfrRuntimeMetrics implements Closeable {
private final List<RecordedEventHandler> recordedEventHandlers;
private final RecordingStream recordingStream;
private final CountDownLatch startUpLatch = new CountDownLatch(1);

private JfrRuntimeMetrics(OpenTelemetry openTelemetry, Predicate<JfrFeature> featurePredicate) {
this.recordedEventHandlers = HandlerRegistry.getHandlers(openTelemetry, featurePredicate);
recordingStream = new RecordingStream();
recordedEventHandlers.forEach(
handler -> {
EventSettings eventSettings = recordingStream.enable(handler.getEventName());
handler.getPollingDuration().ifPresent(eventSettings::withPeriod);
handler.getThreshold().ifPresent(eventSettings::withThreshold);
recordingStream.onEvent(handler.getEventName(), handler);
});
recordingStream.onMetadata(event -> startUpLatch.countDown());
Thread daemonRunner = new Thread(() -> recordingStream.start());
daemonRunner.setDaemon(true);
daemonRunner.start();
}

static JfrRuntimeMetrics build(
OpenTelemetry openTelemetry, Predicate<JfrFeature> featurePredicate) {
if (!hasJfrRecordingStream()) {
return null;
}
return new JfrRuntimeMetrics(openTelemetry, featurePredicate);
}

@Override
public void close() {
recordingStream.close();
recordedEventHandlers.forEach(RecordedEventHandler::close);
}

// Visible for testing
List<RecordedEventHandler> getRecordedEventHandlers() {
return recordedEventHandlers;
}

// Visible for testing
RecordingStream getRecordingStream() {
return recordingStream;
}

// Visible for testing
CountDownLatch getStartUpLatch() {
return startUpLatch;
}

private static boolean hasJfrRecordingStream() {
try {
Class.forName("jdk.jfr.consumer.RecordingStream");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public void beforeEach(ExtensionContext context) throws InterruptedException {
RuntimeMetricsBuilder builder = RuntimeMetrics.builder(sdk);
builderConsumer.accept(builder);
runtimeMetrics = builder.build();
runtimeMetrics.getStartUpLatch().await(30, TimeUnit.SECONDS);
RuntimeMetrics.JfrRuntimeMetrics jfrRuntimeMetrics = runtimeMetrics.getJfrRuntimeMetrics();
if (jfrRuntimeMetrics != null) {
jfrRuntimeMetrics.getStartUpLatch().await(30, TimeUnit.SECONDS);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void build() {
var openTelemetry = OpenTelemetry.noop();
try (var jfrTelemetry = new RuntimeMetricsBuilder(openTelemetry).build()) {
assertThat(jfrTelemetry.getOpenTelemetry()).isSameAs(openTelemetry);
assertThat(jfrTelemetry.getRecordedEventHandlers())
assertThat(jfrTelemetry.getJfrRuntimeMetrics().getRecordedEventHandlers())
.hasSizeGreaterThan(0)
.allSatisfy(handler -> assertThat(handler.getFeature().isDefaultEnabled()).isTrue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void create_AllDisabled() {
void builder() {
try (var jfrTelemetry = RuntimeMetrics.builder(sdk).build()) {
assertThat(jfrTelemetry.getOpenTelemetry()).isSameAs(sdk);
assertThat(jfrTelemetry.getRecordedEventHandlers())
assertThat(jfrTelemetry.getJfrRuntimeMetrics().getRecordedEventHandlers())
.hasSizeGreaterThan(0)
.allSatisfy(handler -> assertThat(handler.getFeature().isDefaultEnabled()).isTrue());
}
Expand All @@ -82,7 +82,10 @@ void close() {
try (RuntimeMetrics jfrTelemetry = RuntimeMetrics.builder(sdk).build()) {
// Track whether RecordingStream has been closed
AtomicBoolean recordingStreamClosed = new AtomicBoolean(false);
jfrTelemetry.getRecordingStream().onClose(() -> recordingStreamClosed.set(true));
jfrTelemetry
.getJfrRuntimeMetrics()
.getRecordingStream()
.onClose(() -> recordingStreamClosed.set(true));

assertThat(reader.collectAllMetrics()).isNotEmpty();

Expand Down

0 comments on commit f916243

Please sign in to comment.