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

Virtual threads metrics #5067

Merged
merged 20 commits into from
Sep 24, 2024
Merged
Changes from 1 commit
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
@@ -0,0 +1,70 @@
/*
* Copyright 2024 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.core.instrument.binder.jvm;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.binder.MeterBinder;
import jdk.jfr.consumer.RecordingStream;

import static java.util.Collections.emptyList;

public class JvmVirtualThreadEventMetrics implements MeterBinder, AutoCloseable {
shakuzen marked this conversation as resolved.
Show resolved Hide resolved

private final Iterable<Tag> tags;

private final RecordingStream recordingStream;

public JvmVirtualThreadEventMetrics() {
this(emptyList());
}

public JvmVirtualThreadEventMetrics(Iterable<Tag> tags) {
this.tags = tags;
this.recordingStream = new RecordingStream();

recordingStream.enable("jdk.VirtualThreadPinnedEvent");
recordingStream.enable("jdk.VirtualThreadSubmitFailedEvent");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with the right RecordingStream usage, but I was looking at this blog, and it made me wonder: do we not need to start the stream? And should we not set a max age so we don't accumulate events indefinitely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @shakuzen,

Do you think we need to expose maxAge duration as a constructor parameter?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I had the same thought, but I'm not sure due to my lack of familiarity with JFR and in particular RecordingStream. I don't understand what this max age means in terms of the event listeners we register. I assume the event listeners are run nearly instantly after an event is recorded. So I'm not sure the max age could affect them. Or will max age effectively filter out events with a duration longer than the max age duration?

}

@Override
public void bindTo(MeterRegistry registry) {
final Timer pinnedTimer =
Timer.builder("jvm.virtual.thread.pinned")
.tags(tags)
.description("The duration of virtual threads that were pinned to a physical thread")
.register(registry);

final Counter submitFailedCounter =
Counter.builder("jvm.virtual.thread.submit.failed")
.tags(tags)
.description("The number of virtual thread submissions that failed")
.register(registry);

recordingStream.onEvent("jdk.VirtualThreadPinnedEvent", event ->
pinnedTimer.record(event.getDuration()));

recordingStream.onEvent("jdk.VirtualThreadSubmitFailedEvent",
event -> submitFailedCounter.increment());
}

@Override
public void close() throws Exception {
recordingStream.close();
}
}