Skip to content

Commit a6cd230

Browse files
committed
feat: add per-thread cpu time collector
1 parent 803e987 commit a6cd230

File tree

1 file changed

+26
-6
lines changed
  • common/src/main/kotlin/dev/cubxity/plugins/metrics/common/metric/system/thread

1 file changed

+26
-6
lines changed

common/src/main/kotlin/dev/cubxity/plugins/metrics/common/metric/system/thread/ThreadCollector.kt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package dev.cubxity.plugins.metrics.common.metric.system.thread
1919

2020
import dev.cubxity.plugins.metrics.api.metric.collector.Collector
21+
import dev.cubxity.plugins.metrics.api.metric.collector.NANOSECONDS_PER_SECOND
2122
import dev.cubxity.plugins.metrics.api.metric.data.CounterMetric
2223
import dev.cubxity.plugins.metrics.api.metric.data.GaugeMetric
2324
import dev.cubxity.plugins.metrics.api.metric.data.Metric
@@ -26,10 +27,29 @@ import java.lang.management.ManagementFactory
2627
class ThreadCollector : Collector {
2728
private val bean = ManagementFactory.getThreadMXBean()
2829

29-
override fun collect(): List<Metric> = listOf(
30-
GaugeMetric("jvm_threads_current_count", value = bean.threadCount),
31-
GaugeMetric("jvm_threads_daemon_count", value = bean.daemonThreadCount),
32-
CounterMetric("jvm_threads_started_total", value = bean.totalStartedThreadCount),
33-
GaugeMetric("jvm_threads_peak", value = bean.peakThreadCount),
34-
)
30+
override fun collect(): List<Metric> {
31+
val ids = bean.allThreadIds
32+
val list = ArrayList<Metric>(4 + 2 * ids.size)
33+
34+
list += GaugeMetric("jvm_threads_current_count", value = bean.threadCount)
35+
list += GaugeMetric("jvm_threads_daemon_count", value = bean.daemonThreadCount)
36+
list += CounterMetric("jvm_threads_started_total", value = bean.totalStartedThreadCount)
37+
list += GaugeMetric("jvm_threads_peak", value = bean.peakThreadCount)
38+
39+
ids.forEach { id ->
40+
val info = bean.getThreadInfo(id)
41+
val labels = mapOf("thread" to info.threadName)
42+
list += CounterMetric(
43+
"jvm_threads_cpu_time_total",
44+
value = bean.getThreadCpuTime(id) / NANOSECONDS_PER_SECOND,
45+
labels = labels
46+
)
47+
list += CounterMetric(
48+
"jvm_threads_user_time_total",
49+
value = bean.getThreadUserTime(id) / NANOSECONDS_PER_SECOND,
50+
labels = labels
51+
)
52+
}
53+
return list
54+
}
3555
}

0 commit comments

Comments
 (0)