-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add JFR metrics for virtual thread pinning and submit failures #19092
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
Open
tsawada
wants to merge
2
commits into
open-telemetry:main
Choose a base branch
from
tsawada:tsawada/jfr-virtual-threads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...lemetry/instrumentation/runtimetelemetry/internal/threads/VirtualThreadPinnedHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.runtimetelemetry.internal.threads; | ||
|
|
||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.metrics.DoubleHistogram; | ||
| import io.opentelemetry.api.metrics.Meter; | ||
| import io.opentelemetry.instrumentation.runtimetelemetry.internal.Constants; | ||
| import io.opentelemetry.instrumentation.runtimetelemetry.internal.DurationUtil; | ||
| import io.opentelemetry.instrumentation.runtimetelemetry.internal.JfrFeature; | ||
| import io.opentelemetry.instrumentation.runtimetelemetry.internal.RecordedEventHandler; | ||
| import java.time.Duration; | ||
| import java.util.Optional; | ||
| import jdk.jfr.consumer.RecordedEvent; | ||
|
|
||
| /** | ||
| * This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
| * any time. | ||
| */ | ||
| public final class VirtualThreadPinnedHandler implements RecordedEventHandler { | ||
| private static final String METRIC_NAME = "jvm.thread.virtual.pinned"; | ||
| private static final String METRIC_DESCRIPTION = "Duration of virtual thread pinning"; | ||
| private static final String EVENT_NAME = "jdk.VirtualThreadPinned"; | ||
|
|
||
| private final DoubleHistogram histogram; | ||
| private final Attributes attributes; | ||
|
|
||
| public VirtualThreadPinnedHandler(Meter meter) { | ||
| histogram = | ||
| meter | ||
| .histogramBuilder(METRIC_NAME) | ||
| .setDescription(METRIC_DESCRIPTION) | ||
| .setUnit(Constants.SECONDS) | ||
| .build(); | ||
|
|
||
| attributes = Attributes.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getEventName() { | ||
| return EVENT_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public JfrFeature getFeature() { | ||
| return JfrFeature.VIRTUAL_THREAD_METRICS; | ||
| } | ||
|
|
||
| @Override | ||
| public void accept(RecordedEvent recordedEvent) { | ||
| histogram.record(DurationUtil.toSeconds(recordedEvent.getDuration()), attributes); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Duration> getThreshold() { | ||
| return Optional.empty(); | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
...y/instrumentation/runtimetelemetry/internal/threads/VirtualThreadSubmitFailedHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.runtimetelemetry.internal.threads; | ||
|
|
||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.metrics.LongCounter; | ||
| import io.opentelemetry.api.metrics.Meter; | ||
| import io.opentelemetry.instrumentation.runtimetelemetry.internal.JfrFeature; | ||
| import io.opentelemetry.instrumentation.runtimetelemetry.internal.RecordedEventHandler; | ||
| import java.time.Duration; | ||
| import java.util.Optional; | ||
| import jdk.jfr.consumer.RecordedEvent; | ||
|
|
||
| /** | ||
| * This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
| * any time. | ||
| */ | ||
| public final class VirtualThreadSubmitFailedHandler implements RecordedEventHandler { | ||
| private static final String METRIC_NAME = "jvm.thread.virtual.submit_failed"; | ||
| private static final String METRIC_DESCRIPTION = | ||
| "Number of times a virtual thread failed to be submitted to its scheduler"; | ||
| private static final String EVENT_NAME = "jdk.VirtualThreadSubmitFailed"; | ||
|
|
||
| private final LongCounter counter; | ||
| private final Attributes attributes; | ||
|
|
||
| public VirtualThreadSubmitFailedHandler(Meter meter) { | ||
| counter = meter.counterBuilder(METRIC_NAME).setDescription(METRIC_DESCRIPTION).build(); | ||
|
|
||
| attributes = Attributes.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getEventName() { | ||
| return EVENT_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public JfrFeature getFeature() { | ||
| return JfrFeature.VIRTUAL_THREAD_METRICS; | ||
| } | ||
|
|
||
| @Override | ||
| public void accept(RecordedEvent recordedEvent) { | ||
| counter.add(1, attributes); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Duration> getThreshold() { | ||
| return Optional.empty(); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
...21/java/io/opentelemetry/instrumentation/runtimetelemetry/JfrVirtualThreadPinnedTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.runtimetelemetry; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.opentelemetry.instrumentation.runtimetelemetry.internal.Constants; | ||
| import io.opentelemetry.instrumentation.runtimetelemetry.internal.JfrFeature; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.locks.LockSupport; | ||
| import org.junit.jupiter.api.Assumptions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| class JfrVirtualThreadPinnedTest { | ||
|
|
||
| @RegisterExtension | ||
| JfrExtension jfrExtension = | ||
| new JfrExtension( | ||
| jfrConfig -> { | ||
| jfrConfig.disableAllFeatures(); | ||
| jfrConfig.enableFeature(JfrFeature.VIRTUAL_THREAD_METRICS); | ||
| }); | ||
|
|
||
| @Test | ||
| void shouldHaveVirtualThreadPinnedEvents() throws InterruptedException { | ||
| // Thread.ofVirtual() is GA in Java 21; synchronized pinning was removed in Java 24 (JEP 491) | ||
| int feature = Runtime.version().feature(); | ||
| Assumptions.assumeTrue(feature >= 21 && feature < 24, "Requires Java 21-23"); | ||
|
|
||
| CountDownLatch latch = new CountDownLatch(1); | ||
|
|
||
| // Run a synchronized block inside a virtual thread to trigger pinning | ||
| Object lock = new Object(); | ||
| Thread vt = | ||
| Thread.ofVirtual() | ||
| .start( | ||
| () -> { | ||
| synchronized (lock) { | ||
| // Parking inside a synchronized block pins the virtual thread | ||
| LockSupport.parkNanos(100_000_000L); // 100ms | ||
| } | ||
| latch.countDown(); | ||
| }); | ||
|
|
||
| assertThat(latch.await(10, SECONDS)).isTrue(); | ||
| vt.join(5000); | ||
|
|
||
| jfrExtension.waitAndAssertMetrics( | ||
| metric -> | ||
| metric | ||
| .hasName("jvm.thread.virtual.pinned") | ||
| .hasUnit(Constants.SECONDS) | ||
| .hasHistogramSatisfying(histogram -> histogram.hasPointsSatisfying(point -> {}))); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could consider
@EnabledOnJre(JRE.JAVA_21)