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 <haotu@alauda.io>

open-telemetry#6561  has closed

Signed-off-by: HaoTu <haotu@alauda.io>
  • Loading branch information
tuhao1020 authored and LironKS committed Oct 31, 2022
1 parent ee58cd9 commit 3490fda
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 3490fda

Please sign in to comment.