Skip to content

Commit

Permalink
add daemon attribute to process.runtime.jvm.threads.count (open-telem…
Browse files Browse the repository at this point in the history
…etry#6635)

Signed-off-by: HaoTu <[email protected]>

open-telemetry#6561  has closed

Signed-off-by: HaoTu <[email protected]>
  • Loading branch information
Hao Tu authored and LironKS committed Dec 4, 2022
1 parent 938f00b commit 53eb672
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,14 +23,17 @@
* <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 {

// 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());
Expand All @@ -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() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand All @@ -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())))));
}
}

0 comments on commit 53eb672

Please sign in to comment.