From 23dd7d7dd209f35f62f884e9679f504a3b2c89c9 Mon Sep 17 00:00:00 2001 From: Hao Tu Date: Sun, 25 Sep 2022 02:03:00 +0800 Subject: [PATCH] add daemon attribute to process.runtime.jvm.threads.count (#6635) Signed-off-by: HaoTu #6561 has closed Signed-off-by: HaoTu --- .../runtimemetrics/Threads.java | 15 +++++++++++++-- .../runtimemetrics/ThreadsTest.java | 18 ++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) 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()))))); } }