1818package dev.cubxity.plugins.metrics.common.metric.system.thread
1919
2020import dev.cubxity.plugins.metrics.api.metric.collector.Collector
21+ import dev.cubxity.plugins.metrics.api.metric.collector.NANOSECONDS_PER_SECOND
2122import dev.cubxity.plugins.metrics.api.metric.data.CounterMetric
2223import dev.cubxity.plugins.metrics.api.metric.data.GaugeMetric
2324import dev.cubxity.plugins.metrics.api.metric.data.Metric
@@ -26,10 +27,29 @@ import java.lang.management.ManagementFactory
2627class 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