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

Add metrics for the total number of application threads started in the JVM #3562

Merged
merged 6 commits into from
Feb 17, 2023
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 @@ -17,6 +17,7 @@

import io.micrometer.common.lang.NonNullApi;
import io.micrometer.common.lang.NonNullFields;
import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
Expand Down Expand Up @@ -66,6 +67,10 @@ public void bindTo(MeterRegistry registry) {
.description("The current number of live threads including both daemon and non-daemon threads")
.baseUnit(BaseUnits.THREADS).register(registry);

FunctionCounter.builder("jvm.threads.started", threadBean, ThreadMXBean::getTotalStartedThreadCount).tags(tags)
.description("The total number of application threads started in the JVM").baseUnit(BaseUnits.THREADS)
.register(registry);

try {
threadBean.getAllThreadIds();
for (Thread.State state : Thread.State.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,20 @@ class JvmThreadMetricsTest {
void threadMetrics() {
MeterRegistry registry = new SimpleMeterRegistry();
new JvmThreadMetrics().bindTo(registry);
double initialThreadCount = registry.get("jvm.threads.started").functionCounter().count();

assertThat(registry.get("jvm.threads.live").gauge().value()).isGreaterThan(0);
assertThat(registry.get("jvm.threads.daemon").gauge().value()).isGreaterThan(0);
assertThat(registry.get("jvm.threads.peak").gauge().value()).isGreaterThan(0);
assertThat(registry.get("jvm.threads.states").tag("state", "runnable").gauge().value()).isGreaterThan(0);
assertThat(registry.get("jvm.threads.live").gauge().value()).isPositive();
assertThat(registry.get("jvm.threads.daemon").gauge().value()).isPositive();
assertThat(registry.get("jvm.threads.peak").gauge().value()).isPositive();
assertThat(registry.get("jvm.threads.states").tag("state", "runnable").gauge().value()).isPositive();

createBlockedThread();
assertThat(registry.get("jvm.threads.states").tag("state", "blocked").gauge().value()).isGreaterThan(0);
assertThat(registry.get("jvm.threads.states").tag("state", "waiting").gauge().value()).isGreaterThan(0);
assertThat(registry.get("jvm.threads.states").tag("state", "blocked").gauge().value()).isPositive();
assertThat(registry.get("jvm.threads.states").tag("state", "waiting").gauge().value()).isPositive();

createTimedWaitingThread();
assertThat(registry.get("jvm.threads.states").tag("state", "timed-waiting").gauge().value()).isGreaterThan(0);
assertThat(registry.get("jvm.threads.states").tag("state", "timed-waiting").gauge().value()).isPositive();
assertThat(registry.get("jvm.threads.started").functionCounter().count()).isGreaterThan(initialThreadCount);
}

@Test
Expand Down