diff --git a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/Threads.java b/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/Threads.java index fbcd4a4e71ec..cd0f9cc5d99e 100644 --- a/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/Threads.java +++ b/instrumentation/runtime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/Threads.java @@ -6,6 +6,7 @@ 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 @@ *

Example metrics being exported: * *

- *   process.runtime.jvm.threads.count 4
+ *   process.runtime.jvm.threads.count{daemon=true} 2
+ *   process.runtime.jvm.threads.count{daemon=false} 5
  * 
*/ 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 @@ void registerObservers(OpenTelemetry openTelemetry, ThreadMXBean threadBean) { .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() {} diff --git a/instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/ThreadsTest.java b/instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/ThreadsTest.java index b1128ce41faa..a2d3d0c5a81e 100644 --- a/instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/ThreadsTest.java +++ b/instrumentation/runtime-metrics/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/ThreadsTest.java @@ -5,6 +5,7 @@ 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 @@ void registerObservers() { 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()))))); } }