Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())))));
}
}