-
Notifications
You must be signed in to change notification settings - Fork 868
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
Fill jvm.thread.state attribute for jvm.thread.count metric on jdk8 #12724
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 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 |
---|---|---|
|
@@ -25,6 +25,8 @@ | |
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
|
@@ -55,11 +57,34 @@ public final class Threads { | |
|
||
/** Register observers for java runtime class metrics. */ | ||
public static List<AutoCloseable> registerObservers(OpenTelemetry openTelemetry) { | ||
return INSTANCE.registerObservers(openTelemetry, ManagementFactory.getThreadMXBean()); | ||
return INSTANCE.registerObservers(openTelemetry, !isJava9OrNewer()); | ||
} | ||
|
||
private List<AutoCloseable> registerObservers(OpenTelemetry openTelemetry, boolean useThread) { | ||
if (useThread) { | ||
return registerObservers(openTelemetry, Threads::getThreads); | ||
} | ||
return registerObservers(openTelemetry, ManagementFactory.getThreadMXBean()); | ||
} | ||
|
||
// Visible for testing | ||
List<AutoCloseable> registerObservers(OpenTelemetry openTelemetry, ThreadMXBean threadBean) { | ||
return registerObservers( | ||
openTelemetry, | ||
isJava9OrNewer() ? Threads::java9AndNewerCallback : Threads::java8Callback, | ||
threadBean); | ||
} | ||
|
||
// Visible for testing | ||
List<AutoCloseable> registerObservers( | ||
OpenTelemetry openTelemetry, Supplier<Thread[]> threadSupplier) { | ||
return registerObservers(openTelemetry, Threads::java8ThreadCallback, threadSupplier); | ||
} | ||
|
||
private static <T> List<AutoCloseable> registerObservers( | ||
OpenTelemetry openTelemetry, | ||
Function<T, Consumer<ObservableLongMeasurement>> callbackProvider, | ||
T threadInfo) { | ||
Meter meter = JmxRuntimeMetricsUtil.getMeter(openTelemetry); | ||
List<AutoCloseable> observables = new ArrayList<>(); | ||
|
||
|
@@ -68,8 +93,7 @@ List<AutoCloseable> registerObservers(OpenTelemetry openTelemetry, ThreadMXBean | |
.upDownCounterBuilder("jvm.thread.count") | ||
.setDescription("Number of executing platform threads.") | ||
.setUnit("{thread}") | ||
.buildWithCallback( | ||
isJava9OrNewer() ? java9AndNewerCallback(threadBean) : java8Callback(threadBean))); | ||
.buildWithCallback(callbackProvider.apply(threadInfo))); | ||
|
||
return observables; | ||
} | ||
|
@@ -104,6 +128,36 @@ private static Consumer<ObservableLongMeasurement> java8Callback(ThreadMXBean th | |
}; | ||
} | ||
|
||
private static Consumer<ObservableLongMeasurement> java8ThreadCallback( | ||
Supplier<Thread[]> supplier) { | ||
return measurement -> { | ||
Map<Attributes, Long> counts = new HashMap<>(); | ||
for (Thread thread : supplier.get()) { | ||
Attributes threadAttributes = threadAttributes(thread); | ||
counts.compute(threadAttributes, (k, value) -> value == null ? 1 : value + 1); | ||
} | ||
counts.forEach((threadAttributes, count) -> measurement.record(count, threadAttributes)); | ||
}; | ||
} | ||
|
||
// Visible for testing | ||
static Thread[] getThreads() { | ||
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. initially I planned to use reflection to call Thread.getThreads(), but that method is not available on openj9 |
||
while (threadGroup.getParent() != null) { | ||
threadGroup = threadGroup.getParent(); | ||
} | ||
// use a slightly larger array in case new threads are created | ||
int count = threadGroup.activeCount() + 10; | ||
Thread[] threads = new Thread[count]; | ||
int resultSize = threadGroup.enumerate(threads); | ||
if (resultSize == threads.length) { | ||
return threads; | ||
} | ||
Thread[] result = new Thread[resultSize]; | ||
System.arraycopy(threads, 0, result, 0, resultSize); | ||
return result; | ||
} | ||
|
||
private static Consumer<ObservableLongMeasurement> java9AndNewerCallback( | ||
ThreadMXBean threadBean) { | ||
return measurement -> { | ||
|
@@ -132,5 +186,12 @@ private static Attributes threadAttributes(ThreadInfo threadInfo) { | |
JvmAttributes.JVM_THREAD_DAEMON, isDaemon, JvmAttributes.JVM_THREAD_STATE, threadState); | ||
} | ||
|
||
private static Attributes threadAttributes(Thread thread) { | ||
boolean isDaemon = thread.isDaemon(); | ||
String threadState = thread.getState().name().toLowerCase(Locale.ROOT); | ||
return Attributes.of( | ||
JvmAttributes.JVM_THREAD_DAEMON, isDaemon, JvmAttributes.JVM_THREAD_STATE, threadState); | ||
} | ||
|
||
private Threads() {} | ||
} |
This file contains 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
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.
the
java8Callback
is only used in tests