add daemon attribute to process.runtime.jvm.threads.count
has closed
MODIFY
instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/Threads.java
instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/Threads.java
package io.opentelemetry.instrumentation.runtimemetrics;
import io.opentelemetry.api.OpenTelemetry;
+import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.Meter;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
@@ -22,7 +23,8 @@ import java.lang.management.ThreadMXBean;
* <p>Example metrics being exported:
*
* <pre>
- * process.runtime.jvm.threads.count 4
+ * process.runtime.jvm.threads.count{daemon=true} 2
+ * process.runtime.jvm.threads.count{daemon=false} 5
* </pre>
*/
public final class Threads {
@@ -30,6 +32,8 @@ public final class Threads {
// Visible for testing
static final Threads INSTANCE = new Threads();
+ static final String DAEMON_KEY = "daemon";
+
/** Register observers for java runtime class metrics. */
public static void registerObservers(OpenTelemetry openTelemetry) {
INSTANCE.registerObservers(openTelemetry, ManagementFactory.getThreadMXBean());
@@ -44,7 +48,14 @@ public final class Threads {
.setDescription("Number of executing threads")
.setUnit("1")
.buildWithCallback(
- observableMeasurement -> observableMeasurement.record(threadBean.getThreadCount()));
+ observableMeasurement -> {
+ observableMeasurement.record(
+ threadBean.getDaemonThreadCount(),
+ Attributes.builder().put(DAEMON_KEY, true).build());
+ observableMeasurement.record(
+ threadBean.getThreadCount() - threadBean.getDaemonThreadCount(),
+ Attributes.builder().put(DAEMON_KEY, false).build());
+ });
}
private Threads() {}

MODIFY
instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/ThreadsTest.java
instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/ThreadsTest.java
package io.opentelemetry.instrumentation.runtimemetrics;
+import static io.opentelemetry.instrumentation.runtimemetrics.Threads.DAEMON_KEY;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static org.mockito.Mockito.when;
@@ -28,7 +29,8 @@ class ThreadsTest {
@Test
void registerObservers() {
- when(threadBean.getThreadCount()).thenReturn(3);
+ when(threadBean.getThreadCount()).thenReturn(7);
+ when(threadBean.getDaemonThreadCount()).thenReturn(2);
Threads.INSTANCE.registerObservers(testing.getOpenTelemetry(), threadBean);
@@ -46,6 +48,18 @@ class ThreadsTest {
sum.isNotMonotonic()
.hasPointsSatisfying(
point ->
- point.hasValue(3).hasAttributes(Attributes.empty())))));
+ point
+ .hasValue(2)
+ .hasAttributes(
+ Attributes.builder()
+ .put(DAEMON_KEY, true)
+ .build()),
+ point ->
+ point
+ .hasValue(5)
+ .hasAttributes(
+ Attributes.builder()
+ .put(DAEMON_KEY, false)
+ .build())))));
}
}

add daemon attribute to process.runtime.jvm.threads.count (#6635)

Signed-off-by: HaoTu <haotu@alauda.io>

#6561  has closed

Signed-off-by: HaoTu <haotu@alauda.io>